mysq5.6主从同步配置(无坑)

tech2022-10-26  112

网上的 一堆博客,东拼西凑 总算是跑通了 。基本上都是很多坑。 以下为个人总结流程:

环境:VMware centos7 dvd版

配置文件:/etc/my.conf 日志文件:log-error=/var/log/mysqld.log (随时查看,错误信息记录在这)

关于防火墙操作:

(1)设置开机启用防火墙:systemctl enable firewalld.service (2)设置开机禁用防火墙:systemctl disable firewalld.service (3)启动防火墙:systemctl start firewalld (4)关闭防火墙:systemctl stop firewalld (5)检查防火墙状态:systemctl status firewalld

关闭SELinux 1、临时关闭:输入命令setenforce 0,重启系统后还会开启。 2、永久关闭:输入命令vi /etc/selinux/config,将SELINUX=enforcing改为SELINUX=disabled,然后保存退出。

坑:

1.网络不通——你相互ping一下各自的ip地址,ping的通就说明不是这个问题,我是开的两台虚拟机,在同一局域网内,所以肯定通,如果你用的是阿里云啊之类的外网服务器,ping不通就检查网络接口之类的。关闭防火墙:操作如上,先关闭 再禁用开机启动(如未关闭 Slave_IO_Running: conncting)配置主mysql的用户 远程登录情况(配置完之后确保mysql -h xxx -u -p 能正常登录上)如果从机是克隆主机uuid重复 需要删除文件 rm -rf /var/lib/mysql/auto.cnf 会自动生成uuid开启同步之前,务必保证数据库数据一直,开启之后才复制所有操作错误总结:查看 错误日志文件,各种报错 网上都有解决方案。重点是能找问题点。

主mysql配置:

[mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M # log-bin=/var/lib/mysql/binlog #开启二进制记录 二进制文件,一定要保证此文件 可读写 server-id=1 #id 唯一 binlog-do-db = test # 需要同步的数据库 同时 不需要同步的配置 也有配置项 网上搜 #要同步的数据库,如果需要就填,指定数据库的名称即可,默认为所有库,声明了不同步就默认除了不同步数据库意外的所有库 # 以下是原本自带的默认配置 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock

配置完 需要重启 数据库

systemctl restart mysql # 重启数据库 如果卡主不动 那就是配置错误 查看日志文件 mysql -u xxx -p #连接mysql show master status; # 查看作为主mysql的状态

详细说明: File:当前使用的二进制文件 记下,等会用 position:当前二进制文件 的使用长度 ,从机需要从此处开始同步 记下 binlog_do_db :同步的数据库

从mysql 配置:

[mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M server-id=2 log_bin=/var/lib/mysql/mysql_bin datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock

重启 mysql

进入mysql 运行 以下命令 :

change master to master_host='主mysql的ip地址', master_user='能够远程登录主mysql的用户名', master_password='能够远程登录主mysql的密码', master_port=3306, master_log_file='主mysql正在使用的二进制文件', #当前使用的二进制文件 我的是 binlog.000005 master_log_pos=2506; #主mysql正在使用的二进制文件位置 我这里是2506 都是前面主机的show master status

回车看到ok设置正确

start slave #开启从机模式 stop slave # 关闭从机模式

Slave运行2个线程 — Slave_IO:复制master主机binlog日志文件里的SQL到本机的relay-log文件里 — Slave_SQL:执行本机relay-log文件里的SQL语句,重现Master的数据操作

如果不是 两个yes 请查看日志 报错 。 终结 测试 自行测试,

纯手写不易 如果成功 请收藏 点赞 谢谢

补充备用操作: 备份 还原数据库:

mysqldump -h 192.168.235.133 -u root -p test > test.sql # 备份133主机的test 当钱路径 mysql -uroot -p djf136169 test <test.sql # 还原到当前库 需要先创建库

远程用户授权操作:

grant all privileges on *.* to 'root'@'%'identified by 'youpassword'; %:表示所有地址均可访问 youpassword:自己设的密码

链式同步: 类似 a—b-----c b同步a c同步b 中 c是不会同步a的 ,a中的所有改变c都不会复制 这是需要添加配置 在b的 my.conf中 天剑 log-slave-updates 即可 意思就是将 作为从服务器 从别处获取到的改变 也写入binlog文件 c便可同步a

下章 将介绍 双机互备 ,多主多从配置 全流程!!

最新回复(0)