2台y 发表于 2017-8-23 09:25:23

mysql主从复制读写分离-Altas

本文读写分离使用的软件是Altas,altas是奇虎360公司开发的开源数据库代理软件。它是基于mysql-proxy开发而成的
它集中地响应应用的请求,依据用户事先设置的规则,将SQL请求发送到特定的数据库上执行。基于此可以实现负载均衡、读写分离、高可用性等需求。

mysql读写分离原理:
    数据库层在高并发的情况下,i/o会产生瓶颈。而实际上用户读的请求要远远大于写的请求。

    使用代理服务作为数据库前端,将不同的请求根据规则分配到不同的后端数据上面去,比如将写的请求分配给master数据库,将读的请求分配给slave数据库。master和slave可以是一个或多个做成负载均衡。master数据库再通过主从复制同步数据给slave.

    了解mysql主从复制原理:http://lesliecheung.blog.51cto.com/12622169/1958255

环境介绍:
HostNameOSIP作用
mastercentos6.5192.168.100.150担任mysql主服务器
salvecentos6.5192.168.100.151担任mysql从服务器
Altascentos6.5192.168.100.152担任mysql代理
ftpcentos6.5192.168.100.100担任ftp为主从提供yum源,软件支持(可以使用公网yum源代替此主机)

1:主从安装mysql:
1
2
3
    # yum -y install mysql-server
   
    # yum -y install msyql-server




2:修改主从的配置文件,以支持bin_log日志记录

1
2
3
4
5
6
7
8
# vi /etc/my.cnf
7log-bin=mysql-bin      ##支持bin-log日志记录,bin-log日志文件名以mysql-bin开头
8server-id=150         ##服务的唯一标识符号,默认是1,这里方便记忆,我使用了ip最后一段

# vi /etc/my.cnf
7server-id=151
# /etc/init.d/mysqld start   ##重启服务
# /etc/init.d/mysqld start




3:在主数据库上面授权给从复制的权限:    登陆服务器授权
1
2
3
4
5
6
7
# mysqladmin -uroot password 123123
# 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两栏中内容:以查到的为准。

1
2
3
4
5
6
7
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的数据库    登入数据库

1
2
# mysqladmin -uroot password 123123
# mysql -uroot -p123123




    设置从服务器读取master bin-log的相关信息

1
2
3
4
5
6
7
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

1
2
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)




    插卡slave状态:   


1
2
3
            ##查到的状态这两个为yes,下面没有error错误就正常
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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:测试:
    在主数据库上新建库,查看库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)




    在从数据库上查看库:

1
2
3
4
5
6
7
8
9
10
mysql> show databases;
+--------------------+
| Database         |
+--------------------+
| information_schema |
| mysql            |
| test               |
| test_databases   |
+--------------------+
4 rows in set (0.00 sec)





在master端授权:

1
2
3
4
5
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服务器下载安装软件

1
# wget -O ./altashttps://github.com/Qihoo360/Atlas/releases/download/sharding-1.0.1/Atlas-sharding_1.0.1-el6.x86_64.rpm





1
2
3
4
5
6
7
# ls
altasanaconda-ks.cfginstall.loginstall.log.syslog
# file altas
altas: RPM v3.0 bin i386/x86_64 Atlas-sharding_1.0.1-el6
# rpm -ivh altas
Preparing...                ###########################################
   1:Atlas                  ###########################################




修改配置文件:

1
# vim /usr/local/mysql-proxy/conf/test.cnf




    需要更改的地方:


1
proxy-backend-addresses = 192.168.100.150:3306





1
proxy-read-only-backend-addresses = 192.168.100.151:3306





1
pwds = root:++gAN07C/Q0=   ##这里用/usr/local/mysql-proxy/bin/encrypt 加上数据库授权的密码,生成密文密码填写在这里




      生成密文密码


1
2
3
4
# pwd
/usr/local/mysql-proxy/bin
# ./encrypt 123123
++gAN07C/Q0=





1
daemon = true





1
sql-log = REALTIME





1
charset = utf8





    全部的配置项,以及注释


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#管理接口的密码
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





启动关闭代理服务:

1
2
3
4
5
6
7
8
9
10
11
# ls
encryptmysql-binlog-dumpmysql-myisam-dumpmysql-proxymysql-proxydVERSION
# ./mysql-proxyd test start
OK: MySQL-Proxy of test is started
# ./mysql-proxyd test stop
OK: MySQL-Proxy of test is stopped
# ./mysql-proxyd test start
OK: MySQL-Proxy of test is started
# ./mysql-proxyd test restart
OK: MySQL-Proxy of test is stopped
OK: MySQL-Proxy of test is started




查看进程:


1
2
3
4
# ps aux |grep mysql-proxy
root      12660.00.2671561452 ?      S    19:24   0:00 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
root      12670.00.6 1614603352 ?      Sl   19:24   0:01 /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/test.cnf
root   167560.00.1 103248   876 pts/0    S+   20:55   0:00 grep mysql-proxy





安装mysql,只安装客户端。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# yum -y install mysql

# 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




查看日志信息:

1
# 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: 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数据库进行管理)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# mysql -uuser -ppwd -h192.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)






页: [1]
查看完整版本: mysql主从复制读写分离-Altas