amoeba和mysql_proxy分别实现mysql-5.6的读写分离+mysql主从复制

一、mysql-5.6的主从复制

前言:

MySQL 5.6引入的GTID(Global Transaction IDs)使得其复制功能的配置、监控及管理变得更加易于实现,且更加健壮。

MySQL 5.6.10版本提供了更方便的基于GTID的复制功能,MySQL可以通过GTID自动识别上次同步的点,极大地方便了运维人员,减少出错的几率。

注意:mysql5.6之前的版本是不支持gtid的。

binlog-format:二进制日志的格式,有rowstatementmixed几种类型;

需要注意的是:当设置隔离级别为READ-COMMITED必须设置二进制日志格式为ROW,现在MySQL官方认为STATEMENT这个已经不再适合继续使用;但mixed类型在默认的事务隔离级别下,可能会导致主从数据不一致;

Mysql5.6下载地址

http://downloads.mysql.com/archives.php?p=mysql-5.6&v=5.6.13


Node1172.16.5.11

Node2172.16.5.12

主节点Node1配置

# tar xf mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
# useradd -r mysql
# cd /usr/local/
# ln -sv mysql-5.6.13-linux-glibc2.5-x86_64 mysql
# cd mysql
# chown -R root.mysql
# cp support-files/mysql.server /etc/init.d/mysqld
# chmod +x /etc/init.d/mysqld
# chkconfig --add mysqld
# vim /etc/my.cnf
client]
port= 3306
socket= /tmp/mysql.sock
[mysqld]
port= 3306
socket= /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 2
datadir = /mydata/data
innodb_file_per_table = 1
log-bin=mysql-bin
binlog-format=row   #当设置隔离级别为READ-COMMITED必须设置二进制日志格式为ROW
log-slave-updates=true   #从服务器更新二进制日志
gtid-mode=on #启用gtid
enforce-gtid-consistency=true    #强制gtid的一致性
master-info-repository=TABLE   #主服务器信息的记录方式,记录到表中,下同
relay-log-info-repository=TABLE  #
sync-master-info=1
slave-parallel-workers=2  # 主服务器有几个库,就启动几个
binlog-checksum=CRC32  #二进制日志的校验算法
master-verify-checksum=1   #主服务是否校验
slave-sql-verify-checksum=1  #从服务器是否校验
binlog-rows-query-log_events=1 #启动行级查询日志事件
report-port=3306
report-host=www.a.com #报告主机名
server-id= 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
# vim /etc/profile.d
export PATH=/usr/local/mysql/bin:$PATH
# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
# ldconfig -v
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# service mysqld start


从节点node2配置

将一样的配置都从节点node1中复制过来

# scp /etc/my.cnf 172.16.5.12:/etc/
# scp /etc/profile.d/mysql.sh 172.16.5.12:/etc/profile.d/
# scp /etc/ld.so.conf.d/mysql.conf 172.16.5.12:/etc/ld.so.conf.d/
# scp mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz 172.16.5.12:/root
然后
# . /etc/profile.d/mysql.sh
# ldconfig
# vim /etc/my.cnf
Server-id 10
report-host=www.b.com  #修改server-id和主机名即可
# tar xf mysql-5.6.13-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
# useradd -r mysql
# cd /usr/local/
# ln -sv mysql-5.6.13-linux-glibc2.5-x86_64 mysql
# cd mysql
# chown -R root.mysql ./*
# cp support-files/mysql.server /etc/init.d/mysqld
# chmod +x /etc/init.d/mysqld
# chkconfig --add mysqld
# mkdir -pv /mydata/data
# chown -R mysql.mysql /mydata/data
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# service mysqld start

主从复制配置

Node1

mysql> grant replication slave,replication client on *.* to 'testuser'@'172.16.%.%' identified by 'pass';

Node2

mysql> change master to
master_host='172.16.5.11',master_user='testuser',master_password='pass',master_auto_position=1;
mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.5.11
                  Master_User: testuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 955
               Relay_Log_File: www-relay-bin.000002
                Relay_Log_Pos: 1165
        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: 955
              Relay_Log_Space: 1367
              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:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: 6d41030e-3318-11e3-900b-000c2920c7a4
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set: 6d41030e-3318-11e3-900b-000c2920c7a4:1-4
            Executed_Gtid_Set: 6d41030e-3318-11e3-900b-000c2920c7a4:1-4
                Auto_Position: 1


node1
mysql> CREATE DATABASE db1;
node2
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| test               |
+--------------------+

错误与解决办法

从节点执行 change master to

master_host='172.16.5.11',master_user='testuser',master_password='pass',master_auto_position=1;后,报错:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 172.16.5.11
                  Master_User: testuser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File:
          Read_Master_Log_Pos: 4
               Relay_Log_File: www-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File:
             Slave_IO_Running: Connecting
            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: 0
              Relay_Log_Space: 151
              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: 1045
                Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 1
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 0
                  Master_UUID:
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp: 131012 16:51:54
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 1

从节点Mysql初始化,主从节点都对mysql初始化,再次授权同步,依然如此错误。

错误原因,连接失败,在从节点执行

# mysql -utestuser -ppass -h172.16.5.11
Warning: Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'testuser'@'www.a.com' (using password: YES)

竟然连不上过去,问题找到了,权限的问题,主从复制,连接的时候dns解析了,而主服务器中mysql.user表中的testuser对应的密码是ip地址,而不是主机名,因此连接失败。

解决办法:

在主服务器上

# vim /etc/my.cnf

skip-name-resolve

# service mysqld restart

从服务器上

# mysql

mysql>

change master to

master_host='172.16.5.11',master_user='testuser',master_password='pass',master_auto_position=1;

这下就不会有问题了。如果不你放心,可以在从服务器上验证授权用户是否可以登录。

mysql半同步复制

Node1

加载模块,启动功能

mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
mysql> set global rpl_semi_sync_master_enabled =1;
mysql> set global rpl_semi_sync_master_timeout = 1000;

#主服务器等待从服务器复制的时间,如果是同步复制模式,主服务器会一直等到从服务器复制完成。小量数据无法验证效果。

验证功能启动与否

mysql> show global status like 'rpl_semi%';

node2

加载模块,启动功能

mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
mysql> set global rpl_semi_sync_slave_enabled = 1;

重启io线程

mysql> stop slave io_thread;
mysql> start slave io_thread;

验证功能启动与否

mysql> show global status like 'rpl_semi%';

基于sslmysql主从复制

将除了初始化外其余的配置完成后,先不要初始化数据库,先做ca证书

主节点

将主节点创建成ca服务器,并为mysql主服务器颁发证书

# cd /etc/pki/CA
# (umask 077;openssl genrsa -out private/cakey.pem 2048)     //生成私钥
Generating RSA private key, 2048 bit long modulus
.................+++
.......................+++
e is 65537 (0x10001)
[root@www CA]# openssl req -new -x509 -key private/cakey.pem -out  //做ca自己的证书
cacert.crt -days 3650
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HeNan
Locality Name (eg, city) [Default City]:ZhengZhou
Organization Name (eg, company) [Default Company Ltd]:qiufengsong
Organizational Unit Name (eg, section) []:xuanhuan
Common Name (eg, your name or your server's hostname) []:www.a.com
Email Address []:      //直接回车
# touch serial index.txt   //ca数据库文件
# echo 01 > index.txt
# cd /usr/local/mysql/
# mkdir ssl
# chown -R mysql.mysql ssl
# cd ssl
# (umask 077;openssl genrsa -out master.key 2048) // 为mysql主服务器生成私钥
# openssl req -new -key master.key -out master.csr -days 365
# openssl ca -in master.csr -out master.crt -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HeNan  
Locality Name (eg, city) [Default City]:ZhengZhou
Organization Name (eg, company) [Default Company Ltd]:qiufengsong
Organizational Unit Name (eg, section) []:xuanhuan
Common Name (eg, your name or your server's hostname) []:www.qiufengsong.com
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@www ssl]# openssl ca -in master.csr -out master.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Oct 13 13:39:20 2013 GMT
            Not After : Oct 13 13:39:20 2014 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = HeNan
            organizationName          = qiufengsong
            organizationalUnitName    = xuanhuan
            commonName                = www.qiufengsong.com
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                7D:3D:84:DF:75:52:74:5C:BF:1D:62:14:F9:D1:7E:92:C5:E2:75:42
            X509v3 Authority Key Identifier:
                keyid:41:71:FB:A6:23:11:58:B6:0F:F0:D9:1A:2E:E1:98:26:E8:46:C7:D4
Certificate is to be certified until Oct 13 13:39:20 2014 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

主节点证书颁发完毕

为从节点颁发证书,从节点先生成证书申请,将证书申请发给ca服务器,即主节点,主节点签署完毕,再发送给从节点。

提供一个脚本,可以自动生成私钥文件和证书申请,并让ca签署证书

#!/bin/bash
(umask 077;openssl genrsa -out master.key 2048) #// 为mysql主服务器生成私钥
echo 'CN
HeNan
ZhengZhou
qiufengsong
xuanhuan
www.qiufengsong.com
' | openssl req -new -key master.key -out master.csr -days 365
echo 'y
y' | openssl ca -in master.csr -out master.crt -days 365


遇到问题

签署证书的时候,failed to update database,这时,删除/etc/pki/CA/index.txt,再重新touch一个,问题就解决了。

修改上边的脚本,然后执行,再将生成的ca证书发送到从节点上。


#vim ssl.sh
#!/bin/bash
(umask 077;openssl genrsa -out slave.key 2048) #// 为mysql主服务器生成私钥
echo 'CN
HeNan
ZhengZhou
qiufengsong
xuanhuan
www.qiufengsong.com
' | openssl req -new -key slave.key -out slave.csr -days 365
echo 'y
y' | openssl ca -in slave.csr -out slave.crt -days 365
# scp slave.crt slave.csr slave.key 172.16.5.12:/usr/local/mysql/ssl/
# scp /etc/pki/CA/cacert.pem 172.16.5.12:/usr/local/mysql/ssl/

主从节点都修改ssl文件及目录下文件的权限

# chown -R mysql.mysql ssl

要想持久有效,将ssl安全相关的文件写入/etc/my.cnf

主节点

# vim /etc/my.cnf
[client]
port= 3306
socket= /tmp/mysql.sock
[mysqld]
explicit_defaults_for_timestamp
port= 3306
socket= /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 2
datadir = /mydata/data
innodb_file_per_table = 1
log-bin=mysql-bin
binlog-format=row   #当设置隔离级别为READ-COMMITED必须设置二进制日志格式为ROW
log-slave-updates=true   #从服务器更新二进制日志
gtid-mode=on #启用gtid
enforce-gtid-consistency=true    #强制gtid的一致性
master-info-repository=TABLE   #主服务器信息的记录方式,记录到表中,下同
relay-log-info-repository=TABLE  #
sync-master-info=1
slave-parallel-workers=2  # 主服务器有几个库,就启动几个
binlog-checksum=CRC32  #二进制日志的校验算法
master-verify-checksum=1   #主服务是否校验
slave-sql-verify-checksum=1  #从服务器是否校验
binlog-rows-query-log_events=1 #启动行级查询日志事件
report-port=3306
report-host=www.a.com #报告主机名
server-id= 1
ssl
ssl-ca=/etc/pki/CA/cacert.pem
ssl-key=/usr/local/mysql/ssl/master.key
ssl-cert=/usr/local/mysql/ssl/master.crt
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout


从节点

# vim /etc/my.cnf
[client]
port= 3306
socket= /tmp/mysql.sock
[mysqld]
explicit_defaults_for_timestamp
port= 3306
socket= /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 2
datadir = /mydata/data
innodb_file_per_table = 1
log-bin=mysql-bin
binlog-format=row   #当设置隔离级别为READ-COMMITED必须设置二进制日志格式为ROW
log-slave-updates=true   #从服务器更新二进制日志
gtid-mode=on #启用gtid
enforce-gtid-consistency=true    #强制gtid的一致性
master-info-repository=TABLE   #主服务器信息的记录方式,记录到表中,下同
relay-log-info-repository=TABLE  #
sync-master-info=1
slave-parallel-workers=2  # 主服务器有几个库,就启动几个
binlog-checksum=CRC32  #二进制日志的校验算法
master-verify-checksum=1   #主服务是否校验
slave-sql-verify-checksum=1  #从服务器是否校验
binlog-rows-query-log_events=1 #启动行级查询日志事件
report-port=3306
report-host=www.b.com #报告主机名
server-id= 10
ssl
ssl-ca=/usr/local/mysql/ssl/cacert.pem
ssl-key=/usr/local/mysql/ssl/slave.key
ssl-cert=/usr/local/mysql/ssl/slave.crt
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout

主从节点初始化数据库并启动mysqld服务

# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# service mysqld start


主节点

mysql> grant replication slave on *.* to 'test'@'172.16.%.%' identified by 'pass' require ssl;
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000003 |      545 |              |                  | 5c10ed87-33f5-11e3-95ab-000c2920c7a4:1-2 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)


从节点

mysql> change master to
master_host='172.16.5.11',master_user='test',master_password='pass',master_log_file='mysql-bin.000003',master_log_pos=545,master_ssl=1,master_ssl_ca='/usr/local/mysql/ssl/cacert.pem',master_ssl_cert='/usr/local/mysql/ssl/slave.crt',master_ssl_key='/usr/local/mysql/ssl/slave.key';
mysql> START SLAVE;
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.5.11
                  Master_User: test
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 545
               Relay_Log_File: www-relay-bin.000002
                Relay_Log_Pos: 314
        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: 545
              Relay_Log_Space: 516
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: Yes
           Master_SSL_CA_File: /usr/local/mysql/ssl/cacert.pem
           Master_SSL_CA_Path:
              Master_SSL_Cert: /usr/local/mysql/ssl/slave.crt
            Master_SSL_Cipher:
               Master_SSL_Key: /usr/local/mysql/ssl/slave.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:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
                  Master_UUID: 5c10ed87-33f5-11e3-95ab-000c2920c7a4
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl: /usr/local/mysql/ssl/cacert.pem
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0


二、amoeba实现mysql的读写分离

amoeba是java程序,需要jdkjava环境

Jdk下载地址

http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6-419409.html#jdk-6u45-oth-JPR

下载 jdk-6u45-linux-i586-rpm.bin

# chmod +x jdk-6u31-linux-x64-rpm.bin
# ./jdk-6u31-linux-x64-rpm.bin
Press Enter to continue.....
直接ctrl+c退出
# cd /usr/java
# ls
default jdk1.6.0_31 latest
#vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH
# . /etc/profile.d/java.sh
# java -version
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b04) 运行环境版本
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01, mixed mode)


Amoeba下载地址

http://sourceforge.net/projects/amoeba/files/Amoeba%20for%20mysql/2.2.x/

安装配置

由于amoeba解压后没有自己的独立目录,需要手动建立


# mkdir /usr/local/amoeba
# tar xf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba-mysql-2.2.0
# cd /usr/local/amoeba
# ln -sv amoeba-mysql-2.2.0 amoeba
# vim /etc/profile.d/amoeba
export AMOEBA_HOME=/usr/local/amoeba
export PATH=$AMOEBA_HOME/bin:$PATH
# amoeba
amoeba start|stop  # 说明配置文件已经生效
# cd conf


前端连接web服务器主要修改的配置

# vim amoeba.xml
<property name="port">3306</property>
<property name="ipAddress">0.0.0.0</property>
<property name="user">root</property>   #前端web程序连进mysql的用户名
<property name="password">password</property>#前端web程序连进mysql的密码
<property name="LRUMapSize">1500</property>
<property name="defaultPool">mypool</property>
 <!--
 <property name="writePool">db1</property>   # 写操作对应的mysql服务器  需要是启动
 <property name="readPool">db2</property>    # 读操作对应的mysql服务器
 -->

后端连接mysql服务器主要修改的配置


# vim dbServer.xml
 <property name="port">3306</property>
<!-- mysql schema -->
<property name="schema">shopxx</property>
<!-- mysql user -->
<property name="user">shopxxuser</property>
<!--  mysql password
<property name="password">password</property>
 -->
<property name="password">shopxxpass</property>

# 以上是amoeba连接后方mysql服务器对应的端口和数据库以及数据库的用户名和密码,下边是后端默认用到的mysql服务器

<dbServer name="db1"  parent="abstractServer">
# 主要用来定义后端mysql服务器的属性
        <factoryConfig>
      <!-- mysql ip -->
      <property name="ipAddress">172.16.5.15</property>
         </factoryConfig>
        </dbServer>

#定义后端mysql服务器池,主要实现三个功能,轮叫调度负载,权重调度负载,高可用


<dbServer name="myPool" virtual="true">
Mysql
 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
   <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
             <property name="loadbalance">1</property>
   <!-- Separated by commas,such as: server1,server2,server1 -->
         <property name="poolNames">db1,db2</property>
                </poolConfig>
        </dbServer>

读写分离的实现则需要如下配置代码

前端

# vim amoeba.xml
<!--<property name="defaultPool">mypool</property>-->
 <property name="writePool">db1</property>   # 写操作对应的mysql服务器  需要是启动
 <property name="readPool">db2</property>    # 读操作对应的mysql服务器

后端

<dbServer name="db1"  parent="abstractServer">
  <factoryConfig>
    <!-- mysql ip -->
     <property name="ipAddress">172.16.5.11</property>
  </factoryConfig>
        </dbServer>
        <dbServer name="db2"  parent="abstractServer">
   <factoryConfig>
         <!-- mysql ip -->
         <property name="ipAddress">172.16.5.12</property>
   </factoryConfig>
        </dbServer>

三、mysql_proxy对mysql读写分离的实现

敬请期待。。。。

你可能感兴趣的:(amoeba,mysql主从复制,mysql_proxy,mysql-5.6)