当前位置: 首页 > 科技观察

通过Atlas实现MySQL读写分离

时间:2023-03-22 00:16:05 科技观察

【引自Crazy_Coder的博客】前期准备准备4台机器,系统为CentOSrelease6.6ip分别为192.168.20.121、192.168.20.122、192.168.20.123、192.168.20.1244台机器服务分别作为Atlas代理服务,masterMySQL,slaveMySQL1,slaveMySQL2下载QiHoo360的Atlas地址并安装Atlas1.下载Atlas-XX.el6.x86_64.rpm安装文件2.sudorpm–iAtlas-XX.el6.x86_64.rpm安装3.安装在/usr/local/mysql-proxy4.安装目录分析bin可执行文件encrypt用于加密密码,后面会用到mysql-proxy,这是MySQL自带的读写分离代理mysql-proxyd运行AtlasVERSIONconftest.cnf配置文件一个文件就是一个实例,实例名就是文件名,需要带上这个实例名lib依赖包log记录日志5.启动命令:/usr/local/mysql-proxy/bin/mysql-proxyd[instancename]start6.停止命令:/usr/local/mysql-proxy/bin/mysql-proxyd[instancename]stop7.同理,restart表示重启,status表示查看状态8.Configuration文件解释请参考官方文档数据库配置1.1master和2slave必须配置相同的用户名和密码,并且都必须可以远程访问2.分别进入3台服务器,创建相同的用户名和密码,创建数据库test。设置权限CREATEUSER'test'@'%'IDENTIFIEDBY'test123';CREATEUSER'test'@'localhost'IDENTIFIEDBY'test123';grantallprivilegesontest.*to'test'@'%'identifiedby'test123';grantallprivilegesontest.*to'test'@'localhost'identifiedby'test123';flushprivileges;3.主从数据库配置1.配置主服务器找到MySQL配置文件my.cnf,一般修改etc目录下的配置文件[mysqld]#其他一些配置...#主从复制配置innodb_flush_log_at_trx_commit=1sync_binlog=1#要备份的数据库binlog-do-db=test#不要备份的数据库binlog-ignore-db=mysql#启动二进制文件log-bin=mysql-bin#ServerIDserver-id=1#Disablingsymbolic-linkisrecommendedtopreventassortedsecurityriskssymbolic-links=0重启数据库服务mysqlrestart进入数据库,配置主从复制权限mysql-uroot-p123456grantreplicationslaveon*.*to'test'@'127.0.0.1'identifiedby'test123';查看主库信息,记住下面的File和Position信息,这是配置从库的关键信息mysql>showmasterstatus;+----------------+---------+------------+-----------------+|文件|位置|Binlog_Do_DB|Binlog_Ignore_DB|+----------------+----------+----------------+----------------+|mysql-bin.000002|17620976|测试|mysql|+----------------+----------+-------------+-------------------+1rowinset(0.00sec)2.配置两台salve服务器,找到配置文件my.cnf,修改配置文件如下[mysqld]#Someotherconfigurations...#几个服务器不能是同一个server-id=2#Disablingsymbolic-linksisrecommendedtopreventassortedsecurityriskssymbolic-links=0进入数据库,从数据库配置信息,这里输入刚才记录的File和Position信息,在上面执行从服务器(执行时,#lines不要copy进去):#master数据库的ipmysql>changemastertomaster_host='192.168.20.122',#master的用户名master_user='buck',#passwordmaster_password='hello',#portmaster_port=3306,#master数据库的`File`master_log_file='mysql-bin.000002',master数据库的`Position`master_log_pos=17620976,master_connect_retry=10;启动进程mysql>startslave;QueryOK,0rowsaffected(0.00sec)查看主从复制状态,看到下面的Slave_IO_Running,Slave_SQL_Running信息,这两个都是Yes,说明主从连接正确,如如果一个是No,需要重新确认刚刚记录的日志信息,停止“stopslave”,重新配置主从连接mysql>showslavestatus\G;******************************1.row****************************Slave_IO_State:WaitingformastertosendeventMaster_Host:192.168.246.134Master_User:buckMaster_Port:3306Connect_Retry:10Master_Log_File:mysql-bin.000002Read_Master_Log_Pos:17620976Relay_Log_File:mysqld-relay-bin.000002Relay_Log_Pos:251Relay_Master_Log_File:mysql-bin.000002Slave_IO_Running:YesSlave_SQL_Running:YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno:0Last_Error:Skip_Counter:0Exec_Master_Log_Pos:17620976Relay_Log_Space:407Until_Condition:NoneUntil_Log_File:Until_Log_Pos:0Master_SSL_Allowed:NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master:0Master_SSL_Verify_Server_Cert:NoLast_IO_Errno:0Last_IO_Error:Last_SQL_Errno:0Last_SQL_Error:1rowinset(0.00sec)ERROR:NoqueryspecifiedAtlas配置1。使用Atlas的加密工具对上面用户的密码进行加密/usr/local/mysql-proxy/bin/encrypttest12329uENYYsKLo=2。Configureatlas这是管理员登录Atlas的账号和密码,对应Atlas监控的管理界面的IP和端口,也就是说只需要设置管理员登录的端口中可以进入管理员界面。默认端口为2345,也可以指定IP登录,指定IP后其他IP无法访问管理员命令界面进行测试。这里我没有指定登录的IP和端口。配置主数据地址和从数据库地址。这里配置的主库是122,从库是123,主库的IP和端口是124,可以设置多项,用逗号分隔。proxy-backend-addresses=192.168.20.122:3306#连接Atlas后端的MySQL从库的IP和端口。@后面的数字代表权重,用于负载均衡。如果省略,默认为1。可以设置多项,proxy-read-only-backend用逗号分隔-addresses=192.168.20.123:3306[@1](https://my.oschina.net/u/1198),192.168.20.124:3306@这个是用来配置mysql账号和密码的,上面创建的User,用户名为test,密码为test123,对应的加密密码pwds=buck:RePBqJ+5gI4=3是刚刚使用Atlas提供的工具生成的。启动Atlas[root[@localhost](https://my.oschina.net/u/570656)/usr/local/mysql-proxy/bin]#./mysql-proxydteststartOK:MySQL-Proxyoftestisstartedtest1.进入管理atlas界面[root[@localhost](https://my.oschina.net/u/570656)~]#mysql-h127.0.0.1-P2345-uuser-ppwd欢迎使用MySQLmonitor.Commandsendwith;or\g.YourMySQLconnectionidis1Server版本:5.0.99-agent-adminCopyright(c)2000,2013,Oracleand/oritsaffiliates.Allrightsreserved.Oracle是OracleCorporation和/或其附属公司的注册商标。其他名称可能是其各自所有者的商标soft。输入'帮助;inputstatement.mysql>select*fromhelp;+----------------------------+------------------------------------------------------+|命令|描述|+----------------------------+------------------------------------------------------+|SELECT*FROMhelp|showthishelp||SELECT*FROMbackends|列出后端及其状态||SETOFFLINE$backend_id|offlinebackendserver,$backend_idisbackend_ndx'sid||SETONLINE$backend_id|onlinebackendserver,...||ADDMASTER$backend|example:"addmaster127.0.0.1:3306",...||ADDSLAVE$后端|示例:“addslave127.0.0.1:3306”,...||REMOVEBACKEND$backend_id|示例:“removebackend1”,...||SELECT*FROMclients|liststheclients||ADDCLIENT$client|示例:“addclient192。168.1.2",...||REMOVECLIENT$client|示例:"removeclient192.168.1.2",...||SELECT*FROMpwds|liststhepwds||ADDPWD$pwd|示例:"addpwduser:raw_password",...||ADDENPWD$pwd|示例:“addenpwduser:encrypted_pa??ssword”。..||REMOVEPWD$pwd|示例:“removepwduser”,...||SAVECONFIG|将后端保存到配置文件||SELECTVERSION|显示图集版本|+--------------------------+------------------------------------------------------+16rowsinset(0.00sec)mysql>2.使用工作界面访问[root@localhost~]#mysql-h127.0.0.1-P1234-utest-ptest123欢迎使用MySQLmonitor.Commandsendwith;or\g.YourMySQLconnectionidis34Serverversion:5.0.81-logMySQLCommunityServer(GPL)Copyright(c)2000,2013,Oracleand/oritsaffiliates.Allrightsreserved.OracleisregisteredtrademarkofOracleCorporationand/oritsaffiliates.Othernamesmaybetrademarksoftheirhelp\Type''\c'toclearthecurrentinputstatement.mysql>showdatabases;+----------------+|数据库|+----------------------+|information_schema||mysql||performance_schema||test|+------------------+4rowsinset(0.00sec)mysql>使用可视化管理工具Navicat登录使用用户名test,密码test123,端口1234,地址192.168.20.121正常登录注意这里登录的是atlasserver,不是任何mysqlserver