[root@master ~]# service mysqld restart
Shutting down MySQL.. [ OK ]
Starting MySQL. [ OK ]
[root@slave ~]# service mysqld restart
Shutting down MySQL.. [ OK ]
Starting MySQL. [ OK ]
step4:查看主从的log-bin日志状态
记录File和Position的值
[root@master ~]# mysql -uroot -ppasswd -e 'show master status'
Warning: Using a password on the command line interface can be insecure.
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 414 | | | |
+------------------+----------+--------------+------------------+-------------------+
[root@slave ~]# mysql -uroot -ppasswd -e 'show master status'
Warning: Using a password on the command line interface can be insecure.
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 414 | | | |
+------------------+----------+--------------+------------------+-------------------+
step5:创建主从同步replication用户
1、master
mysql> grant replication slave on *.* to 'replication'@'192.168.1.211' identified by 'replication';
mysql> flush privileges;
mysql> change master to
-> master_host='192.168.1.211',
-> master_user='replication',
-> master_password='replication',
-> master_port=3306,
-> master_log_file='mysql-bin.000001',
-> master_log_pos=414;
mysql> start slave;
2、slave
mysql> grant replication slave on *.* to 'replication'@'192.168.1.210' identified by 'replication';
mysql> flush privileges;
mysql> change master to
-> master_host='192.168.1.210',
-> master_user='replication',
-> master_password='replication',
-> master_port=3306,
-> master_log_file='mysql-bin.000001',
-> master_log_pos=414;
mysql> start slave;
同步失败可能需要停止或重设slave
mysql> stop slave;
mysql> reset slave;
step6:分别在master和slave上查看slave状态,验证是否成功配置主主复制模式
1、master
2、slave
slave状态同步过程可能需要重启MySQL服务
[root@master ~]# service mysqld restart
[root@slave ~]# service mysqld restart
[root@slave ~]# ls /usr/sbin/keepalived
/usr/sbin/keepalived
[root@slave ~]# ls /etc/init.d/keepalived
/etc/init.d/keepalived
[root@slave ~]# ls /etc/keepalived/keepalived.conf
/etc/keepalived/keepalived.conf
mysql> show variables like 'hostname%';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| hostname | master |
+---------------+--------+
1 row in set (0.00 sec)
从上面查看的结果看样看出在正常情况下连接的是master
2、故障测试,停止master的MySQL服务,再次查看是否转移至slave服务器上
[root@master ~]# service mysqld stop
Shutting down MySQL.... SUCCESS!
mysql> show variables like 'hostname%';
ERROR 2013 (HY000): Lost connection to MySQL server during query
mysql> show variables like 'hostname%';
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 1268
Current database: *** NONE ***
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| hostname | slave |
+---------------+-------+
1 row in set (0.01 sec)