mysql>slave start; mysql>show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.1 Master_User: test Master_Port: 3306 Connect_Retry: 60 Master_Log_File: updatelog.000001 Read_Master_Log_Pos: 106 Relay_Log_File: onlinevc-relay-bin.000013 Relay_Log_Pos: 1069 Relay_Master_Log_File: updatelog.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: data Replicate_Ignore_DB: mysql 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: 106 Relay_Log_Space: 1681 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)
mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | updatelog.000012 | 15016 | data | mysql | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
BEGIN #Routine body goes here... DECLARE i INT DEFAULT 0; WHILE i < 1000000 DO INSERT INTO test values(i, concat('zhangsan',i)); SET i = i + 1; END WHILE; END
<dbServer name="abstractServer" abstractive="true"> <factoryConfig class="com.meidusa.amoeba.mysql.net.MysqlServerConnectionFactory"> <property name="manager">${defaultManager}</property> <property name="sendBufferSize">64</property> <property name="receiveBufferSize">128</property> <!-- mysql port --> <property name="port">3306</property> <!-- mysql schema --> <property name="schema">dbname</property> <!-- mysql user --> <property name="user">root</property> <!-- mysql password --> <property name="password">root</property> </factoryConfig> <poolConfig class="com.meidusa.amoeba.net.poolable.PoolableObjectPool"> <property name="maxActive">500</property> <property name="maxIdle">500</property> <property name="minIdle">10</property> <property name="minEvictableIdleTimeMillis">600000</property> <property name="timeBetweenEvictionRunsMillis">600000</property> <property name="testOnBorrow">true</property> <property name="testWhileIdle">true</property> </poolConfig> </dbServer>
此部分定義真實mysql服務器的端口,數據庫名稱,mysql用戶及密碼。
主從數據庫定義:
<dbServer name="Master" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.0.1</property> </factoryConfig> </dbServer> <dbServer name="Slave1" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.0.2</property> </factoryConfig> </dbServer> <dbServer name="Slave2" parent="abstractServer"> <factoryConfig> <!-- mysql ip --> <property name="ipAddress">192.168.0.3</property> </factoryConfig> </dbServer> <dbServer name="virtualSlave" 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>
此部分定義主服務器,從服務器,及從服務器連接池。這裡只定義數據庫地址,它們的用戶及密碼就是上面的abstractServer裡的設置。注意用來連接真實mysql服務器的用戶必須擁有遠程連接權限。
amoeba連接驗證配置:
<property name="authenticator"> <bean class="com.meidusa.amoeba.mysql.server.MysqlClientAuthenticator"> <property name="user">root</property> <property name="password">root</property> <property name="filter"> <bean class="com.meidusa.amoeba.server.IPAccessController"> <property name="ipFile">${amoeba.home}/conf/access_list.conf</property> </bean> </property> </bean> </property>
這裡定義連接amoeba時用來驗證的用戶及密碼。
讀寫分離配置:
<property name="defaultPool">Master</property> <property name="writePool">Master</property> <property name="readPool">virtualSlave</property>
defaultPool:配置了默認的數據庫節點,一些除了SELECTUPDATEINSERTDELETE的語句都會在defaultPool執行。
writePool :配置了數據庫寫庫,通常配為Master,如這裡就配置為之前定義的Master數據庫。
readPool :配置了數據庫讀庫,通常配為Slave或者Slave組成的數據庫池,如這裡就配置之前的virtualSlave數據庫池。
啟動命令:
amoeba start (前台啟動可以查看報錯信息執行:/usr/local/amoeba/bin/amoeba可能會遇到以下錯誤:
此命令以前台的方式啟動,會輸出啟動時的信息,檢查沒有錯誤信息後,中斷,並後台運行:
nohup amoeba start &(後台啟動命令)
進行mysql操作的時候查看nohup日志發現Could not create a validated object, cause: ValidateObject failed
無法連接,主要可能是:
dbServer.xml中的數據庫密碼配置 是否出錯
數據庫名稱是否出錯 scheme比如 我想把mysql從庫改為主庫,這時候你更改完所有配置文件,你在主庫上邊mysql> show master status查看時候,可以看到以前打的連接狀態,怎麼辦?
需要登錄mysql 執行以前命令
mysql> slave stop;
mysql>change master to master_host=' '; 就可以了
mysql 主從集群做好了後,這時候一不小心從主庫上刪掉一個數據庫數據,從庫沒有這個數據庫,這時候 Slave_SQL_Running: NO,會變成NO,解決方法有兩種如下
記錄File和Position對應的值。
mysql> show master status; +------------------+-----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+-----------+--------------+------------------+ | mysql-bin.000013 | 330748356| | | +------------------+-----------+--------------+------------------+ 1 row in set (0.00 sec)
到slave服務器上執行手動同步:
mysql> slave stop; mysql> change master to > master_log_file='mysql-bin.000013', > master_log_pos=330748356; 1 row in set (0.00 sec) mysql> slave start; 1 row in set (0.00 sec)
再次查看slave狀態發現:
Slave_IO_Running: Yes Slave_SQL_Running: Yes ... Seconds_Behind_Master: 0
解決辦法二:
mysql> slave stop; mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1; mysql> slave start;