MySQL5.7主从复制-异步复制搭建
发表于:2025-02-13 作者:千家信息网编辑
千家信息网最后更新 2025年02月13日,两台服务器,系统是Redhat6.5,MySQL版本是5.7.18。1、在主库上,创建复制使用的用户,并授予replication slave权限。这里创建用户repl,可以从IP为10.10.10.
千家信息网最后更新 2025年02月13日MySQL5.7主从复制-异步复制搭建两台服务器,系统是Redhat6.5,MySQL版本是5.7.18。
1、在主库上,创建复制使用的用户,并授予replication slave权限。这里创建用户repl,可以从IP为10.10.10.210的主机进行连接。
grant replication slave on *.* to 'repl'@'10.10.10.210' identified by 'mysql';
2、修改主服务器配置,加入如下配置:
cat /etc/my.cnf
[mysqld]
server-id=1
log-bin=mysql-bin
log-bin-index=mysql-bin.index
binlog_format=mixed
3、在主库上,设置读锁,确保没有数据操作,获得一个一致性的快照
flush tables with read lock;
4、然后在主库上获得当前二进制日志名和偏量值,改操作的目的是从库启动之后,从这个点开始恢复数据。
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
5、利用mysqldump导出数据,拷贝至从库服务器。
6、主库备份完成,恢复写操作
unlock tables;
7、修改从库的配置文件,添加如下参数,注意server-id必须是唯一的,不能和主库相同,多个从库的话,server-id不能有重复。
cat /etc/my.cnf
[mysqld]
server-id=2
8、在从库上,使用--skip-slave-start启动数据库,这样不会立即启动从库上的复制进程,方便我们进行下一步配置。
./bin/mysqld_safe --skip-slave-start &
9、对从库进行配置,指定复制使用的用户,主库的IP、端口以及开始执行复制的日志文件和位置等:
change master to
master_host='10.10.10.200',
master_port=3306,
master_user='real',
master_password='mysql',
master_log_file='mysql-bin.000006',
master_log_pos=120;
10、在从库上启动slave线程
start slave;
11、在从库上查看slave状态
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.10.200
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 120
Relay_Log_File: mysql-relay-bin.000026
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000006
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:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 120
Relay_Log_Space: 619
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 4adfcd1d-4059-11e7-9532-080027d597f9
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
12、在主库进行DDL或者DML测试,在从库查看数据同步情况
1、在主库上,创建复制使用的用户,并授予replication slave权限。这里创建用户repl,可以从IP为10.10.10.210的主机进行连接。
grant replication slave on *.* to 'repl'@'10.10.10.210' identified by 'mysql';
2、修改主服务器配置,加入如下配置:
cat /etc/my.cnf
[mysqld]
server-id=1
log-bin=mysql-bin
log-bin-index=mysql-bin.index
binlog_format=mixed
3、在主库上,设置读锁,确保没有数据操作,获得一个一致性的快照
flush tables with read lock;
4、然后在主库上获得当前二进制日志名和偏量值,改操作的目的是从库启动之后,从这个点开始恢复数据。
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
5、利用mysqldump导出数据,拷贝至从库服务器。
6、主库备份完成,恢复写操作
unlock tables;
7、修改从库的配置文件,添加如下参数,注意server-id必须是唯一的,不能和主库相同,多个从库的话,server-id不能有重复。
cat /etc/my.cnf
[mysqld]
server-id=2
8、在从库上,使用--skip-slave-start启动数据库,这样不会立即启动从库上的复制进程,方便我们进行下一步配置。
./bin/mysqld_safe --skip-slave-start &
9、对从库进行配置,指定复制使用的用户,主库的IP、端口以及开始执行复制的日志文件和位置等:
change master to
master_host='10.10.10.200',
master_port=3306,
master_user='real',
master_password='mysql',
master_log_file='mysql-bin.000006',
master_log_pos=120;
10、在从库上启动slave线程
start slave;
11、在从库上查看slave状态
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.10.200
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 120
Relay_Log_File: mysql-relay-bin.000026
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000006
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:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 120
Relay_Log_Space: 619
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:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 4adfcd1d-4059-11e7-9532-080027d597f9
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
12、在主库进行DDL或者DML测试,在从库查看数据同步情况
数据
配置
服务器
用户
服务
文件
日志
相同
一致
一致性
主机
二进制
位置
参数
备份
多个
快照
情况
拷贝
数据库
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
红旗服务器操作系统
安徽天驱企业级服务器云空间
国家网络安全宣传网启动仪式
网站下载服务器配置
上海市大学生网络安全教育
第二届网络安全精英赛山西赛区
网络安全不能做什么
服务器 空间
河南爱聊网络技术有限公司
河南天创软件开发
c http服务器
阿里云服务器文档管理
版权数据库
更新巫师3无法连接到内容服务器
王者游戏服务器网址
网络安全现在面临的危险
软件开发平台有中文版吗
保定市的软件开发公司电话
路由器能否设置成时间服务器
安全狗服务器手机版下载安装
bmc端口界面进入服务器
银监 107号 网络安全
国家网络安全防线
网络安全450字读后感
网络安全方面人才的需要
泛谈网络安全
伯德虎扑数据库
sql计算数据库行数语句
系统建设 网络安全
c服务器开发