Mysql基於binlog主從複製配置,mysqlbinlog主從
上次配置mysql的主從複製還是年初的時候,沒想到現在又開始使用mysql了。話不多說,進入正題
Master-Slave 搭建環境:Master:Os: rhel-server-6.3-x86_64Mysql: MySQL -5.5.35-1虛擬機器:Virtual BOXIp:192.168.56.12Slave:Os: rhel-server-6.3-x86_64Mysql: MySQL -5.5.35-1虛擬機器:Virtual BOXIp:192.168.56.13
總體步驟:
1、 修改Master設定檔(my.cnf)
2、 在Master上建立一個有複製許可權的使用者
3、 複製Master上的資料庫到Slave
4、 修改Slave設定檔(my.cnf)
5、 Slave初始化複製
6、 測試
7、 常見問題
1、 修改Master設定檔(my.cnf)
[mysqld] server-id=1 //必須是伺服器唯一id,預設是0, log-bin=mysql-bin //必須啟用二進位日誌replicate-do-db=zbdba
2、 在Master上建立一個有複製許可權的使用者
mysql>create user zbdba; mysql>grant replication slave on *.* to zbdba identified by '123456';
3、 複製Master上的資料庫到Slave
mysql>flush tables with read lock; mysql> show master status -> ;+-----------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+-----------------+----------+--------------+------------------+| ogg1-bin.000003 | 107 | | |+-----------------+----------+--------------+------------------+1 row in set (0.00 sec)mysql>quit[root@ogg /]# /usr/bin/mysqldump zbdba -uroot -pmysql --opt | mysql zbdba -uroot -pmysql -h 192.168.56.13需要在slave中授權:GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.56.13' IDENTIFIED BY 'mysql' WITH GRANT OPTION;mysql>unlock tables;
4、 修改Slave設定檔(my.cnf)
[mysqld] server-id=2 //必須是伺服器唯一id,預設是0, log-bin=mysql-bin //必須啟用二進位日誌replicate-do-db=zbdba
5、 Slave初始化複製
mysql>slave stop; mysql>CHANGE MASTER TO MASTER_HOST='192.168.56.12',MASTER_USER='zbdba',MASTER_PASSWORD='123456',MASTER_LOG_FILE='ogg1-bin.000003',MASTER_LOG_POS=107;mysql> show slave status\G;*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.56.12 Master_User: zbdba Master_Port: 3306 Connect_Retry: 60 Master_Log_File: ogg1-bin.000003 Read_Master_Log_Pos: 107 Relay_Log_File: ogg2-relay-bin.000002 Relay_Log_Pos: 252 Relay_Master_Log_File: ogg1-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: zbdba
6、 測試
master:
mysql> show tables;+-----------------+| Tables_in_zbdba |+-----------------+| guestbook || test || test1 |+-----------------+
create table test2 select * from test1;
mysql> show tables;+-----------------+| Tables_in_zbdba |+-----------------+| guestbook || test || test1 || test2 |+-----------------+
salve:
mysql> show tables;+-----------------+| Tables_in_zbdba |+-----------------+| guestbook || test || test1 || test2 |+-----------------+4 rows in set (0.00 sec)
如果需要配置相互主從的關係,只需要在slave資料庫上面建立對應使用者,原master上面做一下初始化就行了。
7、 常見問題
/etc下面沒有對應的my.cnf檔案
解決方案:
如果mysql使用rpm包安裝的,/etc下沒有對應的my.cnf檔案,這時候只需要複製一個/usr/share/mysql目錄下的.cnf檔案到/etc目錄,並改名為my.cnf即可。
Server-id參數無法修改
解決方案:
看my.cnf中是否存在相同的server-id=1
MYSQL主備出現這個錯誤: ERROR 1201 (HY000):
解決方案:
mysql> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> reset slave;
Query OK, 0 rows affected,(0.00 sec)
ERROR 1130 (HY000): Host '*.*.*.*' is not allowed to connect to this MySQL server
解決方案:
如果你想允許使用者myuser從ip為192.168.1.3的主機串連到mysql伺服器,並使用mypassword作為密碼
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'192.168.1.3' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;