使用MySQL 5.6,搭建主從複製。關於5.6的安裝,可以參考《MySQL 5.6 rpm安裝方法和碰見的問題》。 主庫創建slave用戶,設置複製許可權, mysql> create user 'slave'@'1.1.1.2' identified by 'root'; Query OK, 0 ...
使用MySQL 5.6,搭建主從複製。關於5.6的安裝,可以參考《MySQL 5.6 rpm安裝方法和碰見的問題》。
主庫創建slave用戶,設置複製許可權,
mysql> create user 'slave'@'1.1.1.2' identified by 'root';
Query OK, 0 rows affected (0.00 sec)
- mysql> grant replication slave on *.* to 'slave'@'1.1.1.2' identified by 'root';
- Query OK, 0 rows affected (0.00 sec)
- 編輯my.cnf配置文件,設置主庫server-id=1,定義需要複製的庫為test,忽略mysql資料庫
- [root@vm-kvm10000-app mysql]# vi /etc/my.cnf
- [mysqld]
- server-id=1
- log-bin=mysql-bin
- binlog_do_db=test
- binlog_ignore_db=mysql
- 重啟主庫MySQL服務,
- [root@vm-kvm10000-app mysql]# service mysql restart
- Shutting down MySQL.. SUCCESS!
- Starting MySQL. SUCCESS!
- 看一下主庫狀態,
- mysql> show master status;
- +------------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +------------------+----------+--------------+------------------+-------------------+
- | mysql-bin.000002 | 120 | test | mysql | |
- +------------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
- 或者
- mysql> show master status \G
- *************************** 1. row ***************************
- File: mysql-bin.000002
- Position: 120
- Binlog_Do_DB: test
- Binlog_Ignore_DB: mysql
- Executed_Gtid_Set:
- 1 row in set (0.00 sec)
- 從庫,編輯my.cnf配置,設置server-id=2,區別於主庫,
- [root@vm-kvm10001-app mysql]# vi /etc/my.cnf
- [mysqld]
- server-id=2
- 重啟MySQL服務,
- [root@vm-kvm10001-app mysql]# service mysql restart
- Shutting down MySQL.. SUCCESS!
- Starting MySQL. SUCCESS!
- 設置主庫信息,
- mysql> change master to master_host='1.1.1.1',master_user='slave',master_password='root',master_log_file='mysql-bin.000001',master_log_pos=120,master_connect_retry=10;
- Query OK, 0 rows affected, 2 warnings (0.03 sec)
- 檢索從庫狀態,
- mysql> show slave status \G;
- *************************** 1. row ***************************
- Slave_IO_State:
- Master_Host: 10.221.3.129
- Master_User: slave
- Master_Port: 3306
- Connect_Retry: 10
- Master_Log_File: mysql-bin.000001
- Read_Master_Log_Pos: 120
- Relay_Log_File: vm-kvm11853-app-relay-bin.000001
- Relay_Log_Pos: 4
- Relay_Master_Log_File: mysql-bin.000001
- Slave_IO_Running: No
- Slave_SQL_Running: No
- 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: 120
- Relay_Log_Space: 120
- 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: NULL
- 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: 0
- Master_UUID:
- Master_Info_File: /var/lib/mysql/master.info
- SQL_Delay: 0
- SQL_Remaining_Delay: NULL
- Slave_SQL_Running_State:
- 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)
- 其中最重要的就是,這兩個參數,要求YES,但此處為NO,
- Slave_IO_Running: No
- Slave_SQL_Running: No
- 檢索錯誤日誌,提示無法找見./performance_schema/cond_instances.frm文件,
- [root@vm-kvm10001-app mysql]# vm-kvm10001-app.err
- 2017-08-29 16:10:37 22933 [ERROR] /usr/sbin/mysqld: Can't find file: './performance_schema/cond_instances.frm' (errno: 13 - Permission denied)
- 看一下其目錄,發現performance_schema文件夾,是root許可權,mysql用戶無法訪問,因此需要修改一下其許可權,
- [root@vm-kvm10001-app mysql]# ls -rlht
- total 109M
- ...
- drwx------ 2 root root 4.0K Aug 29 15:05 performance_schema
- ...
- [root@vm-kvm10001-app mysql]# chown -R mysql:mysql *
- [root@vm-kvm10001-app mysql]# ls -rlht
- total 109M
- ...
- drwx------ 2 mysql mysql 4.0K Aug 29 15:05 performance_schema
- ...
- 重啟服務,
- [root@vm-kvm10001-app mysql]# service mysql restart
- Shutting down MySQL.... SUCCESS!
- Starting MySQL. SUCCESS!
- 再看從庫狀態,
- mysql> show slave status \G
- *************************** 1. row ***************************
- ...
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- ...
- 主庫導出數據,用於導入從庫,首先需要設置讀鎖,避免數據不一致,
- mysql> flush tables with read lock;
- mysql> show master logs;
- +------------------+-----------+
- | Log_name | File_size |
- +------------------+-----------+
- | mysql-bin.000001 | 143 |
- | mysql-bin.000002 | 222 |
- | mysql-bin.000003 | 545 |
- +------------------+-----------+
- 3 rows in set (0.00 sec)
- 執行mysqldump將test庫,導出test.sql文件,
- [root@vm-kvm10000-app mysql]# mysqldump -uroot -p -B test > test.sql
- 然後解鎖表,
- mysql> unlock tables;
- 從庫執行導入,
- [root@vm-kvm10001-app mysql]# mysql -uroot -p < test.sql
- 要確保從庫,這兩個值正確,
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- 此時就完成了主從複製,向主庫插入一條記錄,
- mysql> INSERT INTO test
- -> (id, name)
- -> VALUES
- -> (1, "a");
- Query OK, 1 rows affected (0.00 sec)
- 從庫中可以檢索出來,
- mysql> select * from test;
- +-------+--------+
- | id | name |
- +-------+--------+
- | 1 | a |
- +-------+--------+
- 總結:
- 1. MySQL相關文件、文件夾需要的許可權,可能會因為不正確,例如需要mysql許可權,但卻是root,導致資料庫異常。
- 2. 主從複製,需要關註從庫,這兩個參數值,需要均為YES,出現NO,則可以檢索錯誤日誌,進一步定位。
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes