主從複製模型配置過程;mysql
備註: 主節點IP地址是192.168.1.106 從節點的ip地址是192.168.1.107;sql
主節點:數據庫
(1)啓動二進制日誌;vim
1-1 編輯配置文件 vim /etc/my.cnf配置以下所示;服務器
[mysqld]ide
log-bin=master-bin測試
server-id=1spa
innodb-file-per-table=ON線程
skip_name_resolve=ON日誌
systemctl start mariadb.service
1-2 進入mysql使用 mysql命令
1-3 show global variables like '%log%'; 查看一下日誌文件是否已經打開,以下狀態;
| log_bin | ON
1-4 show master logs;查看一下主節點日誌
MariaDB [(none)]> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 16739 |
| mysql-bin.000002 | 516 |
| mysql-bin.000003 | 245 |
+------------------+-----------+
1-5show global variables like '%server%';
MariaDB [(none)]> show global variables like '%server%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| character_set_server | latin1 |
| collation_server | latin1_swedish_ci |
| server_id | 1 |
+----------------------+-------------------+
(2)爲當前節點設置一個全局惟一的ID號;
此配置已經在第一步的時候設置完成了,這裏在註明一下
編輯配置文件vim /etc/my.cnf
[mysqd]
server-id=1
(3)建立有複製權限的用戶帳號;
grant replication slave,replication client on *.* to 'repluser'@'192.168.%.%' identified by 'replpass';
flush privileges;
MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'192.168.%.%' identified by 'replpass';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
從節點;
(1)啓動中繼日誌;
1-1編輯配置文件 vim /etc/my.cnf
[mysqld]
relay-log=relay-log
server-id=7
relay_log_index=relay-log.index
innodb_file_per_table=ON
skip_name_resolve=ON
systemctl start mariadb
mysql
1-2查看一下中繼日誌是否已經啓動show global variables '%log%'; 以下所示
| relay_log | relay-log
1-3查看一下server id 以下所示;
MariaDB [(none)]> show global variables like '%server%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| character_set_server | latin1 |
| collation_server | latin1_swedish_ci |
| server_id | 7 |
+----------------------+-------------------+
(2)爲當前節點設置一個全局惟一的ID號;
編輯配置文件 vim /etc/my.cnf
[mysqld]
server-id=7
設置本身是從節點(備註: 從節點不能進行寫操做)
(3)使用有複製權限的用戶帳號鏈接至主服務器,並啓動複製線程;
3-1MariaDB [(none)]> change master to master_host='192.168.1.106', master_user='repluser', master_password='replpass', master_log_file='mysql-bin.000003', master_log_pos=245;
MariaDB [(none)]> change master to master_host='192.168.1.106', master_user='repluser', master_password='replpass', master_log_file='mysql-bin.000007', master_log_pos=245;
Query OK, 0 rows affected (0.03 sec)
而後將其從服務器的複製狀態打開 slave start ;
MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)
能夠查看狀態:show slave status; 狀態以下所示;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.106
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 245
Relay_Log_File: relay-log.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-bin.000003
Slave_IO_Running: No
Slave_SQL_Running: No
3-2啓動從節點的複製狀態;以下所示 ;
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> SHOW SLAVE STATUS\G 查看一下狀態是否啓動了、以下所示;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.106
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 328
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 612
Relay_Master_Log_File: mysql-bin.000007
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: 328
Relay_Log_Space: 900
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
1 row in set (0.00 sec)
3-3 進行最後的測試工做(備註:由於主服務器纔有寫權限,而後就在主服務器上面建一個數據庫,看是否可以同步到從服務器上面來)若是能,咱們的配置就成功了,以下所示;
先在主服務器上面操做以下;
MariaDB [(none)]> create database zhongguo;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000007 | 419 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| data |
| hellodb |
| li |
| mysql |
| performance_schema |
| test |
| zhongguo |
+--------------------+
8 rows in set (0.00 sec) ( 主服務器建立了一個zhongguo的數據庫)
而後咱們在從服務器上面進行查看一下;以下所示
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| data |
| hellodb |
| mysql |
| performance_schema |
| test |
| zhongguo |
+--------------------+
以上的配置沒有問題,從服務器已經能夠讀取主服務器的數據了,OK,咱們的配置成功了。
備註:出現的問題及解決辦法
160605 9:19:59 [ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file', Error_code: 1236160605 9:19:59 [Note] Slave I/O thread exiting, read up to log 'master-bin.000009', position 245
Slave_IO_Running: No (這個問題按照以下的方法能夠進行解決)
這個問題的解決辦法是將從服務器的受權重新作一邊
1.中止從服務器
stop slave;
reset slave ; 這一步時一點要執行的。
2.執行受權
MariaDB [(none)]> change master to master_host='192.168.1.106', master_user='repluser', master_password='replpass', master_log_file='mysql-bin.000007', master_log_pos=245;
3.而後進行啓動從服務器;
MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.106
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 245
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000007
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: 245
Relay_Log_Space: 817
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
1 row in set (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.106
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 328
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 612
Relay_Master_Log_File: mysql-bin.000007
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: 328
Relay_Log_Space: 900
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
1 row in set (0.00 sec)