mysql主主复制
1. 修改my.cnf:
主机A
[mysqld]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #标识唯一id(必须) server-id=1 #开启二进制日志 log-bin=mysql-bin #不同步的数据库,可设置多个 binlog-ignore-db=information_schema binlog-ignore-db=performance_schema binlog-ignore-db=mysql binlog-ignore-db=sys #指定需要同步的数据库(和slave是相互匹配的),可以设置多个 #binlog-do-db=test #主-主形式需要多添加的部分 log-slave-updates #sync_binlog = 1 auto_increment_offset = 1 auto_increment_increment = 2 replicate-ignore-db = mysql,information_schema,performance_schema,sys #设置存储模式不设置默认 binlog_format=MIXED #日志清理时间 expire_logs_days=7 #日志大小 max_binlog_size=100m #缓存大小 binlog_cache_size=4m #最大缓存大小 max_binlog_cache_size=521m |
主机B
[mysqld]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #标识唯一id(必须) server-id=2 #开启二进制日志 log-bin=mysql-bin #不同步的数据库,可设置多个 binlog-ignore-db=information_schema binlog-ignore-db=performance_schema binlog-ignore-db=mysql binlog-ignore-db=sys #指定需要同步的数据库(和slave是相互匹配的),可以设置多个 #binlog-do-db=test #主-主形式需要多添加的部分 log-slave-updates #sync_binlog = 1 auto_increment_offset = 2 auto_increment_increment = 2 replicate-ignore-db = mysql,information_schema,performance_schema,sys #设置存储模式不设置默认 binlog_format=MIXED #日志清理时间 expire_logs_days=7 #日志大小 max_binlog_size=100m #缓存大小 binlog_cache_size=4m #最大缓存大小 max_binlog_cache_size=521m |
2. 重启mysql,
2.1 配置MasterA主 ==》 MasterB主
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | mysql> grant replication slave on *.* to 'repl' @ '192.199.162.15' identified by '123456' ; mysql> flush privileges; #刷新权限 mysql> SHOW MASTER STATUS; +------------------+----------+--------------+-------------------------------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+-------------------------------------------------+-------------------+ | mysql-bin.000007 | 613 | | information_schema,performance_schema,mysql,sys | | +------------------+----------+--------------+-------------------------------------------------+-------------------+ #File、Position 在配置从机时需要用到 #MasterB 从机配置 mysql> change master to master_host= '192.199.162.37' , master_user= 'repl' , master_port=3307, master_password= '123456' , master_log_file= 'mysql-bin.000007' , master_log_pos=613; #启动slave同步进程: mysql> start slave; #查看slave状态: mysql> show slave status\G; |
2.2配置masterB主 ==》 masterA从
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | mysql> grant replication slave on *.* to 'repl' @ '192.199.162.37' identified by '123456' ; mysql> flush privileges; #刷新权限 mysql> SHOW MASTER STATUS; +------------------+----------+--------------+-------------------------------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+-------------------------------------------------+-------------------+ | mysql-bin.000004 | 613 | | information_schema,performance_schema,mysql,sys | | +------------------+----------+--------------+-------------------------------------------------+-------------------+ #File、Position 在配置从机时需要用到 #MasterA从机配置 mysql> change master to master_host= '192.199.162.15' , master_user= 'repl' , master_password= '123456' , master_log_file= 'mysql-bin.000004' , master_log_pos=613; #启动slave同步进程: mysql> start slave; #查看slave状态: mysql> show slave status\G; |
3. 配置成功,查看slave状态:
1 2 | #当Slave_IO_Running和Slave_SQL_Running都为YES的时候就表示主从同步设置成功了 mysql> show slave status\G; |
4. 正确关闭mysql以及slave服务步骤
1 2 3 4 5 6 7 8 9 10 | 关闭MySQL从库 1. 先查看当前的主从同步状态show slave status\G;看是否双yes 2. 分别执行stop slave,停止从库线程 3. 停止从库服务mysqladmin shutdown -u用户名 -p密码 4. 查看是否还有mysql的进程ps -ef | grep mysql 5. 如果部署了多个实例,那每个实例都要按照以上步骤来操作 关闭MySQL主库 1. 停止主库服务mysqladmin shutdown -u用户名 -p密码 2. 查看是否还有mysql的进程ps -ef | grep mysql |
5. 正确启动主从服务步骤
1 2 3 4 5 6 7 8 9 | 启动MySQL主库 1. 启动主库服务mysqladmin start -u用户名 -p密码 2. 查看mysql的进程ps -ef | grep mysql 启动MySQL从库 1. 启动从库服务mysqladmin start -u用户名 -p密码 2. 分别启动复制start slave; 3. 检查同步状态show slave status\G;是否双yes 4. 查看mysql的进程ps -ef | grep mysql |
Q&A:
1. Slave_SQL_Running: No
1.1 先停掉两台的slave
1.2 登录masterA查看master
show master status;
1.3 登录masterB
stop slave;
change master to master_host='127.0.0.1',
master_port=3308,
master_user='repl',
master_password='123456',
master_log_file='mysql-bin.000003',
master_log_pos=154;
start slave;
show slave status\G
确认双Yes
1.4 登录masterA,执行1.3步骤
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通