部署搭建MySQL双向主从复制流程
admin
2023-04-21 10:24:40
0

下面一起来了解下部署搭建MySQL双向主从复制流程,相信大家看完肯定会受益匪浅,文字在精不在多,希望部署搭建MySQL双向主从复制流程这篇短内容是你想要的。

作为Master云服务器apenglinux-001.cn的配置
/ -- 建库,表,备份库,将备份传给另一台机器-- /
[root@apenglinux-001 ~]# 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   |
+----+----------+

[root@apenglinux-001 ~]# mysqldump -uroot -p123456 -B db1 > db1_all.sql

[root@apenglinux-001 ~]# mysqldump -uroot -p123456 -B db1 > db1_all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@apenglinux-001 ~]# 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.
root@192.168.1.20's password: 

db1_all.sql                                                      100% 2024   856.5KB/s   00:00

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

/ -- 开启MySQL服务-- /
[root@apenglinux-001 ~]# systemctl restart mysqld

/-- 作为master时,授权,将表锁定--/
[root@apenglinux-001 ~]# mysql -uroot -p123456 -e "grant replication slave on . to slave@192.168.1.20 identified by '123';flush tables with read lock;"

作为slave云服务器apenglinux-002的配置

/-- 还原从master上传过来的数据库-- /
[root@apenglinux-002 ~]# 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--/
[root@apenglinux-002 ~]# systemctl restart mysqld
/ -- 停止slave,指定主云服务器的ip,user,password,log_file,log_pos,开启slave-- /
[root@apenglinux-002 ~]# 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;"
/ -- 去主云服务器上开启解表操作 -- /
[root@apenglinux-001 ~]# mysql -uroot -p123456 -e 'unlock tables;'
/ -- 查看slave的状态 -- /
[root@apenglinux-002 ~]# 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的配置
[root@apenglinux-002 ~]# mysql -uroot -p123456 -e 'grant replication slave on . to slave@192.168.1.10 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的配置
[root@apenglinux-001 ~]# 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 上解锁
[root@apenglinux-002 ~]# mysql -uroot -p123456 -e 'unlock tables;'
/ -- 查看slave的状态 --/
[root@apenglinux-001 ~]# 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 上查看-- /
[root@apenglinux-001 ~]# 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       |
+----+----------+

[root@apenglinux-002 ~]# 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上查看-- /
[root@apenglinux-002 ~]# 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     |
+----+--------+

[root@apenglinux-001 ~]# 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     |
+----+--------+

看完部署搭建MySQL双向主从复制流程这篇文章后,很多读者朋友肯定会想要了解更多的相关内容,如需获取更多的行业信息,可以关注我们的行业资讯栏目。

相关内容

热门资讯

燃气发电与电池储能相结合,成为... 来源:市场资讯 (来源:i商周) 孟菲斯一座xAI数据中心的燃气轮机 人工智能的用电飙升,让数据中心...
景嘉微:JM11性能大幅提升,... 有投资者在互动平台向景嘉微提问:“董秘您好!关注到近期有用户反馈公司JM11显卡推出了适配windo...
原创 v... 影像的发展进一步推动,不少品牌推出了专业影像手机,拥有2亿像素摄像头、色彩还原摄像头、影像芯片、影像...
荣耀首款自研耳夹式耳机官宣即将... 快科技5月13日消息,日前,荣耀首席营销官关海涛宣布,荣耀全场景团队自研首款耳夹式耳机马上上市,并称...
谷歌推出Googlebooks... IT之家 5 月 13 日消息,2026 年 I/O 开发者大会下周(5 月 19~20 日)召开之...
自控所推动GNC专业智能化升级 来源:滚动播报 (来源:中国航空报) 本报讯 5月6日,航空工业自控所召开 GNC+AI关键技术研发...
华电电力申请数据库访问方法专利... 国家知识产权局信息显示,华电电力科学研究院有限公司申请一项名为“数据库访问方法、装置、设备及介质”的...
苏州率先打造数据流通利用新范式 数据,作为第五大生产要素 具有流动性强、非消耗性、非均质性等特点 苏州率先打造数据流通利用新范式 夯...
伊媒披露伊美新一轮谈判5个先决... 当地时间5月12日,据伊朗法尔斯通讯社援引知情人士消息报道,伊朗对与美国新一轮谈判提出的5个先决条件...
英国将向霍尔木兹海峡多国护航行... 当地时间12日,总台记者从英国国防部获悉,英国将向在霍尔木兹海峡执行任务的多国护航行动提供无人机、战...