MySQL資料庫主從及主主複製配置示範
實驗系統:CentOS 6.6_x86_64
實驗前提:提前準備好編譯環境,防火牆和selinux都關閉
實驗說明:本實驗共有2台主機,IP分配如拓撲
實驗軟體:mariadb-10.0.20
實驗拓撲:
一、安裝mysql
1.編譯安裝mariadb:
tar xf mariadb-10.0.20-linux-x86_64.tar.gz -C /usr/local/
cd /usr/local/
ln -sv mariadb-10.0.20-linux-x86_64 mysql
mkdir -pv /mydata/data
useradd -r mysql
chown -R mysql.mysql /mydata/data/
cd mysql/
chown -R root.mysql .
scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
cp support-files/my-large.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
2.編輯設定檔:
vim /etc/my.cnf
--------------------------------
datadir = /mydata/data //增加此行
--------------------------------
service mysqld start
/usr/local/mysql/bin/mysql //串連測試
二、主從複製
實驗中我先讓19.43當主伺服器,19.48為從伺服器。
1.主伺服器操作:
(1)編輯my.cnf,啟用二進位日誌(如果定義到其他路徑,請給予其mysql許可權):
log-bin=/mydata/data/mysql-bin
(2)定義server-id:
server-id = 1
(3)建立有複製許可權的帳號:
/usr/local/mysql/bin/mysql
-----------------------------------------
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO slave@192.168.19.48 IDENTIFIED BY '123456'; //遵循最小許可權原則
FLUSH PRIVILEGES;
(4)記錄最後的二進位日誌資訊,CHANGE MASTER時會用到:
SHOW MASTER LOGS;
2.從伺服器操作:
(1)啟動中繼日誌(如果定義到其他路徑,請給予其mysql許可權):
relay_log=/mydata/data/relay-log
(2)從伺服器用中繼日誌就足夠了,關閉二進位日誌,減少磁碟IO:
#log-bin=mysql-bin //將其注釋
#binlog_format=mixed
(3)定義server-id:
server-id = 2 //不能與主伺服器相同
(4)配置CHANGE MASTER:
/usr/local/mysql/bin/mysql
-----------------------------------------
CHANGE MASTER TO MASTER_HOST='192.168.19.43',MASTER_USER='slave',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000002',MASTER_LOG_POS=326;
SHOW SLAVE STATUS\G //查看狀態
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.19.43
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 326
Relay_Log_File: relay-log.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000002
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: 326
Relay_Log_Space: 248
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_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
1 row in set (0.00 sec)
(5)啟動io thread以及sql thread:
START SLAVE;
3.在主伺服器建立資料庫,從伺服器查看:
CREATE DATABASE jason; //主伺服器建立資料庫
SHOW DATABASES; //從伺服器查看
4.如果主要資料庫不是建立立的,而是使用過一段時間,且裡面已經有不少資料的情況下,需要先把主伺服器資料匯出,再匯入到從伺服器,然後根據上面的步驟進行主從複製,這裡將不再示範。
匯出資料庫命令參考:mysqldump --all-databases --lock-all-tables --master-data=2 > name.sql
三、半同步複製
1.什麼是半同步複製?
因為開啟這個功能之後,主伺服器只等待多個從伺服器中的指定的一台從伺服器複製成功,然後才進行其他寫操作,使這個從伺服器和主伺服器上的資料完全同步,而並不管其他的從伺服器。這在一定程度上就保證了我們資料的安全性。這個功能需要打由google貢獻的補丁,它是以外掛程式的形式存在的。補丁存放的位置在:/usr/local/mysql/lib/plugin/
2.主伺服器打補丁:
/usr/local/mysql/bin/mysql
---------------------------------------------
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
SHOW GLOBAL VARIABLES LIKE '%semi%'; //查看狀態
3.主伺服器修改配置:
SET GLOBAL rpl_semi_sync_master_enabled=1;
SET GLOBAL rpl_semi_sync_master_timeout=1000;
4.從伺服器打補丁:
/usr/local/mysql/bin/mysql
---------------------------------------------
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
SHOW GLOBAL VARIABLES LIKE '%semi%';
5.從伺服器修改配置:
SET GLOBAL rpl_semi_sync_slave_enabled=1;
STOP SLAVE;
START SLAVE;
6.主伺服器查看狀態:
SHOW GLOBAL STATUS LIKE '%semi%';
四、主主複製
1.由於剛才做了許多操作,為了不影響之後的實驗,我們先將資料庫進行重設:
service mysqld stop
rm -rf /mydata/data/* //刪除資料庫檔案
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/mydata/data/ //初始化mysql
2.修改my.cnf:
19.43上:
vim /etc/my.cnf
-------------------------------------------
[mysqld]
server-id=1
log-bin=/mydata/data/mysql1-bin //開啟二進位日誌
relay_log=/mydata/data/relay-log //開始中繼日誌
auto-increment-increment = 2 //每次增長的數值
auto-increment-offset = 1 //開始的數值
19.48上:
vim /etc/my.cnf
-------------------------------------------
[mysqld]
server-id=2
log-bin=/mydata/data/mysql2-bin
relay_log=/mydata/data/relay-log
auto-increment-increment = 2
auto-increment-offset = 2
3.建立具有複製許可權的使用者:
19.43上:
service mysqld start
/usr/local/mysql/bin/mysql
------------------------------------------
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO mmm@192.168.19.48 IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
19.48上:
service mysqld start
/usr/local/mysql/bin/mysql
------------------------------------------
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO mmm@192.168.19.43 IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
4.查看二進位位置:
19.43上:
SHOW MASTER LOGS;
19.48上使用同樣的命令:
5.配置CHANGE MASTER:
19.43上:
CHANGE MASTER TO MASTER_HOST='192.168.19.48',MASTER_USER='mmm',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql2-bin.000001',MASTER_LOG_POS=642;
START SLAVE;
19.48上:
CHANGE MASTER TO MASTER_HOST='192.168.19.43',MASTER_USER='mmm',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql1-bin.000001',MASTER_LOG_POS=642;
START SLAVE;
6.查看狀態:
SHOW SLAVE STATUS\G
之後的複製示範這裡不再示範,請大家自行嘗試。至此,所有實驗示範完畢,謝謝!
--------------------------------------分割線 --------------------------------------
Ubuntu下Nginx做負載實現高效能WEB伺服器5---MySQL主主同步
生產環境MySQL主主同步主鍵衝突處理
MySQL主從失敗 錯誤Got fatal error 1236
MySQL主從複製,單台伺服器上實施
搭建MySQLProxy 伺服器實現讀寫分離+主從同步
MySQL 5.5 主從雙向同步
MySQL 5.5主從同步排錯
MySQL主從複製非同步半同步執行個體
--------------------------------------分割線 --------------------------------------
本文永久更新連結地址: