log_slave_updates
sync-binlog=1
[root@node1 mysql-5.6.23]# /etc/init.d/mysqld restart
ERROR! MySQL server PID file could not be found!
Starting MySQL. SUCCESS!
[root@node1 mysql-5.6.23]#
3.配置主主复制
node1节点:
[root@node1 mysql-5.6.23]# /usr/local/mysql-5.6.23/bin/mysqladmin -u root password 'system' --修改初始密码为system
Warning: Using a password on the command line interface can be insecure.
[root@node1 mysql-5.6.23]# /usr/local/mysql-5.6.23/bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.23-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant replication slave,replication client on *.* to repl_user@'192.168.1.251' identified by 'system!#%246'; --创建复制用户
Query OK, 0 rows affected (0.05 sec)
mysql> show master status; --查看node1节点的二进制位置
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 690 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
node2节点:
[root@node2 mysql-5.6.23]# /usr/local/mysql-5.6.23/bin/mysqladmin -u root password 'system'
Warning: Using a password on the command line interface can be insecure.
[root@node2 mysql-5.6.23]# /usr/local/mysql-5.6.23/bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.23-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant replication slave,replication client on *.* to repl_user@'192.168.1.250' identified by 'system!#%246';
Query OK, 0 rows affected (0.06 sec)
4.测试主主同步是否正常(在两个节点各写一行数据,在两个节点查看数据)
node1节点:
mysql> \u tong
Database changed
mysql> create table t (a int);
Query OK, 0 rows affected (0.33 sec)
mysql> insert into t values(1);
Query OK, 1 row affected (0.08 sec)
mysql>
node2节点:
mysql> \u tong
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t;
+------+
| a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> insert into t values(2);
Query OK, 1 row affected (0.12 sec)
mysql> select * from t;
+------+
| a |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
mysql>
node1节点:
mysql> select * from t;
+------+
| a |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)
6.启动服务和测试状态
node1节点和node1节点:
[root@node2 keepalived]# /etc/init.d/mysqld restart --两个节点启动服务
ERROR! MySQL server PID file could not be found!
Starting MySQL. SUCCESS!
[root@node2 keepalived]# /etc/init.d/keepalived restart --两个节点启动服务
Stopping keepalived: [FAILED]
Starting keepalived: [ OK ]
[root@node2 etc]# /usr/local/mysql-5.6.23/bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.23-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant all privileges on *.* to remote@'%' identified by 'system'; --在两个节点创建相同的远程登陆用户
Query OK, 0 rows affected (0.07 sec)
node2节点:
[root@node2 keepalived]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether d0:27:88:7d:83:9b brd ff:ff:ff:ff:ff:ff
inet 192.168.1.251/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.201/32 scope global eth0 --VIP地址在node2已经启动
inet6 fe80::d227:88ff:fe7d:839b/64 scope link
valid_lft forever preferred_lft forever
[root@node2 keepalived]#
用mysql客户端登陆VIP地址:
[root@node3 keepalived]# /usr/local/mysql-5.6.23/bin/mysql -u remote -p -h 192.168.1.201
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.6.23-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> \u tong
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> create table g (a int);
Query OK, 0 rows affected (0.29 sec)
mysql> insert into g values(1);
Query OK, 1 row affected (0.09 sec)
mysql> select * from g;
+------+
| a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> exit
Bye
[root@node2 keepalived]# /etc/init.d/mysqld stop --关闭node2节点的mysql服务
Shutting down MySQL.... SUCCESS!
[root@node2 keepalived]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether d0:27:88:7d:83:9b brd ff:ff:ff:ff:ff:ff
inet 192.168.1.251/24 brd 192.168.1.255 scope global eth0 --VIP不见了
inet6 fe80::d227:88ff:fe7d:839b/64 scope link
valid_lft forever preferred_lft forever
[root@node2 keepalived]#
node1节点:
[root@node1 keepalived]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 10:78:d2:c9:50:28 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.250/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.201/32 scope global eth0 --VIP在node1启动了
inet6 fe80::1278:d2ff:fec9:5028/64 scope link
valid_lft forever preferred_lft forever
[root@node1 keepalived]#
用mysql客户端登陆VIP地址:
[root@node3 bin]# ./mysql -u remote -p -h 192.168.1.201 -P 3306
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 48
Server version: 5.6.23-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> \u tong
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from g; --数据同步了
+------+
| a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)