Home Database Mysql Tutorial Mysql负载均衡_MySQL

Mysql负载均衡_MySQL

Jun 01, 2016 pm 01:10 PM

MYSQL负载均衡集群的搭建 - zhuzhu - 五事九思  

按照此配置完全可以配置成功!!

一、介绍测试环境:
Server1:ndbd 192.168.1.225
Server2:ndbd 192.168.1.226
Server3:mysqld --ndb-cluster 192.168.1.224 (ndbd_mgm ndbd_mgmd也在本机)
Server4:LVS 192.168.1.111 (调度主服务器,利用此服务器进行MYSQL的负载均衡,否则MYSQL CLUSTER只做到了数据同步的作用,好像在机制内部MYSQL NDB的各各节点上也会有负载均衡这一说!) RH AS4

ndbd: 数据库节点,需要更大的内存。
mysqld --ndb-cluster: MySQL服务器节点,程序直接访问的是这台机器的IP。默认端口仍是3306,。
ndbd_mgm ndbd_mgmd:管理节点。管理/查看各库节点和服务器节点的状态,最好自己单独一台服务器,测试发现只要ndbd_mgmd程序死掉,所有的节点全部停止工作.

二、在Server1、Server2、Server3上安装MySQL 1.安装:
# mv mysql-5.1.23-ndb-6.2.15-linux-i686-glibc23.tar.gz /usr/local/
# cd /usr/local/
# tar -zxvf mysql-5.1.23-ndb-6.2.15-linux-i686-glibc23.tar.gz
# ln -s mysql-5.1.23-ndb-6.2.15-linux-i686-glibc23.tar.gz mysql
# groupadd mysql
# useradd -g mysql mysql
# cd mysql
# scripts/mysql_install_db --user=mysql
# chown -R mysql:mysql .

2.编辑配置文件:
# cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf
# vi /etc/my.cnf 在文件尾加入 ............................................................
# Options for mysqld process:
[MYSQLD]
ndbcluster # run NDB engine
ndb-connectstring=192.168.1.224 # location of MGM node

# Options for ndbd process:
[MYSQL_CLUSTER]
ndb-connectstring=192.168.1.224 # location of MGM node ............................................................ 3.在Server1、Server2上创建日志文件夹,默认
# mkdir /var/lib/mysql-cluster

4.在Server3上创建ndb_mgmd启动配置文件:
# mkdir /var/lib/mysql-cluster
# cd /var/lib/mysql-cluster
# vi config.ini
内容如下:
Options affecting ndbd processes on all data nodes:
[NDBD DEFAULT] 
NoOfReplicas=2 # Number of replicas
DataMemory=80M # How much memory to allocate for data storage
IndexMemory=18M # How much memory to allocate for index storage
# For DataMemory and IndexMemory, we have used the
# default values. Since the "world" database takes up
# only about 500KB, this should be more than enough for
# this example Cluster setup. # TCP/IP options:
[TCP DEFAULT] 
portnumber=2202 # This the default; however, you can use any
# port that is free for all the hosts in cluster
# Note: It is recommended beginning with MySQL 5.0 that
# you do not specify the portnumber at all and simply allow
# the default value to be used instead # Management process options:
[NDB_MGMD] 
hostname=192.168.1.224 # Hostname or IP address of MGM node
datadir=/var/lib/mysql-cluster # Directory for MGM node logfiles # Options for data node "A":
[NDBD] 
# (one [NDBD] section per data node)
hostname=192.168.1.225 # Hostname or IP address
datadir=/usr/local/mysql/data # Directory for this data node's datafiles # Options for data node "B":
[NDBD] 
hostname=192.168.1.226 # Hostname or IP address
datadir=/usr/local/mysql/data # Directory for this data node's datafiles # SQL node options:
[MYSQLD] 
[MYSQLD]
[MYSQLD] 
#hostname=192.168.1.224 # Hostname or IP address
# (additional mysqld connections can be
# specified for this node for various
# purposes such as running ndb_restore)

保存&退出

[MYSQLD]
[MYSQLD]
[MYSQLD]
表示允许有三台MySQL服务器从任何IP访问数据库结点。

各参数功能,请参考http://dev.mysql.com/doc/refman/ ... config-example.html

============
Server3 /usr/local/mysql/bin/ndb_mgmd -f /var/lib/mysql-cluster/config.ini
Server1 /usr/local/mysql/bin/ndbd --initial (只在第一次启动ndbd时使用--initial参数,通知ndbd执行初始化启动。初始化启动将删除以前ndbd实例为恢复目的创建的任何文件。它还能重新创建恢复用日志文件。注意,在某些操作系统上,该进程可能会占用较长的时间)
Server2 /usr/local/mysql/bin/ndbd --initial
Server3 /usr/local/mysql/support-files/mysql.Server start

MYSQL负载均衡集群的搭建

四、检查工作状态回到管理节点服务器Server3上,并启动管理终端:

# /usr/bin/ndb_mgm
键入show命令查看当前工作状态:(下面是一个状态输出示例)

ndb_mgm> show
Cluster Configuration [ndbd(NDB)] 2 node(s)
id=2 (not connected, accepting connect from 192.168.1.225)
id=3 @192.168.1.226 (Version: 5.0.22, Nodegroup: 0, Master)

[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.1.224 (Version: 5.0.22)

[mysqld(API)] 3 node(s)
id=4 @192.168.1.224 (Version: 5.0.22)
id=5 (not connected, accepting connect from any host)
id=6 (not connected, accepting connect from any host)

mysqld(API)即mysqld --ndb-cluster,MySQL服务器节点。当前只有Server3 MySQL启动。

如果上面没有问题,现在开始测试MySQL:
注意,这篇文档对于MySQL并没有设置root密码,推荐你自己设置Server1、Server2、Server3的MySQL root密码。

在Server3中:
# /usr/local/mysql/bin/mysql
> use test;
> CREATE TABLE ctest (i INT) ENGINE=NDBCLUSTER;(一定要加上ENGINE=NDBCLUSTER或ENGINE=NDB,否则各主机无法同步数据!!)
> INSERT INTO ctest () VALUES (1);
> SELECT * FROM ctest;

应该可以看到1 row returned信息(返回数值1)。

如果上述正常,则换到Server1、Server2上重复上面的测试,观察效果,数据库应该是同步刷新的。但首先要启动这两台机器的MySQL服务
# /usr/local/mysql/support-files/mysql.Server start
如果都没有问题,那么恭喜成功!

FAQ: 查找所有ndb和sql相关进程,杀掉,重新按顺序来。
ps -aux |grep ndb
ps -aux |grep sql
kill -9 ...
/usr/local/mysql/bin/ndb_mgm -e shutdown(此命令会关闭所有主机节点)

五、破坏性测试将Server1或Server2的网线拔掉,观察另外一台集群服务器工作是否正常(可以使用SELECT查询测试)。测试完毕后,重新插入网线即可。

如果你接触不到物理服务器,也就是说不能拔掉网线,那也可以这样测试:
在Server1或Server2上:
ifconfig eth0 down #如果只有一块网卡,默认是eth0

之后在Server3上使用SELECT查询测试。并且在管理节点服务器的管理终端中执行show命令会看到被破坏的那台服务器的状态。
测试完成后,只需要重新启动被破坏服务器的ndbd进程即可:
# ndbd
注意!前面说过了,此时是不用加--inital参数的!
至此,MySQL集群就配置完成,MySQL负载均衡功能完成!

六.注意事项:
1.ndb_mgmd管理服务器,防火墙勿忘开1186端口.
2.ndb数据服务器,防火墙勿忘开2202端口.
3.LVS主服务器应该打开3306端口.
4.创建表时注意,在表后一定要加上ENGINE=NDBCLUSTER或ENGINE=NDB,否则各主机无法同步数据!! 七:用于MySQL簇进程的命令选项
1.ndb_mgm -e show (查看各节点状态)

2. --skip-ndbcluster (禁止NDB簇存储引擎。对于包含该功能的二进制版本,在默认情况下,该功能是被禁止的,换句话讲,NDB簇存储引擎处于禁止状态,直至使用“—ndbcluster”选项激活了它为止。仅当所编译的服务器支持NDB簇存储引擎时,才能使用该选项。)

--ndb-connectstring=connect_string (使用NDB存储引擎时,通过设置该选项,能够指定分配簇配置数据的管理服务器)

3.ndb>

? SHOW
在使用多个管理节点的簇中,该命令仅显示与当前管理服务器实际相连的数据节点的信息

? node_id START

启动由node_id标识的数据节点(或所有数据节点)。

? node_id STOP

停止由node_id标识的数据节点(或所有数据节点)。

? node_id RESTART [-N] [-I]

重启由node_id标识的数据节点(或所有数据节点)。

? node_id STATUS

显示由node_id标识的数据节点(或所有数据节点)的状态信息。

? ENTER SINGLE USER MODE node_id

进入单用户模式,仅允许由节点ID“node_id”标识的MySQL服务器访问数据库。

? EXIT SINGLE USER MODE

退出单用户模式,允许所有的SQL节点(即所有运行的mysqld进程)访问数据库。

? QUIT

中止管理客户端。

? SHUTDOWN

关闭除SQL节点之外的所有簇节点,并退出。

4.用单用户模式,数据库管理员能够将对数据库系统的访问限制在1个MySQL服务器(SQL节点)。进入单用户模式时,与所有其他MySQL服务器的所有连接均将恰当关闭,而且所有正在运行的事务均将被放弃。不允许启动任何新事务.
NDB> ENTER SINGLE USER MODE 5
执行该命令而且簇进入单用户模式后,节点ID为5的SQL节点将成为簇中唯一允许的用户
NDB> EXIT SINGLE USER MODE 八.MYSQL CLUSTER 备份与恢复
1、在管理节点上进行备份。
ndb_mgm> start backup nowait
ndb_mgm> Node 3: Backup 4 started from node 1
Node 3: Backup 4 started from node 1 completed
StartGCP: 43010 StopGCP: 43013
#Records: 2138 #LogRecords: 0
Data: 53068 bytes Log: 0 bytes ndb_mgm> shutdown
Node 3: Cluster shutdown initiated
Node 4: Cluster shutdown initiated
Node 4: Node shutdown completed.
Node 3: Node shutdown completed.
2 NDB Cluster node(s) have shutdown.
Disconnecting to allow management server to shutdown.
ndb_mgm> exit
2、删掉SQL节点的数据。
DROP DATABASE TEST_CLUSTER;
3、关闭MYSQLD服务器。
[root@localhost bin]# service mysqld stop
Shutting down MySQL… SUCCESS!
4、重新顺序启动所有节点。
[root@localhost mysql]# /usr/local/mysql/ndb_mgmd -f /etc/config.ini
[root@localhost data]# /usr/local/mysql/bin/ndbd –initial
我发现如果不带这个 –initial选项的话,恢复会失败。

[root@localhost bin]# service mysqld start
Starting MySQL SUCCESS!

4、在NDBD节点上进行恢复。(每个节点都得执行一次,因为数据分散在两个节点上) [root@localhost BACKUP]#
/usr/local/mysql/bin/ndb_restore -n3 -b4 -r -m /
–backup_path=/usr/local/mysql/data/BACKUP/BACKUP-4/

-r开关是记录集合。
-m是元数据。就是表和库的SCHEMA。
Nodeid = 3
Backup Id = 4
backup path = /usr/local/mysql/data/BACKUP/BACKUP-4/
Ndb version in backup files: Version 5.1.21
Connected to ndb!!
Successfully restored table `test_cluster/def/lk4_test` Successfully created index `PRIMARY` on `lk4_test` _____________________________________________________
Processing data in table: test_cluster/def/lk4_test54) fragment 1
_____________________________________________________ Restored 37 tuples and 0 log entries

NDBT_ProgramExit: 0 - OK [root@localhost BACKUP-1]#
/usr/local/mysql/bin/ndb_restore -n4 -b4 -r /
–backup_path=/usr/local/mysql/data/BACKUP/BACKUP-4/

Nodeid = 4
Backup Id = 4
backup path = /usr/local/mysql/data/BACKUP/BACKUP-4/
Ndb version in backup files: Version 5.1.21
Connected to ndb!!
_____________________________________________________
Processing data in table: sys/def/NDB$EVENTS_0(1) fragment 1
_____________________________________________________
Processing data in table: mysql/def/ndb_apply_status(4) fragment 1
_____________________________________________________
Processing data in table: mysql/def/NDB$BLOB_2_3(3) fragment 1
_____________________________________________________
Processing data in table: test/def/t11(5) fragment 1
_____________________________________________________
Processing data in table: sys/def/SYSTAB_0(0) fragment 1
_____________________________________________________
Processing data in table: mysql/def/ndb_schema(2) fragment 1
Restored 2 tuples and 0 log entries

NDBT_ProgramExit: 0 - OK 5、查看一下有没有数据,为了安全起见。
mysql> show databases; | Database | | information_schema |
| mysql |
| test | 3 rows in set (0.00 sec) MYSQL现在必须重新建立SCHEMA。

mysql> create database test_cluster;
Query OK, 1 row affected (0.33 sec)

mysql> use test_cluster;
Database changed
mysql> show tables; | Tables_in_test_cluster | | lk4_test | +——————————+
27 rows in set (0.11 sec)

mysql> select * from cs_comment;
Empty set (0.00 sec)

不过MYSQL的backup 程序现在还只能进行完全备份。

[root@localhost BACKUP]# du -h
76K ./BACKUP-2
96K ./BACKUP-6
180K ./BACKUP-4
172K ./BACKUP-3
76K ./BACKUP-1
60K ./BACKUP-5
668K .

6、在NDBD节点上进行恢复的时候有一个要注意的问题。
因为NDBD节点以 –initial 方式启动的时候不会自动删除undo 和 data 文件(即保存到磁盘上的表数据),所以得手动在每个NDBD节点上进行RM操作:

[root@node239 ndb_6_fs]# rm -rf *.dat

然后开始备份。
在MASTER上备份的时候要加 -m 开关。
在SLAVE上要加-d 而且不要-m开关。

具体步骤如下:
MASTER :

[root@localhost ndb_3_fs]# /usr/local/mysql/bin/ndb_restore -n3 -b1 -r -m –backup_path=/usr/local/mysql/data/BACKUP/BACKUP-1/
Nodeid = 3
Backup Id = 1
backup path = /usr/local/mysql/data/BACKUP/BACKUP-1/
Ndb version in backup files: Version 5.1.21
Connected to ndb!!
Creating logfile group: lg_1…done
Creating tablespace: ts_1…done
Creating datafile “data_1.dat”…done
Creating undofile “undo_1.dat”…done
Successfully restored table `test/def/t11`
Successfully restored table event REPL$test/t11
_____________________________________________________
Processing data in table: sys/def/NDB$EVENTS_0(1) fragment 0
_____________________________________________________
Processing data in table: mysql/def/NDB$BLOB_2_3(3) fragment 0
_____________________________________________________
Processing data in table: sys/def/SYSTAB_0(0) fragment 0
_____________________________________________________
Processing data in table: mysql/def/ndb_schema(2) fragment 0
_____________________________________________________
Processing data in table: mysql/def/ndb_apply_status(4) fragment 0
_____________________________________________________
Processing data in table: test/def/t11(10) fragment 0
Restored 26 tuples and 0 log entries

NDBT_ProgramExit: 0 - OK

其他的SLAVE上的操作:

[root@node239 ndb_6_fs]# /usr/local/mysql/bin/ndb_restore -n6 -b1 -r -d –backup_path=/usr/local/mysql/data/BACKUP/BACKUP-1/
Nodeid = 6
Backup Id = 1
backup path = /usr/local/mysql/data/BACKUP/BACKUP-1/
Ndb version in backup files: Version 5.1.21
Connected to ndb!!
_____________________________________________________
Processing data in table: sys/def/NDB$EVENTS_0(1) fragment 3
_____________________________________________________
Processing data in table: mysql/def/NDB$BLOB_2_3(3) fragment 3
_____________________________________________________
Processing data in table: sys/def/SYSTAB_0(0) fragment 3
_____________________________________________________
Processing data in table: mysql/def/ndb_schema(2) fragment 3
_____________________________________________________
Processing data in table: mysql/def/ndb_apply_status(4) fragment 3
_____________________________________________________
Processing data in table: test/def/t11(10) fragment 3
Restored 20 tuples and 0 log entries

NDBT_ProgramExit: 0 - OK

-d 开关的意思即:
-d, –no-restore-disk-objects
Dont restore disk objects (tablespace/logfilegroups etc)
既忽略表空间和分组空间

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

Can mysql and mariadb coexist Can mysql and mariadb coexist Apr 08, 2025 pm 02:27 PM

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Apr 02, 2025 pm 07:05 PM

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

See all articles