MySQL主從複製,是一個MySQL的羣集,可以很好的解決的單點故障,並且可以進行讀寫分離來減輕數據庫的壓力。很多情況下主服務器僅作爲寫入數據服務器,而構建多個從節點來進行數據讀取。
儘量保持主從數據庫版本一致。
如果不一致,從數據庫版本要高於主庫的數據庫版本。
創建數據存放目錄和binlog存放目錄,分開存放,建議是安裝到不同的硬盤上,我這裏就是安裝在不同的目錄上了。
# mkdir /mydata # mkdir /mylog
將源碼包複製到另外一臺主機。
# scpmariadb-5.5.36-linux-x86_64.tar.gz 192.168.122.136:/root [email protected]'spassword: mariadb-5.5.36-linux-x86_64.tar.gz 100% 212MB 11.2MB/s 00:19
本地主機解壓,創建mysql賬戶和修改數據目錄,日誌目錄屬主屬組。
# tar xfmariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local/ # useradd -r -s/sbin/nologin mysql # chownmysql:mysql /my* # ls -ld /my* drwxr-xr-x 2 mysqlmysql 4096 Sep 20 14:35 /mydata drwxr-xr-x 2 mysqlmysql 4096 Sep 20 14:36 /mylog
鏈接到mariadb到mysql,創建配置文件存放路徑,修改屬主,屬組。
# ln -smariadb-5.5.36-linux-x86_64/ mysql # mkdir /etc/mysql # chown -Rmysql:mysql mysql/
複製配置文件及Sys腳本到相對應的目錄。
# cpsupport-files/my-large.cnf /etc/mysql/my.cnf # cpsupport-files/mysql.server /etc/init.d/mysqld
修改數據目錄存放位置和binlog日誌存放位置。
# vim/etc/mysql/my.cnf datedir =/mydata/data log-bin=/mylog/master-bin
將mysqld添加到系統服務,並初始化。
# chmod +x/etc/init.d/mysqld # chkconfig --addmysqld #./scripts/mysql_install_db --user=mysql --datadir=/mydata/data
啓動mysql,檢查端口。
# service mysqld start Starting MySQL..SUCCESS! # ss -tnl LISTEN 0 50 *:3306 *:
檢查數據目錄和日誌目錄。
# ls /my* /mydata: data /mylog: master-bin.000001 master-bin.000002 master-bin.000003 master-bin.000004 master-bin.index
另外一臺主機相同配置,可以不指定二進制日誌位置,但是需要創建relaylog日誌目錄,修改serverID。
# mkdir /relaylog # vim/etc/mysql/my.cnf datadir =/mydata/data #log-bin=mysql-bin #binlog_format=mixed server-id = 10 relay-log = /relaylog/relay-log # service mysqldstart Starting MySQL...SUCCESS! # ss -tnl LISTEN 0 50 *:3306 *:*
創建授權賬戶。
# mysql MariaDB[(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TOubu@'192.168.122.136' IDENTIFIED BY '123456'; Query OK, 0 rowsaffected (0.00 sec) MariaDB[(none)]> FLUSH PRIVILEGES; Query OK, 0 rowsaffected (0.00 sec)
授權後登錄從服務器測試連接,如果連接不上需要馬上檢查問題,而不是繼續做下去。不然也會失敗。
# mysql -u ubu -h192.168.122.134 -p Enter password: MariaDB[(none)]>
回到主服務器,查看日誌位置。
MariaDB[(none)]> SHOW MASTER STATUS; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB |Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ |master-bin.000004 | 492 | | | +-------------------+----------+--------------+------------------+ 1 row in set (0.01sec) 因爲是開始重啓失敗,重新初始化了一次,導致日誌已經走到000004的位置。
切換到從服務器,開始複製。
MariaDB[(none)]> CHANGE MASTER TOMASTER_HOST='192.168.122.134',MASTER_USER='ubu',MASTER_PASSWORD='123456',MASTER_LOG_FILE='master-bin.000004',MASTER_LOG_POS=492; Query OK, 0 rowsaffected (0.15 sec) MariaDB[(none)]> START SLAVE; MariaDB[(none)]> SHOW SLAVE STATUS\G ***************************1. row *************************** Slave_IO_State: Waiting formaster to send event Master_Host: 192.168.122.134 Master_User: ubu Master_Port: 3306 Connect_Retry: 60 Master_Log_File:master-bin.000004 Read_Master_Log_Pos: 492 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 530 Relay_Master_Log_File:master-bin.000004 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: 492 Relay_Log_Space: 818 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.00sec) Slave_IO_Running: Yes Slave_SQL_Running: Yes
現在已經開始同步,可以看到relay-log已經到了000002了,開始連接的時候是因爲少寫了一個字符,導致同步的時候IO_THREAD狀態爲no。重新連接後就正常了。
在主庫創建一個數據庫,看從庫能否同步。
MariaDB [(none)]> CREATE DATABASEtestdb; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | testdb | +--------------------+ 5 rows in set (0.00 sec)
從庫查看
MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | testdb | +--------------------+ 5 rows in set (0.01 sec)
主從複製已經完成。如果是一個已經寫入過數據的數據庫同步,需要把主庫的數據導出導入到從庫, 然後才能做主從複製。
限制從服務器寫如數據,變爲只讀服務器,進行數據讀寫分離。
更改slave的全局服務器變量read-only爲yes。
MariaDB [(none)]> SET GLOBALread_only=1;
創建一個普通賬戶,測試能否創建數據。
主庫創建,從庫會直接同步過來。
MariaDB [(none)]> GRANTCREATE,UPDATE,INSERT ON testdb.* TO testuser@'192.168.%.%' IDENTIFIED BY'testuser'; Query OK, 0 rows affected (0.00 sec)
切換到從庫,查看賬戶是否同步,看看是否修改從庫爲read-only後數據是否正常同步。
MariaDB [mysql]> SELECT User,Host FROMuser; +----------+---------------+ | User | Host | +----------+---------------+ | root | 127.0.0.1 | | testuser | 192.168.%.% | | root | ::1 | | | localhost | | root | localhost | | | www.zhang.com | | root | www.zhang.com | +----------+---------------+ 7 rows in set (0.00 sec)
使用testuser登錄從數據庫,檢查能否創建數據。
# mysql -u testuser -h 192.168.122.136 -p MariaDB [(none)]> CREATE DATABASEtestuser; ERROR 1290 (HY000): The MariaDB server isrunning with the --read-only option so it cannot execute this statement
這裏只是限制了普通用戶創建數據庫,但是以root用戶登錄的時候,還是可以正常創建,插入數據 的。
MySQL雙主模式:
三臺服務器,兩個主節點。兩個從節點,一臺服務器充當master和slave服務器,當主服務器故障後,可以切換到另外一臺服務器,保證數據正常寫入同步。
刪除原先的數據,重新初始化,但是A節點需要創建或者指定relay-log存放位置,而B節點需要指定binlog存放位置。這裏就稱呼爲AB節點了。
下面貼出來需要修改的部分,爲A節點配置。
# vim/etc/mysql/my.cnf datadir =/mydata/data log-bin=/mylog/master-bin relay-log=/myrelailogs/master-relay-log server-id = 1 auto-increment-offset = 1 auto-increment-increment= 2
B節點配置文件修改。
# vim/etc/mysql/my.cnf datadir =/mydata/data server-id = 10 relay-log = /relaylog/relay-log log-bin =/mybinlog/master2-bin auto-increment-offset = 2 auto-increment-increment= 2
AB節點相同操作。
MariaDB[(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TOubu@'192.168.%.%' IDENTIFIED BY '123456';
A節點連接B節點開始複製。
MariaDB[(none)]> CHANGE MASTER TOMASTER_HOST='192.168.122.136',MASTER_USER='ubu',MASTER_PASSWORD='123456',MASTER_LOG_FILE='master2-bin.000003',MASTER_LOG_POS=418; Query OK, 0 rowsaffected (0.01 sec) MariaDB[(none)]> START SLAVE; Query OK, 0 rowsaffected (0.00 sec) MariaDB[(none)]> SHOW SLAVE STATUS\G ***************************1. row *************************** Slave_IO_State: Waiting formaster to send event Master_Host: 192.168.122.136 Master_User: ubu Master_Port: 3306 Connect_Retry: 60 Master_Log_File:master2-bin.000003 Read_Master_Log_Pos: 418 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 531 Relay_Master_Log_File:master2-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes
B節點連接A節點開始複製。
MariaDB[(none)]> CHANGE MASTER TO MASTER_HOST='192.168.122.134',MASTER_USER='ubu',MASTER_PASSWORD='123456',MASTER_LOG_FILE='master1-bin.000003',MASTER_LOG_POS=418; Query OK, 0 rowsaffected (0.13 sec) MariaDB[(none)]> START SLAVE; Query OK, 0 rowsaffected (0.00 sec) MariaDB[(none)]> SHOW SLAVE STATUS\G ***************************1. row *************************** Slave_IO_State: Waiting formaster to send event Master_Host: 192.168.122.134 Master_User: ubu Master_Port: 3306 Connect_Retry: 60 Master_Log_File:master1-bin.000003 Read_Master_Log_Pos: 418 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 531 Relay_Master_Log_File:master1-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes
雙方都已經開始連接複製。
分別在AB節點數據庫創建數據,看看是否同步。
現在A節點數據庫庫創建數據庫,B節點數據庫庫查看。
MariaDB[(none)]> CREATE DATABASE testdb; Query OK, 1 rowaffected (0.01 sec) B節點數據庫庫查看 MariaDB[(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ |information_schema | | mysql | |performance_schema | | test | | testdb | +--------------------+ 5 rows in set(0.00 sec)
B節點刪除testdb,A節點查看。
MariaDB[(none)]> DROP DATABASE testdb; Query OK, 0 rowsaffected (0.06 sec) A節點查看 MariaDB[(none)]> CREATE DATABASE testdb; Query OK, 1 rowaffected (0.01 sec) MariaDB[(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ |information_schema | | mysql | |performance_schema | | test | +--------------------+ 4 rows in set(0.01 sec)
雙主模式完成了。
而如果想要做M-M/S-S模型架構,只需要在B節點授權,C節點連接複製即可。並且C節點爲只讀數據庫。
總結:
導致slave_IO_Running 爲connecting 的原因主要有以下 3 個方面:
1、網絡不通
2、密碼不對
3、pos不對
出現這種問題的原一般先檢查寫入是字母是否正確,其次去檢查網絡原因。