后续添加:
Keepalived 实现mysql故障迁移
heartbeat+DRBD+mysql高可用
MMM构建mysql高可用集群
配置环境
[root@wdds_1 ~]# uname -a
Linux wdds_1 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux[root@wdds_1 ~]# ifconfig eth0 |grep "inet addr:"
inet addr:10.0.0.61 Bcast:10.0.0.255 Mask:255.255.255.0
[root@wdds_2 ~]# ifconfig eth0|grep "inet addr:"
inet addr:10.0.0.62 Bcast:10.0.0.255 Mask:255.255.255.0
2.安装mysql
yum install mysql*
3、配置my.cnf文件
节点一:
[root@wdds_1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id = 1
log-bin = mysql-relay-bin
relay-log = mysql-relay-bin
replicate-wild-ignore-table = mysql.%
replicate-wild-ignore-table = test.%
replicate-wild-ignore-table = information_schema.%
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
节点二:
[root@wdds_2 ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
server-id = 2
log-bin = mysql-relay-bin
relay-log = mysql-relay-bin
replicate-wild-ignore-table = mysql.%
replicate-wild-ignore-table = test.%
replicate-wild-ignore-table = information_schema.%
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
4、在同步之前,需要先手动同步数据库,保证数据库一致性
同步之前需要先锁表:
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
同步方法:
1)使用mysqldump工具导出,然后在导入到另一节点中
2)直接打包/var/lib/mysql 文件或其他mysql 的date文件(因这是测试 就直接打包了)
tar zcvf mysql.tar.gz /var/lib/mysql 然后scp到另一节点上。
5、在两个节点上分别创建复制的用户:
节点一:
mysql> grant replication slave on *.* to 'repl_user'@'%' identified by '123.com';
mysql> show master status;
+------------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------------+----------+--------------+------------------+
| mysql-relay-bin.000004 | 1231 | | |
+------------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
节点二:
mysql> grant replication slave on *.* to 'repl_user'@'10.0.0.61' identified by '123.com';
mysql> show master status;
+------------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------------+----------+--------------+------------------+
| mysql-relay-bin.000002 | 480 | | |
+------------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
6、配置各自的主节点并:
节点一:
mysql> change master to master_host='10.0.0.62', master_user='repl_user', master_password='123.com', master_log_file='mysql-relay-bin.000002', master_log_pos=480;
启动:mysql>start slave;
查看启动后的状态:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.62
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-relay-bin.000002
Read_Master_Log_Pos: 480
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-relay-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table: mysql.%,test.%,information_schema.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 480
Relay_Log_Space: 522
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
节点二:
mysql> change master to master_host='10.0.0.61', master_user='repl_user', master_password='123.com', master_log_file='mysql-relay-bin.000004', master_log_pos=480;
启动:mysql>start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.61
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-relay-bin.000004
Read_Master_Log_Pos: 1231
Relay_Log_File: mysql-relay-bin.000004
Relay_Log_Pos: 1227
Relay_Master_Log_File: mysql-relay-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table: mysql.%,test.%,information_schema.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1231
Relay_Log_Space: 1488
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.01 sec)
7、测试:分别在两个节点上创建数据库 或表,在另一个节点上查看是否已经创建。
创建一个数据库 create database test100 character set utf8;
查看数据库 show databases;