MySQL + Atlas --- 部署读写分离
2016-11-14 21:42
363 查看
转载:http://www.cnblogs.com/yyhh/archive/2015/12/29/5084844.htmlAtlas是360团队弄出来的一套基于MySQL-Proxy基础之上的代理,修改了MySQL-Proxy的一些BUG,并且优化了很多东西。而且安装方便。配置的注释写的蛮详细的,都是中文。英文不好的同学有福了。Atlas官方链接: https://github.com/Qihoo360/Atlas/blob/master/README_ZH.mdAtlas下载链接: https://github.com/Qihoo360/Atlas/releases 环境:
系统 | IP | 配置 |
CentOS 6.7 | 192.168.246.143 | Atlas代理服务 |
CentOS 6.7 | 192.168.246.134 | 主MySQL数据库 |
CentOS 6.7 | 192.168.246.135 | 从MySQL数据库 |
1. 数据库的配置
需要进入134与135数据库中配置用户名与密码,用户必须是远程可以访问的用户,配置方法如下: 首先进入到134的MySQL数据库中,创建用户“buck”设置密码为“hello”下列标红的是用户与密码。mysql> grant all on *.* to buck@'127.0.0.1' identified by "hello"; Query OK, 0 rows affected (0.00 sec)修改buck的访问权限,首先得进入mysql数据库,才能修改host权限的信息
# 进入数据库 mysql> use mysql Database changed # 修改host权限为"%"mysql> update user set host = '%' where user = 'buck'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0查看一下user表,看看修改成功了没有。可以看到,buck的用户,host已经修改成百分号了。
mysql> select user, host from user;+------+-----------------------+ | user | host | +------+-----------------------+ | buck | % | | root | 127.0.0.1 | | | localhost | | root | localhost | | | localhost.localdomain | | root | localhost.localdomain | +------+-----------------------+6 rows in set (0.00 sec)更新数据库信息,如果没更新数据库的信息,修改不会立即生效,那就需要重启数据库了。这边直接更新数据库的信息,可以避免重启。
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)主从MySQL都需要创建一个数据库,我这创建的数据库是“test”,为了方便测试读写分离
mysql> create database test; Query OK, 1 row affected (0.00 sec)【主意:135数据库与134的数据库同样配置, 记得要创建同样的数据库哦】
2. 主从数据库连接
配置主从服务器需要编写MySQL的配置文件,详情配置步骤如下:主服务器 ( 192.168.246.134 ),使用vim进行配置[mysqld] datadir=/data/mysql socket=/var/lib/mysql/mysql.sock user=mysql #主从复制配置 innodb_flush_log_at_trx_commit=1 sync_binlog=1 #需要备份的数据库 binlog-do-db=test #不需要备份的数据库 binlog-ignore-db=mysql #启动二进制文件 log-bin=mysql-bin #服务器ID server-id=1 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid【主意:若没有配置binlog-do-db和binlog_ignore_db,表示备份全部数据库。】 重启mysqld服务
[root@localhost bin]# /etc/init.d/mysqld restart进入数据库,配置主从复制的权限
mysql> grant replication slave on *.* to 'buck'@'127.0.0.1' identified by 'hello'; Query OK, 0 rows affected (0.00 sec)锁定数据库
mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec)查看主数据库信息,记住下面的“File”与“Position”的信息,它们是用来配置从数据库的关键信息。可以看到下面同步的数据库的“test”数据库,主从数据库如果数据不一样,首先需要手动去同步一下数据,我在Windows环境下只用Navicat复制的数据,这里就不演示了。
mysql> show master status;+------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000002 | 17620976 | test | mysql | +------------------+----------+--------------+------------------+1 row in set (0.00 sec)从服务器 ( 192.168.246.135 ),也需要使用vim进行配置,只需要在[mysqld]下面加入server-id=2就可以,全部配置如下:
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql server-id=2# Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0[mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid进入数据库,配置从数据库的信息,这里输入刚才记录下来的“File”与“Position”的信息,并且在从服务器上执行:
mysql> change master to master_host='192.168.246.134' | 主服务器的IP |
master_user='buck' | 配置主服务器的用户名 |
master_password='hello' | 对应用户的密码 |
master_port=3306 | 主服务器的mysql端口 |
master_log_file='mysql-bin.000002' | 日志文件的名称,需要与主服务器对应 |
master_log_pos=17620976 | 日志位置,需要与主服务器对应 |
master_connect_retry=10 | 重连次数 |
mysql> change master to master_host='192.168.246.134', -> master_user='buck', -> master_password='hello', -> master_port=3306, -> master_log_file='mysql-bin.000002', -> master_log_pos=17620976, -> master_connect_retry=10; Query OK, 0 rows affected (0.01 sec)启动进程
mysql> start slave; Query OK, 0 rows affected (0.00 sec)检查主从复制状态,要看到下列标红的信息中,两个都是Yes,才说明主从连接正确,如果有一个是No,需要重新确定刚才记录的日志信息,停掉“stop slave”重新进行配置主从连接。
mysql> show slave status \G;*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.246.134 Master_User: buck Master_Port: 3306 Connect_Retry: 10 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 17620976 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000002 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: 17620976 Relay_Log_Space: 407 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: 0Master_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
3. Atlas配置
下载Atlas会有两个版本,其中有个分表的版本,但是这个需要其他的依赖,我这边不需要分表这种需求,所以安装普通的版本 Atlas (普通) : Atlas-2.2.1.el6.x86_64.rpm Atlas (分表) : Atlas-sharding_1.0.1-el6.x86_64.rpm 首先进入Linux的Home目录下,下载非分表的安装包[root@localhost ~]# cd /home/[root@localhost home]# wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm[/code] 下载好了之后,进行安装[root@localhost home]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm Preparing... ########################################### [100%] 1:Atlas ########################################### [100%]安装好了,它会默认在”/usr/local/mysql-proxy”下给你生成4个文件夹,以及需要配置的文件,如下:[root@localhost home]# ll /usr/local/mysql-proxy/total 16drwxr-xr-x. 2 root root 4096 Dec 28 10:47 bindrwxr-xr-x. 2 root root 4096 Dec 28 10:47 confdrwxr-xr-x. 3 root root 4096 Dec 28 10:47 libdrwxr-xr-x. 2 root root 4096 Dec 17 2014 logbin目录下放的都是可执行文件1. “encrypt”是用来生成MySQL密码加密的,在配置的时候会用到2. “mysql-proxy”是MySQL自己的读写分离代理3. “mysql-proxyd”是360弄出来的,后面有个“d”,服务的启动、重启、停止。都是用他来执行的 conf目录下放的是配置文件1. “test.cnf”只有一个文件,用来配置代理的,可以使用vim来编辑 lib目录下放的是一些包,以及Atlas的依赖log目录下放的是日志,如报错等错误信息的记录 进入bin目录,使用encrypt来对数据库的密码进行加密,我的MySQL数据的用户名是buck,密码是hello,我需要对密码进行加密[root@localhost bin]# ./encrypt hello RePBqJ+5gI4=配置Atlas,使用vim进行编辑[root@localhost conf]# cd /usr/local/mysql-proxy/conf/[root@localhost conf]# vim test.cnf进入后,可以在Atlas进行配置,360写的中文注释都很详细,根据注释来配置信息,其中比较重要,需要说明的配置如下:这是用来登录到Atlas的管理员的账号与密码,与之对应的是“#Atlas监听的管理接口IP和端口”,也就是说需要设置管理员登录的端口,才能进入管理员界面,默认端口是2345,也可以指定IP登录,指定IP后,其他的IP无法访问管理员的命令界面。方便测试,我这里没有指定IP和端口登录。#管理接口的用户名 admin-username = user #管理接口的密码 admin-password = pwd这是用来配置主数据的地址与从数据库的地址,这里配置的主数据库是135,从数据库是134#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔 proxy-backend-addresses = 192.168.246.134:3306#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔 proxy-read-only-backend-addresses = 192.168.246.135:3306@1这个是用来配置MySQL的账户与密码的,我的MySQL的用户是buck,密码是hello,刚刚使用Atlas提供的工具生成了对应的加密密码#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码! pwds = buck:RePBqJ+5gI4=这是设置工作接口与管理接口的,如果ip设置的”0.0.0.0”就是说任意IP都可以访问这个接口,当然也可以指定IP和端口,方便测试我这边没有指定,工作接口的用户名密码与MySQL的账户对应的,管理员的用户密码与上面配置的管理员的用户密码对应。#Atlas监听的工作接口IP和端口 proxy-address = 0.0.0.0:1234#Atlas监听的管理接口IP和端口 admin-address = 0.0.0.0:2345启动Atlas[root@localhost bin]# ./mysql-proxyd test start OK: MySQL-Proxy of test is started测试一下Atlas服务器的MySQL状态,要确认它是关闭状态,并且使用mysql命令,进不去数据库[root@localhost bin]# /etc/init.d/mysqld status mysqld is stopped[root@localhost bin]# mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)确认系统中自带的MySQL进不去了,使用如下命令,进入Atlas的管理模式“mysql -h127.0.0.1 -P2345 -uuser -ppwd ”,能进去说明Atlas正常运行着呢,因为它会把自己当成一个MySQL数据库,所以在不需要数据库环境的情况下,也可以进入到MySQL数据库模式。[root@localhost bin]# mysql -h127.0.0.1 -P2345 -uuser -ppwd Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1Server version: 5.0.99-agent-admin Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>可以访问“help”表,来看MySQL管理员模式都能做些什么。可以使用SQL语句来访问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", ... | | REMOVE BACKEND $backend_id | example: "remove backend 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 | +----------------------------+---------------------------------------------------------+16 rows in set (0.00 sec) mysql>也可以使用工作接口来访问,使用命令“mysql -h127.0.0.1 -P1234 -ubuck -phello”[root@localhost bin]# mysql -h127.0.0.1 -P1234 -ubuck -phello Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1Server version: 5.0.81-log Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>如果工作接口可以进入了,就可以在Windows平台下,使用Navicat来连接数据库,填写对应的host,Port,用户名,密码就可以
4. 读写分离测试
这里测试读写分离需要使用到Jmeter了,它是Java写第一套开源的压力测试工具,因为这个比较方便。他有专门测试MySQL的模块,需要使用MySQL的JDBC驱动jar包,配置很简单,东西很好很强大很好用。Jmeter下载地址:http://jmeter.apache.org/download_jmeter.cgiMySQL的JDBC :http://dev.mysql.com/downloads/connector/j/ 下载下来后,分别都解压开来,打开Jmeter ( 在bin路面下的jmeter.bat ) ,在测试计划中,导致JDBC的jar包
配置JDBC的驱动
分别做查询与插入语句
配置好了以后,就先运行查询操作,然后分别监控主数据库与从数据库所在机器的流量,来确定是否读写,使用“sar -n DEV 1 10000”命令来监控读写先来测试写,目前数据库里面一条信息都没有,开启配置好了的Jmeter,进行写入数据测试
主数据库 ( 192.168.246.134 )从数据库 ( 192.168.246.135 )可以看到测试插入数据的操作时,主数据库的网卡流量很大,从数据库的流量很小,是应为主数据是主要负责写入的,而从数据库主要是负责同步的。
查看数据库,发现已经插入了6W多条数据了。
进行读取数据的测试,只需要执行查询就好,执行“select *from sbtest;”来查询数据表主数据库 ( 192.168.246.134 )从数据库 ( 192.168.246.135 )可以看到135数据库的流量非常大,134没有什么流量,这下就可以确定了数据是从数据库读取的。已经实现了读写分离。
相关文章推荐
- MySQL + Atlas --- 部署读写分离
- MySQL + Atlas --- 部署读写分离
- MySQL + Atlas --- 部署读写分离
- MySQL + Atlas --- 部署读写分离
- MySQL + Atlas --- 部署读写分离(实现,其他的技术有参考文档)
- MySQL + Atlas --- 部署读写分离
- MySQL + Atlas --- 部署读写分离
- MySQL + Atlas --- 部署读写分离(参考02)
- Atlas实现MySQL大表部署读写分离
- 使用Atlas实现MySQL读写分离
- MySQL-Proxy实现读写分离部署文档 推荐
- 使用奇虎360的Atlas实现MySQL读写分离
- 采用Atlas+Keepalived实现MySQL读写分离、读负载均衡
- Atlas+keepalived实现mysql读写分离
- 使用mysql 中间件 Atlas 实现数据库读写分离
- mysql读写分离(使用Atlas实现)
- 采用Atlas+Keepalived实现MySQL读写分离、读负载均衡
- 使用Atlas实现MySQL读写分离+MySQL-(Master-Slave)配置
- Amoeba实现Mysql读写分离部署文档
- 部署mysql高可用、读写分离集群 推荐