详解MySQL主从复制读写分离搭建
作者:RyuGou 时间:2024-01-21 02:58:35
MySQL主从设置
MySQL主从复制,读写分离的设置非常简单:
修改配置my.cnf文件
master 和 slave设置的差不多:
[mysqld]
log-bin=mysql-bin
server-id=222
log-bin=mysql-bin
的意思是:启用二进制日志。
server-id=222
的意思是设置了服务器的唯一ID,默认是1,一般取IP最后一段,可以写成别的,只要不和其他mysql服务器重复就好。
这里,有的MySQL默认的my.cnf
文件引用了/etc/mysql/conf.d
路径下的所有cnf文件,因此,也可以在/etc/mysql/conf.d
目录下添加一个cnf文件,添加上以上内容
分别重启master和slave机器的mysql
/etc/init.d/mysql restart
master上建立账户并授权slave,slave上也做好相关配置
用root用户分别登录master和slave机器:
mysql -uroot -p //输入之后会让你输入root密码,输入root密码就算登陆了
master机器上
建立账户并授权slave机器,注意用户名和密码,可以随意设置,但是一定要记下来,因为后面slave还需要用到
GRANT REPLICATION SLAVE ON *.* to 'ryugou'@'%' identified by 'ryugou';
一般不用root帐号,@'%'表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.145.226,加强安全。
slave机器上
查看master状态
show master status;
在mysql终端输入
change master to master_host='xxx.xxx.xxx.xxx',master_user='ryugou',master_password='ryugou',master_log_file='mysql-bin.000004',master_log_pos=120;
master_log_file就是master机器上的对应的内容,master_log_pos也是,注意查看master状态上的内容。
master_user和master_password就是master机器上授权时设置用户名和密码。
启动从服务器
start slave;
检查从服务器复制功能状态
show slave status\G;
会出现一个列表
主要看Slave_IO_Running和Slave_SQL_Running 这俩状态必须都为Yes。
master创建数据库,从服务器设置用户访问权限
在master上创建数据库 test,会发现在slave上也创建了 test,现在想要读写分离,slave上的机器某用户gou可以访问它,但是只能读
grant select on test.* to gou;
来源:https://i6448038.github.io/2017/06/27/MySQL%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E6%90%AD%E5%BB%BA/?utm_source=tuicool&utm_medium=referral
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解MySQL 数据库优化方法
python basemap 画出经纬度并标定的实例
网易网站设计(思想)
![](https://img.aspxhome.com/file/UploadPic/20093/27/01-38s.jpg)
Python常见数字运算操作实例小结
Oracle数据库安全策略分析(一)
Python3利用Qt5实现简易的五子棋游戏
最新IntelliJ IDEA 2020.2永久激活码(亲测有效)
![](https://img.aspxhome.com/file/2023/1/107161_0s.jpg)
Python基于pygame实现图片代替鼠标移动效果
![](https://img.aspxhome.com/file/2023/9/109329_0s.png)
基于Python模拟浏览器发送http请求
PHP中的一些常用函数收集
Python自动化测试利器selenium详解
![](https://img.aspxhome.com/file/2023/1/112951_0s.png)
PHP实现的微信公众号扫码模拟登录功能示例
nacos只支持mysql的原因分析
页面重构中的组件制作要点
![](https://img.aspxhome.com/file/UploadPic/200910/25/medium-14s.png)
python 生成图形验证码的方法示例
![](https://img.aspxhome.com/file/2023/0/66930_0s.png)
ASP利用 xmlhttp 分块上传文件
js贪吃蛇游戏实现思路和源码
![](https://img.aspxhome.com/file/2023/8/136798_0s.jpg)
初学Python函数的笔记整理
python读取文本中数据并转化为DataFrame的实例
![](https://img.aspxhome.com/file/2023/5/77785_0s.jpg)