当今MySQL使用至关普遍,随着用户的增多以及数据量的增大,高并发随之而来。mysql
然而咱们有不少办法能够缓解数据库的压力。分布式数据库、负载均衡、读写分离、增长缓存服务器等等。git
这里咱们将采用读写分离技术进展缓解数据库的压力。github
数据库写入效率要低于读取效率,通常系统中数据读取频率高于写入频率,单个数据库实例在写入的时候会影响读取性能,这是作读写分离的缘由。sql
系统 | 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
下列标红的是用户与密码。vim
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的数据库一样配置, 记得要建立一样的数据库bash
配置主从服务器须要编写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
<span style="color: #ff0000;">Slave_IO_Running: Yes</span>
<span style="color: #ff0000;">Slave_SQL_Running: Yes</span>
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-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目录下放的都是可执行文件
"encrypt"是用来生成MySQL密码加密的,在配置的时候会用到
"mysql-proxy"是MySQL本身的读写分离代理
"mysql-proxyd"是360弄出来的,后面有个"d",服务的启动、重启、中止。都是用他来执行的
conf目录下放的是配置文件
"test.cnf"只有一个文件,用来配置代理的,可使用vim来编辑
lib目录下放的是一些包,以及Atlas的依赖
log目录下放的是日志,如报错等错误信息的记录
进入bin目录,使用encrypt
来对数据库的密码进行加密,个人MySQL数据的用户名是repl
,密码是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加密 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的管理模式
[root@localhost bin]# 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包,配置很简单,东西很好很强大很好用。
能够看到主从数据库的流量都很高,主数据负责写入,从数据库在负责同步数据。
能够看到在执行读取操做时,主库基本没有流量,而从库流量很是大,这下就能够肯定了数据是从数据库读取的。已经实现了读写分离。