您好,登录后才能下订单哦!
下文主要给大家带来使用Altas软件如何实现mysql主从复制读写分离,希望使用Altas软件如何实现mysql主从复制读写分离能够带给大家实际用处,这也是我编辑这篇文章的主要目的。好了,废话不多说,大家直接看下文吧。
mysql读写分离原理:
数据库层在高并发的情况下,i/o会产生瓶颈。而实际上用户读的请求要远远大于写的请求。
使用代理服务作为数据库前端,将不同的请求根据规则分配到不同的后端数据上面去,比如将写的请求分配给master数据库,将读的请求分配给slave数据库。master和slave可以是一个或多个做成负载均衡。master数据库再通过主从复制同步数据给slave.
环境介绍:
HostName | OS | IP | 作用 |
master | centos6.5 | 192.168.100.150 | 担任mysql主云服务器 |
salve | centos6.5 | 192.168.100.151 | 担任mysql从云服务器 |
Altas | centos6.5 | 192.168.100.152 | 担任mysql代理 |
ftp | centos6.5 | 192.168.100.100 | 担任ftp为主从提供yum源,软件支持(可以使用公网yum源代替此主机) |
1:主从安装mysql:
[root@master ~]# yum -y install mysql-server [root@slave ~]# yum -y install msyql-server
2:修改主从的配置文件,以支持bin_log日志记录
[root@master ~]# vi /etc/my.cnf 7 log-bin=mysql-bin ##支持bin-log日志记录,bin-log日志文件名以mysql-bin开头 8 server-id=150 ##服务的唯一标识符号,默认是1,这里方便记忆,我使用了ip最后一段 [root@slave ~]# vi /etc/my.cnf 7 server-id=151 [root@master ~]# /etc/init.d/mysqld start ##重启服务 [root@slave ~]# /etc/init.d/mysqld start
3:在主数据库上面授权给从复制的权限:
登陆云服务器授权
[root@master ~]# mysqladmin -uroot password 123123 [root@master ~]# mysql -uroot -p123123 mysql> grant replication slave on *.* to 'slave'@"192.168.100.%" identified by '123123'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; ##刷新权限 Query OK, 0 rows affected (0.00 sec) mysql>
查看主服务的bin-log日志文件信息:
需要记录file 和position两栏中内容:以查到的为准。
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 476 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
4:在从云服务器上修改自己的master的数据库
登入数据库
[root@slave ~]# mysqladmin -uroot password 123123 [root@slave ~]# mysql -uroot -p123123
设置从云服务器读取master bin-log的相关信息
mysql> change master to -> master_host='192.168.100.150', ##master的ip -> master_user='slave', ##授权允许复制的用户名 -> master_password='123123', ##授权允许复制密码 -> master_log_file='mysql-bin.000003', ##bin-log文件名,上一步在master上查到的信息 -> master_log_pos=476; ##偏移量,在master上查到的信息 Query OK, 0 rows affected (0.07 sec)
启动slave
mysql> start slave; Query OK, 0 rows affected (0.00 sec)
插卡slave状态:
##查到的状态这两个为yes,下面没有error错误就正常 Slave_IO_Running: Yes Slave_SQL_Running: Yes
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.100.150 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 706 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 481 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 706 Relay_Log_Space: 637 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: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 1 row in set (0.00 sec) ERROR: No query specified mysql>
5:测试:
在主数据库上新建库,查看库
mysql> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> create database test_databases; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | | test_databases | +--------------------+ 4 rows in set (0.00 sec)
在从数据库上查看库:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | | test_databases | +--------------------+ 4 rows in set (0.00 sec)
在master端授权:
mysql> grant all on *.* to root@"192.168.100.%" identified by '123123'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql>
在Atlas云服务器下载安装软件
[root@Atlas ~]# wget -O ./altas https://github.com/Qihoo360/Atlas/releases/download/sharding-1.0.1/Atlas-sharding_1.0.1-el6.x86_64.rpm
[root@Atlas ~]# ls altas anaconda-ks.cfg install.log install.log.syslog [root@Atlas ~]# file altas altas: RPM v3.0 bin i386/x86_64 Atlas-sharding_1.0.1-el6 [root@Atlas ~]# rpm -ivh altas Preparing... ########################################### [100%] 1:Atlas ########################################### [100%]
修改配置文件:
[root@Atlas ~]# vim /usr/local/mysql-proxy/conf/test.cnf
需要更改的地方:
proxy-backend-addresses = 192.168.100.150:3306
proxy-read-only-backend-addresses = 192.168.100.151:3306
pwds = root:++gAN07C/Q0= ##这里用/usr/local/mysql-proxy/bin/encrypt 加上数据库授权的密码,生成密文密码填写在这里
生成密文密码
[root@Atlas bin]# pwd /usr/local/mysql-proxy/bin [root@Atlas bin]# ./encrypt 123123 ++gAN07C/Q0=
daemon = true
sql-log = REALTIME
charset = utf8
全部的配置项,以及注释
#管理接口的密码 admin-password = pwd #Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔 proxy-backend-addresses = 192.168.100.150:3306 #Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔 #proxy-read-only-backend-addresses = 127.0.0.1:3305@1 proxy-read-only-backend-addresses = 192.168.100.151:3306 #用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码! pwds = root:++gAN07C/Q0= #设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。 daemon = true keepalive = true #工作线程数,对Atlas的性能有很大影响,可根据情况适当设置 event-threads = 8 #日志级别,分为message、warning、critical、error、debug五个级别 #带#号的为非必需的配置项目 #管理接口的用户名 admin-username = user #管理接口的密码 admin-password = pwd #Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔 proxy-backend-addresses = 192.168.100.150:3306 #Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔 #proxy-read-only-backend-addresses = 127.0.0.1:3305@1 proxy-read-only-backend-addresses = 192.168.100.151:3306 #用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码! pwds = root:++gAN07C/Q0= #设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。 daemon = true keepalive = true #工作线程数,对Atlas的性能有很大影响,可根据情况适当设置 event-threads = 8 #日志级别,分为message、warning、critical、error、debug五个级别 log-level = message #日志存放的路径 log-path = /usr/local/mysql-proxy/log #SQL日志的开关,可设置为OFF、ON、REALTIME,OFF代表不记录SQL日志,ON代表记录SQL日志,REALTIME代表记录SQL日志且实时写入磁盘,默认为OFF sql-log = REALTIME #慢日志输出设置。当设置了该参数时,则日志只输出执行时间超过sql-log-slow(单位:ms)的日志记录。不设置该参数则输出全部日志。 #sql-log-slow = 10 #实例名称,用于同一台机器上多个Atlas实例间的区分 #instance = test #Atlas监听的工作接口IP和端口 proxy-address = 0.0.0.0:1234 #Atlas监听的管理接口IP和端口 admin-address = 0.0.0.0:2345 #分表设置,此例中person为库名,mt为表名,id为分表字段,3为子表数量,可设置多项,以逗号分隔,若不分表则不需要设置该项 #tables = person.mt.id.3 #默认字符集,设置该项后客户端不再需要执行SET NAMES语句 charset = utf8 #允许连接Atlas的客户端的IP,可以是精确IP,也可以是IP段,以逗号分隔,若不设置该项则允许所有IP连接,否则只允许列表中的IP连接 #client-ips = 127.0.0.1, 192.168.1 #Atlas前面挂接的LVS的物理网卡的IP(注意不是虚IP),若有LVS且设置了client-ips则此项必须设置,否则可以不设置 #lvs-ips = 192.168.1.1
启动关闭代理服务:
[root@Atlas bin]# ls encrypt mysql-binlog-dump mysql-myisam-dump mysql-proxy mysql-proxyd VERSION [root@Atlas bin]# ./mysql-proxyd test start OK: MySQL-Proxy of test is started [root@Atlas bin]# ./mysql-proxyd test stop OK: MySQL-Proxy of test is stopped [root@Atlas bin]# ./mysql-proxyd test start OK: MySQL-Proxy of test is started [root@Atlas bin]# ./mysql-proxyd test restart OK: MySQL-Proxy of test is stopped OK: MySQL-Proxy of test is started
查看进程:
[root@Atlas ~]# ps aux |grep mysql-proxy root 1266 0.0 0.2 67156 1452 ? S 19:24 0:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf root 1267 0.0 0.6 161460 3352 ? Sl 19:24 0:01 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf root 16756 0.0 0.1 103248 876 pts/0 S+ 20:55 0:00 grep mysql-proxy
安装mysql,只安装客户端。
[root@Atlas ~]# yum -y install mysql [root@Atlas ~]# mysql -uroot -p123123 -h 192.168.100.152 -P1234 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> quit Bye
查看日志信息:
[root@Atlas ~]# tail -f /usr/local/mysql-proxy/log/test.log
2017-08-22 19:24:32: (message) proxy listening on port 0.0.0.0:1234
2017-08-22 19:24:32: (message) added read/write backend: 192.168.100.150:3306
2017-08-22 19:24:32: (message) added read-only backend: 192.168.100.151:3306
2017-08-22 19:24:32: (message) chassis-event-thread.c:235: starting 8 threads
2017-08-22 19:24:34: (message) chassis-unix-daemon.c:138: [angel] we try to keep PID=1267 alive
2017-08-22 19:24:34: (message) mysql-proxy 0.8.2 started - instance: test
2017-08-22 19:24:34: (message) proxy listening on port 0.0.0.0:1234
2017-08-22 19:24:34: (message) added read/write backend: 192.168.100.150:3306
2017-08-22 19:24:34: (message) added read-only backend: 192.168.100.151:3306
2017-08-22 19:24:34: (message) chassis-event-thread.c:235: starting 8 threads
##可以看到读的操作都交给slave数据库了,master可以读写操作,一般是只写。
登录到管理端口:(可以对后端的mysql数据库进行管理)
[root@Atlas ~]# mysql -uuser -ppwd -h292.168.100.152 -P2345 mysql> select * from help; ##查看管理帮助 +---------------------------------------+---------------------------------------------------------+ | command | description | +---------------------------------------+---------------------------------------------------------+ | SELECT * FROM help | shows this help | | SELECT * FROM backends | lists the backends and their state | | SET OFFLINE $backend_id | offline backend server, $backend_id is backend_ndx's id | | SET ONLINE $backend_id | online backend server, ... | | ADD MASTER $backend | example: "add master 127.0.0.1:3306", ... | | ADD SLAVE $backend | example: "add slave 127.0.0.1:3306", ... | | ADD GMASTER $group_id $backend | example: "add gmaster 1 127.0.0.1:3306", ... | | ADD GSLAVE $group_id $backend | example: "add gslave 1 127.0.0.1:3306", ... | | REMOVE BACKEND $backend_id | example: "remove backend 1", ... | | REMOVE GBACKEND $group_id $backend_id | example: "remove gbackend 1 1", ... | | SELECT * FROM clients | lists the clients | | ADD CLIENT $client | example: "add client 192.168.1.2", ... | | REMOVE CLIENT $client | example: "remove client 192.168.1.2", ... | | SELECT * FROM pwds | lists the pwds | | ADD PWD $pwd | example: "add pwd user:raw_password", ... | | ADD ENPWD $pwd | example: "add enpwd user:encrypted_password", ... | | REMOVE PWD $pwd | example: "remove pwd user", ... | | SAVE CONFIG | save the backends to config file | | SELECT VERSION | display the version of Atlas | +---------------------------------------+---------------------------------------------------------+ 19 rows in set (0.00 sec) mysql> select * from backends; ##查看后端mysql状态,工作类型 +----------+----------------------+-------+------+-------------+ | group_id | address | state | type | backend_ndx | +----------+----------------------+-------+------+-------------+ | -1 | 192.168.100.150:3306 | up | rw | 1 | | -1 | 192.168.100.151:3306 | up | ro | 2 | +----------+----------------------+-------+------+-------------+ 2 rows in set (0.00 sec)
对于以上关于使用Altas软件如何实现mysql主从复制读写分离,大家是不是觉得非常有帮助。如果需要了解更多内容,请继续关注我们的行业资讯,相信你会喜欢上这些内容的。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。