1. 程式人生 > >MySQL雙向主從複製

MySQL雙向主從複製

作為Master伺服器apenglinux-001.cn的配置
/ -- 建庫,表,備份庫,將備份傳給另一臺機器-- /
[[email protected] ~]# mysql -uroot -p123456 -e "create database db1;use db1;create table t1(id int unsigned not null primary key auto_increment,name varchar(100));insert into t1(name)values('zhangsan'),('lisi'),('wangwu');select * from t1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
| 2 | lisi |
| 3 | wangwu |
+----+----------+

[[email protected] ~]# mysqldump -uroot -p123456 -B db1 > db1_all.sql

[[email protected] ~]# mysqldump -uroot -p123456 -B db1 > db1_all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[[email protected] ~]# scp db1_all.sql 192.168.1.20:/root
The authenticity of host '192.168.1.20 (192.168.1.20)' can't be established.
ECDSA key fingerprint is SHA256:ENfUT65MBnG5u82/aeA84Wl7klhZZMS/MI1+36eGu8k.
ECDSA key fingerprint is MD5:bb:7a:dc:8b:d2:5b:99:54:9a:8d:f2:17:81:0a:5e:72.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.20' (ECDSA) to the list of known hosts.

[email protected]'s password:
db1_all.sql 100% 2024 856.5KB/s 00:00

/ -- 配置my.cnf-- /
[[email protected] ~]# vim /etc/my.cnf
log-bin=apenglinux-001.cn
server-id=100
binlog-do-db=db1
binlog-ignore-db=mysql

/ -- 開啟MySQL服務-- /
[[email protected] ~]# systemctl restart mysqld

/-- 作為master時,授權,將表鎖定--/
[[email protected]

~]# mysql -uroot -p123456 -e "grant replication slave on . to [email protected] identified by '123';flush tables with read lock;"

作為slave伺服器apenglinux-002的配置

/-- 還原從master上傳過來的資料庫-- /
[[email protected] ~]# mysql -uroot -p123456 < db1_all.sql
/ -- 配置my.cnf-- /
validate_password=off
log-bin=apenglinux-002.cn
server-id=90
binlog-do-db=db1
binlog-ignore-db=mysql
/-- 重啟mysql--/
[[email protected] ~]# systemctl restart mysqld
/ -- 停止slave,指定主伺服器的ip,user,password,log_file,log_pos,開啟slave-- /
[[email protected] ~]# mysql -uroot -p123456 -e "stop slave;change master to master_host='192.168.1.10',master_port=3306,master_user='slave',master_password='123',master_log_file='apenglinux-001.000001',master_log_pos=449;start slave;"
/ -- 去主伺服器上開啟解表操作 -- /
[[email protected] ~]# mysql -uroot -p123456 -e 'unlock tables;'
/ -- 檢視slave的狀態 -- /
[[email protected] ~]# mysql -uroot -p123456 -e 'show slave status\G'
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
Last_IO_Errno: 0
Last_SQL_Errno: 0
作為master伺服器apenglinux-002.cn的配置
[[email protected] ~]# mysql -uroot -p123456 -e 'grant replication slave on . to [email protected] identified by "123";flush tables with read lock;show master status;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+-----------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------------+----------+--------------+------------------+-------------------+
| apenglinux-002.000001 | 449 | db1 | mysql | |
+-----------------------+----------+--------------+------------------+-------------------+
作為slave伺服器apenglinux-001.cn的配置
[[email protected] ~]# mysql -uroot -p123456 -e 'stop slave;change master to master_host="192.168.1.20",master_port=3306,master_user="slave",master_password="123",master_log_file="apenglinux-002.000001",master_log_pos=449;start slave;'
去apenglinux-002.cn 上解鎖
[[email protected] ~]# mysql -uroot -p123456 -e 'unlock tables;'
/ -- 檢視slave的狀態 --/
[[email protected] ~]# mysql -uroot -p123456 -e 'show slave status\G'
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
Last_IO_Errno: 0
Last_SQL_Errno: 0
此時 apenglinux-001與apenglinux-002兩臺機器互為主從了。
測試:
/ -- 在apenglinux-001.cn上增加一條記錄,在apenglinux-002 上檢視-- /
[[email protected] ~]# mysql -uroot -p123456 -e 'insert into db1.t1(name)values("aa");select * from db1.t1;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
| 2 | lisi |
| 3 | wangwu |
| 4 | aa |
+----+----------+

[[email protected] ~]# mysql -uroot -p123456 -e 'select * from db1.t1;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
| 2 | lisi |
| 3 | wangwu |
| 4 | aa |
+----+----------+

/ -- 在apenglinux-002.cn上刪除兩條記錄,在apenglinux-001上檢視-- /
[[email protected] ~]# mysql -uroot -p123456 -e 'delete from db1.t1 limit 2;select * from db1.t1;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+--------+
| id | name |
+----+--------+
| 3 | wangwu |
| 4 | aa |
+----+--------+

[[email protected] ~]# mysql -uroot -p123456 -e 'select * from db1.t1;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+--------+
| id | name |
+----+--------+
| 3 | wangwu |
| 4 | aa |
+----+--------+