首页 > 代码库 > Mysql主从+Amoeba读写分离
Mysql主从+Amoeba读写分离
一.安装环境
centos6.5 192.168.1.100 Amoeba-mysql
centos6.5 192.168.1.101 mysql-主
centos6.5 192.168.1.102 mysql-从
二.Mysql 主从配置
在主MySQL192.168.1.101上配置
Vim /usr/local/mysql/etc/my.cnf
server-id = 1 主库一定要比从库的数字小
binlog-do-db = backup 从库从主库同步的数据库库,多个库写多行
binlog-ignore-db = myqsl 从库不同步主库的数据库,多个库写多行
单独mysql主从来说上面3个设置就行,并且重新启动MySQL
设置MySQL主从主数据库192.168.1.101对192.168.1.102的slave权限
mysql> grant replication slave on *.* to ‘slave‘@‘192.168.1.102‘ identified by ‘slavepasswd‘; Query OK, 0 rows affected (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
在主库上执行
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 429 | backup | myqsl | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
2. 在从MySQL192.168.1.102上配置
Vim /usr/local/mysql/etc/my.cnf
server-id = 2 replicate-do-db = backup replicate-ignore-db = mysql
启动MySQL
注意:在设置主从之前最好先在两个数据库中创建backup数据库。未免从库在同步的时候自己因为没有这个库而导致主从失败。
create database backup;
在MySQL里执行
mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> change master to master_host=‘192.168.1.101‘,master_user=‘slave‘,master_password=‘slavepasswd‘,master_log_file=‘mysql-bin.000003‘,master_log_pos=429; Query OK, 0 rows affected (0.07 sec)
启动slave并且查看主从情况
mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.101 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 429 Relay_Log_File: localhost-relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: backup Replicate_Ignore_DB: mysql Replicate_Do_Table: Replicate_Ignore_Table:
3. 验证从库是否正常同步主库的数据
1)在主库192.168.1.101
mysql> use backup; Database changed mysql> create table user(id int(5),name char(10)); Query OK, 0 rows affected (0.27 sec) mysql> show tables; +------------------+ | Tables_in_backup | +------------------+ | user | +------------------+ 1 row in set (0.00 sec)
2)在从库192.168.1.102上验证
mysql> use backup; Database changed mysql> show tables; +------------------+ | Tables_in_backup | +------------------+ | user | +------------------+ 1 row in set (0.00 sec)
同步了,最开始创建的时候只是创建了库,并没有创建表。。
在线上的部署数据库应该先把主库锁表不让主库写入,在把整个数据库导入到从库,在把主库解除锁表。
三.Amoeba-mysql实现MySQL读写分离的配置(主库写入,从库读)
Amoeba-mysql 版本
下载: http://sourceforge.net/projects/amoeba/files/Amoeba%20for%20mysql/3.x/amoeba-mysql-3.0.5-RC-distribution.zip
因为amoeba使用Java写的所以要在服务器上安装JAVA
mkdir /usr/java
tar -xzf jdk-7u67-linux-x64.tar.gz -C /usr/java/
Vim /etc/profile
export JAVA_HOME=/usr/java/jdk1.7.0_67 export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin
source /etc/profile [root@localhost bin]# java -version java version "1.7.0_67" Java(TM) SE Runtime Environment (build 1.7.0_67-b01) Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)
在主库,从库里设置对amoeba-mysql服务器的远程权限
mysql> grant all on *.* to ‘amoeba‘@‘192.168.1.100‘ identified by ‘amoebapasswd‘; Query OK, 0 rows affected (0.08 sec) mysql> flush privileges; Query OK, 0 rows affected (0.02 sec)
1)安装Amoeba-mysql
unzip amoeba-mysql-3.0.5-RC-distribution.zip -D /usr/local/ mv amoeba-mysql-3.0.5-RC amoeba
2)配置Amoeba-mysql
我们实现MySQL读写分离,只需要配置
amoeba.xml 主配置文件
dbServers.xml MySQL数据库信息文件
配置dbServers.xml
<dbServer name="abstractServer" abstractive="true"> <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory"> <property name="connectionManager">${defaultManager}</property> <property name="sendBufferSize">64</property> <property name="receiveBufferSize">128</property> <!-- mysql port --> <property name="port">3306</property> <!-- mysql schema --> <property name="schema">backup</property> <!-- mysql user --> <property name="user">amoeba</property> 远程用户和密码 <property name="password">amoebapasswd</property> </factoryConfig>
<dbServer name="master" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.1.101</property> IP:主库master </factoryConfig> </dbServer> <dbServer name="slave" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.1.102</property> IP:从库slave </factoryConfig> </dbServer> 下面设置slave从库池.multiPool,如果多个slave则在poolNames里增加,以,隔开 <dbServer name="multiPool" virtual="true"> <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">slave</property> </poolConfig> </dbServer>
配置amoeba.xml主配置文件
<property name="authenticateProvider"> <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator"> 这里的用户和密码为登入amoeba的用户密码,而不是主从数据库的 <property name="user">root</property> <property name="password">root</property> <property name="filter"> <bean class="com.meidusa.toolkit.net.authenticate.server.IPAccessController"> <property name="ipFile">${amoeba.home}/conf/access_list.conf</property> </bean> </property> </bean> </property>
设置读写设置
<property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property> <property name="LRUMapSize">1500</property> <property name="defaultPool"></property> <property name="defaultPool">master</property> 设置主数据库为写数据库 <property name="writePool">master</property> 设置从数据库池为读数据库 <property name="readPool">multiPool</property> </queryRouter>
四.启动amoeba
[root@localhost bin]# ./launcher The stack size specified is too small, Specify at least 228k Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. The stack size specified is too small, Specify at least 228k Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit.
发现错误
The stack size specified is too small, Specify at least 228k
在amoeba程序的根目录里有一个jvm.properties 优化设置
JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss196k -XX:PermSize=16m -XX:MaxPermSize=96m"
设置为
JVM_OPTIONS="-server -Xms256m -Xmx1024m -Xss300k -XX:PermSize=16m -XX:MaxPermSize=96m"
只要大于上面提示的228K就可以了
[root@localhost bin]# ./launcher The stack size specified is too small, Specify at least 228k Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. The stack size specified is too small, Specify at least 228k Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. 2014-09-03 18:01:17 [INFO] Project Name=Amoeba-MySQL, PID=1086 , starting... log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml 2014-09-03 18:01:19,883 INFO context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-3.0.4-BETA log4j:WARN ip access config load completed from file:/usr/local/amoeba/conf/access_list.conf 2014-09-03 18:01:21,513 INFO net.ServerableConnectionManager - Server listening on 0.0.0.0/0.0.0.0:8066.
OK了,查看下端口
[root@localhost bin]# netstat -natpl Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 932/sshd tcp 0 52 192.168.1.100:22 192.168.1.2:62555 ESTABLISHED 965/sshd tcp 0 0 :::22 :::* LISTEN 932/sshd tcp 0 0 :::8066 :::* LISTEN 1118/java tcp 0 0 ::ffff:192.168.1.100:8260 ::ffff:192.168.1.101:3306 ESTABLISHED 1118/java tcp 0 0 ::ffff:192.168.1.100:8258 ::ffff:192.168.1.101:3306 TIME_WAIT - tcp 0 0 ::ffff:192.168.1.100:58189 ::ffff:192.168.1.102:3306 TIME_WAIT - tcp 0 0 ::ffff:192.168.1.100:58191 ::ffff:192.168.1.102:3306 ESTABLISHED 1118/java
登入
amoeba 账号和密码可就是主配置文件里设置的
五 验证读写效果
在刚刚登入的amoeba里查询下看看
mysql> use backup; Database changed mysql> show tables; +------------------+ | Tables_in_backup | +------------------+ | user | +------------------+ 1 row in set (0.01 sec)
OK,没错
为了验证读写
1我们先在这里插入数据,然后查看
2把从库的slave停掉,插入数据,然后查询看看效果(在从库里看不到slave停掉后主从插入的数据)
例如下:
1).
如下就是能插入,也能查询到刚刚插入的数据。但是这样看是看不到读写效果。也可以去从库看看
mysql> insert into user values (00001,‘zhanglong‘); Query OK, 1 row affected (0.07 sec) mysql> select * from user; +------+-----------+ | id | name | +------+-----------+ | 1 | zhanglong | +------+-----------+ 1 row in set (0.10 sec) mysql>
2).
现在在slave从库里停掉slave然后在amoeba插入新的数据,然后查询看看和分别在主库和从库查看表数据
a.在amoeba上插入并且查询(从库已经执行了stop slave命令)
mysql> insert into user values (00002,‘zhangfei‘); Query OK, 1 row affected (0.09 sec) mysql> select * from user; +------+-----------+ | id | name | +------+-----------+ | 1 | zhanglong | +------+-----------+ 1 row in set (0.06 sec)
b.为了更好的
本文出自 “欧阳无情” 博客,谢绝转载!
Mysql主从+Amoeba读写分离