導讀 | MySQL複製是一個容許您經過自動從主數據庫複製到從數據庫來輕鬆維護MySQL數據的多個副本的過程。 這可能有助於許多緣由,包括爲數據提供備份,一種在不使用主數據庫的狀況下分析數據的方法,或者只是做爲向外擴展的一種手段。 |
mysql複製示例:一個master將向單個slave發送信息。爲了使進程工做,您將須要兩個IP地址:主服務器之一和從屬設備之一。html
本教程將使用如下IP地址:mysql
12.34.56.789-主數據庫 12.23.34.456-從數據庫
本文假設您具備sudo權限的用戶而且已安裝MySQL。 若是你沒有mysql,你能夠用這個命令安裝:linux
sudo apt-get install mysql-server mysql-client
第一步 - 配置主數據庫sql
打開主服務器上的mysql配置文件。shell
sudo nano /etc/mysql/my.cnf 一旦進入該文件,咱們須要進行一些更改。 第一步是找到以下所示的部分,將服務器綁定到本地主機: bind-address = 127.0.0.1 將標準IP地址替換爲服務器的IP地址。 bind-address = 12.34.56.789 下一個配置更改是指位於[mysqld]部分中的server-id。 您能夠爲此點選擇任何數字(可能更容易從1開始),但該數字必須是惟一的,而且不能與複製組中的任何其餘服務器標識匹配。 我要去打電話這個1。 確保此行已取消註釋。 server-id = 1 移動到log_bin行。 這是保存複製的真實細節的地方。 從屬程序將複製在日誌中註冊的全部更改。 對於這一步,咱們只須要取消註釋引用log_bin的行: log_bin = /var/log/mysql/mysql-bin.log 最後,咱們須要指定將在從服務器上覆制的數據庫。 您能夠經過爲全部您須要的數據庫重複此行,包括多個數據庫。 binlog_do_db = newdatabase 完成全部更改後,繼續保存並退出配置文件。 刷新MySQL。
sudo service mysql restart
接下來的步驟將在MySQL shell中進行,自己。數據庫
打開MySQL shell。bash
mysql -u root -p
咱們須要給從屬權限。 您可使用此行命名您的從屬並設置其密碼。 命令應採用如下格式:服務器
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
下一部分是有點bit。。 爲了完成任務,你須要在除了你已經使用了幾步倒行一打開一個新窗口或標籤 。app
在當前標籤頁切換到「newdatabase」。ide
USE newdatabase;
接下來,鎖定數據庫以防止任何新的更改:
FLUSH TABLES WITH READ LOCK;
而後輸入:
SHOW MASTER STATUS;
你會看到一個表應該看起來像這樣:
mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 107 | newdatabase | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
這是從數據庫將開始複製的位置。 記錄這些數字,他們將在之後有用。
若是在同一個窗口中進行任何新的更改,數據庫將自動解鎖。 所以,您應該打開新的選項卡或窗口,而後繼續下一步。
繼續數據庫仍然鎖定,在新窗口中使用mysqldump導出數據庫(確保您在bash shell中而不是在MySQL中鍵入此命令)。
mysqldump -u root -p --opt newdatabase > newdatabase.sql
如今,返回到您的原始窗口,解鎖數據庫(使它們可寫入)。 經過退出shell完成。
UNLOCK TABLES; QUIT;
如今你已經完成了master數據庫的配置。
第二步 - 配置從數據庫
配置主數據庫以後。 你能夠把它放在一邊,咱們如今將開始配置從數據庫。
登陸到從服務器,打開MySQL shell並建立要從主服務器複製的新數據庫(而後退出):
CREATE DATABASE newdatabase; EXIT; 導入先前從主數據庫導出的數據庫。 mysql -u root -p newdatabase Now we need to configure the slave configuration in the same way as we did the master: sudo nano /etc/mysql/my.cnf We have to make sure that we have a few things set up in this configuration. The first is the server-id. This number, as mentioned before needs to be unique. Since it is set on the default (still 1), be sure to change it’s something different. server-id = 2 Following that, make sure that your have the following three criteria appropriately filled out: relay-log = /var/log/mysql/mysql-relay-bin.log log_bin = /var/log/mysql/mysql-bin.log binlog_do_db = newdatabase You will need to add in the relay-log line: it is not there by default. Once you have made all of the necessary changes, save and exit out of the slave configuration file. Restart MySQL once again: sudo service mysql restart The next step is to enable the replication from within the MySQL shell. Open up the the MySQL shell once again and type in the following details, replacing the values to match your information: CHANGE MASTER TO MASTER_HOST='12.34.56.789',MASTER_USER='slave_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS= 107; This command accomplishes several things at the same time: It designates the current server as the slave of our master server. It provides the server the correct login credentials Last of all, it lets the slave server know where to start replicating from; the master log file and log position come from the numbers we wrote down previously. With that—you have configured a master and slave server. Activate the slave server: START SLAVE; You be able to see the details of the slave replication by typing in this command. The \G rearranges the text to make it more readable. SHOW SLAVE STATUS\G If there is an issue in connecting, you can try starting slave with a command to skip over it: SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; SLAVE START; All done. See More MySQL replication has a lot different options, and this was just a brief overview. If you have any further questions about the specific capabilities of MySQL, feel free to post your questions in our Q&A Forum and we’ll be happy to answer them. By Etel Sverdlov
如今咱們須要以與咱們作主機相同的方式配置從機配置:
sudo nano /etc/mysql/my.cnf 咱們必須確保咱們在這個配置中設置了一些東西。 第一個是服務器標識。 這個數字,如前所述須要是惟一的。 由於它被設置爲默認(仍然是1),必定要改變它的東西不一樣。 server-id = 2 以後,請確保您已正確填寫如下三個條件: relay-log = /var/log/mysql/mysql-relay-bin.log log_bin = /var/log/mysql/mysql-bin.log binlog_do_db = newdatabase
您將須要在中繼日誌行中添加:默認狀況下不存在。 一旦完成全部必要的更改,保存並退出從配置文件。
再次從新啓動MySQL:
sudo service mysql restart
下一步是在MySQL shell中啓用複製。
再次打開MySQL shell,輸入如下詳細信息,替換值以匹配您的信息:
CHANGE MASTER TO MASTER_HOST='12.34.56.789',MASTER_USER='slave_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS= 107;
此命令同時完成幾個事情:
它將當前服務器指定爲咱們的主服務器的從屬。
它爲服務器提供正確的登陸憑據
最後,它讓從服務器知道從哪裏開始複製; 主日誌文件和日誌位置來自咱們以前寫下的數字。
這樣,您已經配置了主服務器和從服務器。
激活從服務器:
START SLAVE;
經過鍵入此命令,您能夠看到從複製的詳細信息。 \ G從新排列文本,使其更易讀。
SHOW SLAVE STATUS\G
若是在鏈接中存在問題,能夠嘗試使用命令啓動從器件以跳過它:
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; SLAVE START;
全作完了。