實驗步驟:html
[root@localhost ~]# vim /etc/my.cnf [mysqld] server_id=1 #爲當前節點設置一個全局唯一的ID號 datadir=/mysql/data log_bin=/mysql/logbin/master-bin #主服務器必須開啓二進制日誌 innodb_file_per_table socket=/var/lib/mysql/mysql.sock ... [root@localhost ~]# systemctl restart mariadb.service #從新啓動服務使配置生效 [root@localhost ~]# mysql MariaDB [(none)]> show master status\G; #查看主服務器的爲二進制文件信息,須要在級聯服務器上啓動複製線程 *************************** 1. row *************************** File: master-bin.000006 Position: 8666 Binlog_Do_DB: Binlog_Ignore_DB: MariaDB [(none)]> grant replication slave on *.* to repluser@'172.18.153.%' identified by 'centos'; #建立有複製權限的用戶帳號 ,規定172.18.153/24內的IP能夠鏈接主服務器 #這個地方特別注意先查看二進制文件,而後建立用戶,不然級聯服務器上將二進制文件同步過去之後將找不到用戶
[root@localhost ~]# vim /etc/my.cnf [mysqld] server_id=2 #id號惟一,無前後順序 datadir=/mysql/data innodb_file_per_table log_bin=/mysql/logbin/cascade-bin #開啓級聯服務器的二進制日誌 log_slave_updates #開啓與主服務器的日誌同步,啓用級聯複製 socket=/var/lib/mysql/mysql.sock ... [root@localhost ~]# systemctl restart mariadb.service [root@localhost ~]# mysql MariaDB [(none)]> show master status\G; #獲取級聯服務器的二進制信息,在從服務器上啓動複製線程 *************************** 1. row *************************** File: cascade-bin.000003 Position: 9066 Binlog_Do_DB: Binlog_Ignore_DB: MariaDB [(none)]> help change master to; #啓動的複製線程的語法,能夠幫助help獲取 ... CHANGE MASTER TO MASTER_HOST='master2.mycompany.com', #主服務器的host MASTER_USER='replication', #使用有複製權限的用戶 MASTER_PASSWORD='bigs3cret', #複製權限的用戶的密碼 MASTER_PORT=3306, #數據庫端口 MASTER_LOG_FILE='master2-bin.001', #主服務器上的二進制文件名稱 MASTER_LOG_POS=4, #獲取二進制文件的記錄的起始位置(好比這個日誌,如今建立一個數據庫和一個表,這個建立表和庫的記錄就會在這個日誌的4開始記錄,也就是說在這個日誌裏從4之後存儲的記錄是我剛纔建立了一個數據庫和表) MASTER_CONNECT_RETRY=10; #鏈接的時間 ... MariaDB [(none)]> CHANGE MASTER TO #開啓線程 -> MASTER_HOST='172.18.153.7', -> MASTER_USER='repluser', -> MASTER_PASSWORD='centos', -> MASTER_PORT=3306, -> MASTER_LOG_FILE='master-bin.000006', -> MASTER_LOG_POS=8666, -> MASTER_CONNECT_RETRY=10; MariaDB [(none)]> start slave; #開啓 MariaDB [(none)]> show slave status\G; #查看當前的信息 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.18.153.7 #主服務器的host Master_User: repluser #主服務器上建立的有複製權限的用戶 Master_Port: 3306 Connect_Retry: 10 Master_Log_File: master-bin.000006 #二進制文件名 Read_Master_Log_Pos: 8666 #記錄點 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 1376 Relay_Master_Log_File: master-bin.000006 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: 8666 Relay_Log_Space: 1672 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 #鏈接了一臺主服務器
[root@localhost ~]# vim /etc/my.cnf [mysqld] server_id=3 datadir=/mysql/data read_only #開啓只讀,從服務器的配置都是由級聯服務器複製來的,不須要寫操做,從服務器用來數據庫的讀操做,實現數據庫的讀寫分離 socket=/var/lib/mysql/mysql.sock ... [root@localhost ~]# systemctl restart mariadb.service [root@localhost ~]# mysql MariaDB [(none)]> CHANGE MASTER TO -> MASTER_HOST='172.18.153.17', -> MASTER_USER='repluser', -> MASTER_PASSWORD='centos', -> MASTER_PORT=3306, -> MASTER_LOG_FILE='cascade-bin.000003', -> MASTER_LOG_POS=9066, -> MASTER_CONNECT_RETRY=10; MariaDB [(none)]> start slave; MariaDB [(none)]> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.18.153.17 Master_User: repluser Master_Port: 3306 Connect_Retry: 10 Master_Log_File: cascade-bin.000003 Read_Master_Log_Pos: 9066 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 531 Relay_Master_Log_File: cascade-bin.000003 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: 9066 Relay_Log_Space: 827 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: 2
MariaDB [(none)]> create database test1; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | test1 | +--------------------+
在開啓了複製線程以後會遇到幾處錯誤 Last_IO_Error: error connecting to master 'user@172.18.153.17:3306' - retry-time: 10 retries: 86400 message: Host '172.18.153.27' is not allowed to connect to this MariaDB server 這個錯誤是由於你的用戶權限問題,假如先獲取了二進制日誌,而後進行建立的用戶,這個時候在從服務器會報這個錯誤,由於從服務器沒法根據二進制文件進行數據同步,只要從新配置便可 Last_IO_Error: error connecting to master 'user@172.18.153.7:3306' - retry-time: 10 retries: 86400 message: Can't connect to MySQL server on '172.18.153.7' (113) 這個錯誤是由於你的服務器的防火牆和selinux的問題,還有多是你的網絡問題(檢查你服務器之間的IP是否能夠ping通) Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file' 這個錯誤是由於你的日誌文件的問題,主服務器沒法讀取你的二進制日誌。這個問題先關閉服務,刪除日誌文件,重啓服務讓他自動生成便可。