MySQL+Atlas读写分离

659次阅读

共计 9138 个字符,预计需要花费 23 分钟才能阅读完成。

环境

系统

IP

配置

CentOS 6.5

192.168.2.40

Atlas代理服务

CentOS 6.5

192.168.2.41

主MySQL数据库

CentOS 6.5

192.168.2.42

从MySQL数据库

      CentOS 6.5

192.168.2.43

从MySQL数据库

数据库的配置

需要进入192.168.2.41、192.168.2.42和192.168.2.43数据库中配置用户名与密码,用户必须是远程可以访问的用户,配置方法如下:

 

首先进入到192.168.2.41的MySQL数据库中,创建用户“repl”设置密码为“hello”下列标红的是用户与密码。

mysql> grant all on *.* to repl@'192.168.2.%' identified by "hello";
Query OK, 0 rows affected (0.00 sec)

 

mysql> select user, host from user;
+------+-----------------------+
| user | host                  |
+------+-----------------------+
| repl | %                     |
| root | 192.168.2.%           |
|      | 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)

 

注意:192.168.2.42/43数据库与192.168.2.41的数据库同样配置, 记得要创建同样的数据库】

主从数据库连接

配置主从服务器需要编写MySQL的配置文件,详情配置步骤如下:

主服务器 ( 192.168.2.41),使用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 'repl'@'192.168.2.%' 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”数据库,主从数据库如果数据不一样,首先需要手动去同步一下数据

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.2.42/43 ),也需要使用vim进行配置,只需要在[mysqld]下面加入server-id=2和server-id=3就可以,全部配置如下:

192.168.2.42:

[mysqld]
datadir=/data/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

192.168.2.43:

[mysqld]
datadir=/data/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql

server-id=3

# 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.2.41′

主服务器的IP

master_user=’repl’

配置主服务器的用户名

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.2.41',
    -> master_user='repl',
    -> 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.2.41
                  Master_User: repl
                  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: 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

 

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

下载好了之后,进行安装

[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 16
drwxr-xr-x. 2 root root 4096 Dec 28 10:47 bin
drwxr-xr-x. 2 root root 4096 Dec 28 10:47 conf
drwxr-xr-x. 3 root root 4096 Dec 28 10:47 lib
drwxr-xr-x. 2 root root 4096 Dec 17  2014 log

bin目录下放的都是可执行文件

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 = admin

#管理接口的密码
admin-password = admin.com

这是用来配置主数据的地址与从数据库的地址,这里配置的主数据库是41,从数据库是42/43

#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 192.168.2.41:3306

#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
proxy-read-only-backend-addresses = 192.168.2.42:3306@1,192.168.2.43:3306@1

这个是用来配置MySQL的账户与密码的,我的MySQL的用户是repl,密码是hello,刚刚使用Atlas提供的工具生成了对应的加密密码

#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!
pwds = repl: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 -h127.0.0.1 -P2345 -uadmin -padmin.com”,能进去说明Atlas正常运行着呢,因为它会把自己当成一个MySQL数据库,所以在不需要数据库环境的情况下,也可以进入到MySQL数据库模式。

[root@localhost bin]# mysql -h127.0.0.1 -P2345 -uadmin -padmin.com
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server 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 -urepl -phello”

[root@localhost bin]# mysql -h127.0.0.1 -P1234 -urepl -phello
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server 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>
mysql> select * from backends;
+-------------+-------------------+-------+------+
| backend_ndx | address | state | type |
+-------------+-------------------+-------+------+
| 1 | 192.168.2.41:3306 | up | rw |
| 2 | 192.168.2.42:3306 | up | ro |
| 3 | 192.168.2.43:3306 | up | ro |
+-------------+-------------------+-------+------+
3 rows in set (0.01 sec)

读写分离测试

这里测试读写分离需要使用到Jmeter了,它是Java写第一套开源的压力测试工具,因为这个比较方便。他有专门测试MySQL的模块,需要使用MySQL的JDBC驱动jar包,配置很简单,东西很好很强大很好用。

MySQL+Atlas读写分离

测试写入操作:

MySQL+Atlas读写分离

MySQL+Atlas读写分离

可以看到主从数据库的流量都很高,主数据负责写入,从数据库在负责同步数据。

测试读取操作:

MySQL+Atlas读写分离

MySQL+Atlas读写分离

可以看到在执行读取操作时,主库基本没有流量,而从库流量非常大,这下就可以确定了数据是从数据库读取的。已经实现了读写分离。

正文完
 
mervinwang
版权声明:本站原创文章,由 mervinwang 2017-08-30发表,共计9138字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
文章搜索