mysql 主从配置

一.主(master)服务器配置

server_id=1 #指定MySQL的id
log-bin=mysql-bin #开启二进制日志文件、
binlog_format =ROW

二.创建复制账号

GRANT REPLICATION SLAVE ON . to xxx@‘%’ identified by ‘密码’;
如果出现
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option

flush privileges; 先执行这个
GRANT REPLICATION SLAVE ON . to xxx@‘%’ identified by ‘密码’; 再执行

账户xxx
密码xxxx

三.修改从(slave)服务器
server_id=2
log-bin=mysql-bin
binlog_do_db=db_test #表示要同步的数据库

四、重启MySQL
Windows的就服务里重启
Linux service mysqld restart
五、查看主服务器状态
show master status
在这里插入图片描述

File 主服务器 binlog 文件名
Postiton 主服务器 binlog 文件中的 Postion 值
六、配置从服务器(连接 Master 服务器)
#登录从服务
mysql -u用户 -p密码;
#设置同步主节点:
CHANGE MASTER TO
MASTER_HOST=‘ip’,
MASTER_PORT=3306,
MASTER_USER=‘xxx’,
MASTER_PASSWORD=‘密码’,
MASTER_LOG_FILE=‘mysql-bin.000015’,
MASTER_LOG_POS=172281545
GET_MASTER_PUBLIC_KEY=1;
#master_host是主服务器的ip
#master_port=3306(这里没有配置,默认3306)
#master_user:Master 服务器授权用户,也就是 Master 前面创建的那个用户
#master_password:Master 服务器授权用户对应的密码
#master_log_file:Master binlog 文件名
#master_log_pos:Master binlog 文件中的 Postion 值
七、开启slave
start slave;
八、查看主从同步状态
show slave status;
或者用 show slave status \G; 这样查看比较简洁
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.2.40
Master_User: xxx
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.0000015
Read_Master_Log_Pos: 172281129
Relay_Log_File: mysqld-relay-bin.000008
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.0000015
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table: photo.%
Replicate_Wild_Ignore_Table: mysql.%
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 13456620
Relay_Log_Space: 36764898503
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: 249904

Slave_IO_Running: Yes
Slave_SQL_Running: Yes
代表配置成功
九、验证主从结果
现在的状态:

下面新建一个表,看看是否可以同步过去:

十、查看从服务器

可以看到已经同步过去了同步过去

十一、Slave_SQL_Running=no
就表示主从同步失败了。
这有可能是因为在从数据库上进行了写操作,与同步过来的SQL操作冲突了,也
有可能是slave从服务重启后有事务回滚了。
如果是因为slave从服务事务回滚的原因,可以按照以下方式重启主从同
步:
而另一种解决方式就是重新记录主节点的binlog文件消息
mysql> stop slave ;
mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
mysql> start slave ;

而另一种解决方式就是重新记录主节点的binlog文件消息
mysql> stop slave ;
mysql> #设置同步主节点:
change master to master_host=‘主服务器的ip’,master_user=‘服务器授权用户’,master_password=‘密码’,master_log_file=‘mysql-bin.000005’,master_log_pos=154;
mysql> start slave;

2种方式恢复同步

一种 从库数据库未开启 重启能恢复同步
一种 从库连不上,连上能恢复同步
如果修改从库跟主库的操作有冲突的,比如唯一索引的数据 无法解决

十二、Slave_IO_Running=no
就表示主从io线程没连接
1.可能网络问题
2.Server_id相同 修改主从任何一个server_id就行,windows的my.ini文件里修改
Linux的my.cnf文件里修改
3.Server_uuid相同 修改主从任何一个server_uuid就行,windows的anto.cnf文件里修改

Linux的anto.cnf文件里修改

mysqlbinlog --no-defaults mysql-bin.000010 --start-datetime="2021-08-23 00:00:00” --stop-datetime=“2021-08-24 09:43:00”> /usr/local/mysql/beifen/lizengliangl.sql

mysqlbinlog binlog.000007 | mysql -uroot -p密码

flush privileges;
GRANT REPLICATION SLAVE ON . to ‘xxx’@‘%’ identified by ‘密码’;

change master to master_host=‘192.168.74.129’,master_user=‘replication’,master_password=‘Abc123…’,master_log_file=‘mysql-bin.000001’,master_log_pos=2041;

Mysql 主从 数据备份参考地址
https://blog.csdn.net/weixin_43184774/article/details/109830081
https://www.2cto.com/database/201407/314927.html
https://blog.csdn.net/u011374582/article/details/84062418
https://www.cnblogs.com/happy4java/p/11206023.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值