mysql主从复制+读写分离
Mysql主从复制
环境准备:mysql1.1+mysql1.2(主)+mysql1.3
mysql主服务器:
源码包安装mysql过程省略...
[root@mysql /]
...............................
server_id = 1
log_bin = master-bin
log-slave-updates = true
[root@mysql /]
[root@mysql /]
mysql> grant replication slave on *.* to 'slave'@'192.168.1.%' identified by '123.com';
mysql> flush privileges;
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 408 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql从服务器1.1:
源码包安装mysql过程略..
[root@mysql /]
................
server_id = 2
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
[root@mysql /]
[root@mysql /]
mysql> change master to master_host='192.168.1.2',master_user='slave',master_password='123.com',master_Log_file='master-bin.000001',master_log_pos=408;
mysql> start slave;
mysql> show slave status\G
显示如下:
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.2
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 408
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 284
Relay_Master_Log_File: master-bin.000001
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: 408
Relay_Log_Space: 455
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: ecfee380-2a4a-11eb-814f-000c29d2f2f0
Master_Info_File: /usr/local/mysql/data/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:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
mysql从服务器1.3配置如上! 验证效果: 主服务器上创建数据库 从服务器上进行同步 即为成功!
Mysql读写分离
环境准备: 主从复制环境+amoeba1.4+client1.5
主从mysql分别给amoeba授权
mysql> grant all on *.* to amoeba@'192.168.1.%' identified by '123.com';
amoeba配置
[root@localhost media]
amoeba-mysql-binary-2.2.0.tar.gz jdk-6u14-linux-x64.bin
[root@localhost media]
[root@localhost media]
[root@localhost src]
[root@localhost src]
..................
Do you agree to the above license terms? [yes or no]
yes
Press Enter to continue.....
Done.
[root@localhost src]
[root@localhost src]
添加如下:
export JAVA_HOME=/usr/local/jdk1.6
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin:$PATH:$HOME/bin
export AMOEBA_HOME=/usr/local/amoeba
export PATH=$PATH:$AMOEBA_HOME/bin
[root@localhost src]
安装并且配置amoeba
[root@localhost /]
[root@localhost /]
[root@localhost src]
[root@localhost /]
[root@localhost /]
amoeba start|stop
[root@localhost /]
................................
<property name="authenticator">
<bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator">
<property name="user">amoeba</property>
<property name="password">123.com</property>
<property name="filter">
<bean class="com.meidusa.amoeba.server.IPAccessController">
.........................................
<property name="sqlFunctionFile">${amoeba.home}/conf/functionMap.xml</property>
<property name="LRUMapSize">1500</property>
<property name="defaultPool">master</property>
<property name="writePool">master</property>
<property name="readPool">slaves</property>
<property name="needParse">true</property>
</queryRouter>
编辑dbServers/xml文件
[root@localhost /]
............................
<!-- mysql user -->
<property name="user">amoeba</property>
<property name="password">123.com</property>
</factoryConfig>
..............................
<dbServer name="master" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.1.2</property>
</factoryConfig>
</dbServer>
<dbServer name="slave1" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.1.1</property>
</factoryConfig>
</dbServer>
<dbServer name="slave2" parent="abstractServer">
<factoryConfig>
<!-- mysql ip -->
<property name="ipAddress">192.168.1.3</property>
</factoryConfig>
</dbServer>
<dbServer name="slaves" 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">slave1,slave2</property>
</poolConfig>
</dbServer>
[root@localhost /]
client客户端:
[root@localhost /]
[root@localhost /]
验证效果!
主服务器中在test库中创建一个表zzc,同步到各个从服务器上,并且写入一条内容!当前主从服务器均可查询到~~~ 关闭从服务器的slave功能,并且从服务器分别插入表zzc中一条内容查询会出现从服务器中有刚添加的内容而主服务器中没有。 最后使用客户机远程登陆在test库中zzc表里写入一条内容,在关闭slave功能情况下此内容仅客户机与主服务器可以查询重新开启slave功能则从服务器同步成功!这样,就实现了 MySQL 的读写分离,所有写操作全部在 master主服务器上,用来避免数据的不同步,所有读操作都分摊给了从服务器,用来分担服务器的压力。