在MySQL中設置主從複製入門實例

mysql複製示例:一個master將向單個slave發送信息。為了使進程工作,您將需要兩個IP地址:主服務器之一和從屬設備之一。

本教程將使用以下IP地址:

12.34.56.789-主數據庫12.23.34.456-從數據庫

本文假設您具有sudo權限的用戶並且已安裝MySQL。 如果你沒有mysql,你可以用這個命令安裝:

sudo apt-get install mysql-server mysql-client

第一步 - 配置主數據庫

打開主服務器上的mysql配置文件。

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。

mysql -u root -p

我們需要給從屬權限。 您可以使用此行命名您的從屬並設置其密碼。 命令應採用以下格式:

GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'password';FLUSH PRIVILEGES;

下一部分是有點bit。。 為了完成任務,你需要在除了你已經使用了幾步倒行一打開一個新窗口或標籤 。

在當前標籤頁切換到“newdatabase”。

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.cnfWe 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 = 2Following that, make sure that your have the following three criteria appropriately filled out:relay-log = /var/log/mysql/mysql-relay-bin.loglog_bin = /var/log/mysql/mysql-bin.logbinlog_do_db = newdatabaseYou 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 restartThe 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 credentialsLast 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 MoreMySQL 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.loglog_bin = /var/log/mysql/mysql-bin.logbinlog_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; 

全做完了。


分享到:


相關文章: