Heim  >  Artikel  >  Datenbank  >  mysql proxy、mysql-mmm实现读写分离高可用性

mysql proxy、mysql-mmm实现读写分离高可用性

WBOY
WBOYOriginal
2016-06-07 17:40:351197Durchsuche

mysqlproxy、mysql-mmm实现读写分离测试环境:mysqlA:192.168.128.6mastermysqlB:192.168.128.7mastermysqlproxy+mysql-mmm:192.168.128.5数据库写操作:192.

mysql proxy、mysql-mmm实现读写分离
测试环境:
mysql A:192.168.128.6  master
mysql B:192.168.128.7  master
mysql proxy+mysql-mmm:192.168.128.5
数据库写操作:192.168.128.8
数据库读操作:192.168.128.9,192.168.128.10
(数据库库读、写ip为虚拟ip,不在网卡上配置)
说明:mysqlA、B互为master实现数据库复制;mysql-mmm实现高可用,网站空间,当其中一台服务器宕机,可将数据库读写操作自动切换到另一台;mysql proxy实现读写分离
一、    Mysql数据库安装(此处略过)
二、    Mysql数据库复制配置,两台数据库互为master
1、    数据库A上操作
登陆mysql
Mysql –u root –p
授权从服务器B同步数据用户
mysql> GRANT REPLICATION SLAVE ON *.* to 'slavedb'@'192.168.128.7' identified by '123456';
查看主服务器状态,记录红色字体标示,配置从服务器备用(注:数据库在生产系统时,需要先所表,防止数据库写入数据flush tables with read lock;)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000029 |      106 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.03 sec)
修改mysql配置文件
        vi /etc/my.cnf
        log-bin=mysql-bin
        server-id = 1   #设置server-id为1,1表示为主服务器
        binlog-do-db=  #需要进行同步的数据库,全部库都同步可不填
        binlog-ignore-db=  #不需要同步的数据库
2、    数据库B上操作
登陆mysql
Mysql –u root –p
授权从服务器B同步数据用户
mysql> GRANT REPLICATION SLAVE ON *.* to 'slavedb'@'192.168.128.6' identified by '123456';
查看主服务器状态,记录红色字体标示,配置从服务器备用
       mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 |      107 |              |                  |
+------------------+----------+--------------+------------------+
修改mysql配置文件
vi /etc/my.cnf
log-bin=mysql-bin
server-di = 2  #设置server-id为2
binlog-do-db=  #根据需要进行设置
binlog-ignore-db=    #根据需要进行设置
3、    分别登陆mysql A和B执行数据库同步命令
mysql –u root –p
停止slave同步
mysql> salve stop;

数据库A执行同步命令, master_log_file, master_log_pos选项需要根据主数据填写
Mysql>Change master to
master_host='192.168.128.7',
master_user='slavedb',
master_password='123456',
master_log_file='mysql-bin.000005',
master_log_pos=107;

数据库B执行同步命令, master_log_file, master_log_pos选项需要根据主数据填写
Mysql>Change master to
master_host='192.168.128.6',
master_user='slavedb',
master_password='123456',
master_log_file='mysql-bin.000029',
master_log_pos=106;

启动slave同步
mysql> salve start;
查看A同步状态,Slave_IO_Running,虚拟主机,Slave_SQL_Running同时为yes表示已开启同步
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.128.7
                  Master_User: slavedb
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 107
               Relay_Log_File: mysqld-relay-bin.000026
                Relay_Log_Pos: 252
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:        
查看B同步状态
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.128.6
                  Master_User: slavedb
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000029
          Read_Master_Log_Pos: 106
               Relay_Log_File: lamp-relay-bin.000026
                Relay_Log_Pos: 252
        Relay_Master_Log_File: mysql-bin.000029
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
4、    测试主从复制
在数据库A创建数据库db1
mysql>create database db1;
在数据库B创建数据库db2
mysql>create database db2;

分别在数据库A、B查看数据库是否复制
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| db2                |
| mysql              |
三、    配置mysql-mmm实现数据库双机热备高可用行
1、    在128.5、128.6、128.7三台服务器上分别安装mysql-mmm,需要perl支持
在128.6、128.7mysql服务器上安装perl模块
# perl -MCPAN -e shell
cpan> install Algorithm::Diff
cpan> install DBI
cpan>install Log::Dispatch
cpan> install Log::Log4perl
cpan> install Mail::Send
cpan> install Net::ARP
cpan> install Proc::Daemon
cpan> install Time::HiRes
cpan>install DBD::mysql
cpan>install File::stat
cpan>install File:basename

在128.5服务器安装perl模块
# perl -MCPAN -e shell
cpan> install Algorithm::Diff
cpan> install Class::Singleton
cpan> install Log::Dispatch
cpan> install Log::Log4perl
cpan> install Mail::Send
cpan> install Proc::Daemon
cpan> install Thread::Queue
cpan> install Time::HiRes
cpan> install DBI
cpan>install DBD::mysql

下载mysql-mmm
wget :mmm2:mysql-mmm-2.2.1.tar.gz
也可以安装EPEL yum源安装

安装mysql-mmm
tar –zxvf mysql-mmm-2.2.1.tar.gz
cd mysql-mmm-2.2.1
make install

mysql-mmm文件位置及用途
/usr/lib/perl5/vendor_perl/5.8.8/MMM                 MMM 使用的 perl 模块
/usr/lib/mysql-mmm                                              MMM 的脚本揑件
/usr/sbin                                                               MMM 的命令保存路径
/var/log/mysql-mmm                                             MMM 的日志保存路径
/etc                                                                      MMM 配置文件保存的路径
/etc/mysql-mmm                                                  MMM 配置文件保存的路径,优先级最高
/etc/init.d/                                                            agentd 和 monitor 的启劢关闭脚本

2、    修改mysql-mmm配置文件
数据库A配置文件
vi /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db1  

vi /etc/mysql-mmm/mmm_common.conf
active_master_role writer


        cluster_interface                       eth0

        pid_path                            /var/run/mmm_agentd.pid
        bin_path                                /usr/lib/mysql-mmm/

        replication_user                        slavedb
        replication_password                    123456

        agent_user                              mmm_agent
        agent_password                          mmm_agent



        ip                                      192.168.128.6
        mode                                    master
        peer                                    db2



        ip                                      192.168.128.7
        mode                                    master
        peer                                    db1



        hosts                                   db1, db2
        ips                                     192.168.128.8
        mode                                    exclusive



        hosts                                   db1, db2
        ips                            192.168.128.9, 192.168.128.10
        mode                                    balanced


把mmm_common.conf配置文件复制到数据库B
数据库B配置文件
vi /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db2

在数据库A、B中分别添加授权用户
GRANT ALL PRIVILEGES ON *.* TO "mmm_agent"@"192.168.128.7" IDENTIFIED BY 'mmm_agent' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO "mmm_agent"@"192.168.128.6" IDENTIFIED BY 'mmm_agent' WITH GRANT OPTION;

在数据库A、B上启动mysql-mmm客户端
[root@nagios soft]# service mysql-mmm-agent start
Daemon bin: '/usr/sbin/mmm_agentd'
Daemon pid: '/var/run/mmm_agentd.pid'
Starting MMM Agent daemon... Ok


修改128.5服务器mysql-mmm-monitor
vi /etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf

        ip                              127.0.0.1
        pid_path                        /var/run/mmm_mond.pid
        bin_path                        /usr/lib/mysql-mmm/
        status_path                     /var/lib/misc/mmm_mond.status
        ping_ips                        192.168.128.6, 192.168.128.7



        monitor_user                    mmm_monitor
        monitor_password                mmm_monitor

debug 0

vi /etc/mysql-mmm/mmm_common.conf
active_master_role writer

        ip                                      192.168.128.6
        mode                                    master
        peer                                    db2



        ip                                      192.168.128.7
        mode                                    master
        peer                                    db1




        hosts                                   db1, db2
        ips                                     192.168.128.8
        mode                                    exclusive



        hosts                                   db1, db2
        ips                        192.168.128.9, 192.168.128.10
        mode                                    balanced


在数据库A、B上添加授权账号
GRANT ALL PRIVILEGES ON *.* TO "mmm_monitor"@"192.168.128.5" IDENTIFIED BY 'mmm_monitor' WITH GRANT OPTION;

启动mysql-mmm-monitor
[root@centos ~]# service mysql-mmm-monitor start
Daemon bin: '/usr/sbin/mmm_mond'
Daemon pid: '/var/run/mmm_mond.pid'
Starting MMM Monitor daemon: Ok

查看mysql-mmm状态
[root@centos ~]# mmm_control show
db1(192.168.128.6) master/ONLINE. Roles: reader(192.168.128.10)
db2(192.168.128.7) master/ONLINE. Roles: reader(192.168.128.9), writer(192.168.128.8)

将数据库服务器设为在线状态
[root@centos ~]#mmm_control set_online db1

测试mysql-mmm,写操作在db2服务器上,将db2数据库停止,读操作将自动切换到db1
[root@centos ~]# mmm_control show
db1(192.168.128.6) master/ONLINE. Roles: reader(192.168.128.10), reader(192.168.128.9), writer(192.168.128.8)
db2(192.168.128.7) master/HARD_OFFLINE. Roles:

在客户端分别连接虚拟ip128.8,128.9,128.10进行读写测试

四、    配置数据库读写分离,由mysql proxy代理完成
1、    配置mysql proxy,根据需要下载32位或64位
wget
wget
解压mysql proxy
tar –zxvf mysql-proxy-0.8.3-linux-glibc2.3-x86-32bit.tar.gz
cp –r mysql-proxy-0.8.3-linux-glibc2.3-x86-32bit /usr/local/mysql-proxy
2、    创建mysql proxy配置文件,具体参数可查看/usr/local/mysql-proxy/bin/mysql-proxy –help-all
vi /etc/mysql-proxy.cnf   
[mysql-proxy]
user=root      #指定mysql-proxy运行用户
proxy-address=192.168.128.5:4040   #mysql-proxy运行运行ip和端口号
proxy-read-only-backend-addresses=192.168.128.9:3306  #读操作数据库地址
proxy-read-only-backend-addresses=192.168.128.10:3306
proxy-backend-addresses=192.168.128.8:3306   #写操作数据库地址
proxy-lua-script=/usr/local/mysql-proxy/scripts/rw-splitting.lua  #读写分离配置文件路径
log-file=/usr/local/mysql-proxy/log/mysql-proxy.log  #日志文件存放路径
log-level=debug
daemon=true  #以守护进程方式运行
keepalive=true   #mysql-proxy进程假死后自动重启

3、    创建日志存放目录
        mkdir /usr/local/mysql-proxy/log
4、    复制读写分离配置文件
mkdir /usr/local/mysql-proxy/scripts
cp /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua /usr/local/mysql-proxy/scripts
     
5、    修改读写分离配置文件,默认达到4个连接才进行读写分离
        vi /usr/local/mysql-proxy/scripts/rw-splitting.lua 
        if not proxy.global.config.rwsplit then
        proxy.global.config.rwsplit = {
                min_idle_connections = 1,  #默认4,改为1
                max_idle_connections = 1,  #默认8,虚拟主机,改为1
                is_debug = false 
        }
end

6、    修改mysql-proxy.cnf权限
chmod 660 /etc/mysql-proxy.cnf 

7、    启动mysql proxy
/usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf

8、    创建用于读写分离数据库连接账户
登陆主服务器,创建用户
mysql > GRANT ALL PRIVILEGES ON *.* TO "proxy"@"192.168.128.5" IDENTIFIED BY 'proxy' WITH GRANT OPTION;
9、    测试读写分离
从客户端登陆proxy代理,进行测试
[root@mysql ~]# mysql -u test -h192.168.128.5 -p -P4040
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 61
Server version: 5.1.61-log Source distribution
Copyright (c) 2000, 2011, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| db2                |
| db3                |
| mysql              |
| zabbix             |
+--------------------+
6 rows in set (0.16 sec)
查询为128.6数据库
关闭128.6mysql,在查询
mysql> show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    199
Current database: *** NONE ***

+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| db2                |
| db3                |
| mysql              |
| performance_schema |
| test               |
| wikidb             |
+--------------------+
8 rows in set (0.02 sec)

查询内容为128.7数据库
 

本文出自 “lishiy” 博客,请务必保留此出处

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn