MYSQL主從庫同步配置過程

MYSQL主從庫同步配置過程

爲了實現網站數據庫的異地備份,採用了MySQL數據庫主從同步配置,須要兩臺服務器分別做爲主從庫,當主庫發生增刪改等操做,會實時反映到從庫,個人我的服務器配置以下:html

主庫爲centos release 6.7 (final) + mysql 5.5.42,ip爲192.168.3.11mysql

從庫爲centos release 6.7 (final) + mysql 5.5.42,ip爲192.168.3.10sql

數據庫內均無數據,關閉主從庫的iptables(chkconfig iptables off)。數據庫

一、主庫配置

修改主庫MySQL配置文件/ect/my.cnf:centos

[mysqld]
log-bin=mysql-bin
server-id=1
#skip-networking
#bind-address

server-id用來標識MySQL數據庫,log-bin開啓二進制日誌,註釋掉skip-networking和bind-address。 重啓主庫MySQL。服務器

二、帳號配置

在主庫服務器上,新建一個用於同步的帳號。markdown

mysql> create user 'user'@'%.domain.com' identified by 'password';
mysql> grant repliction slave on *.* to 'user'@'%.domain.com';

新建的用戶user@%.domain.com,賦予slave權限。dom

三、主庫狀態

mysql> show master status;
+------------------+-----------+--------------+------------------+
| File             | Position  | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+-----------+--------------+------------------+
| mysql-bin.000009 |       480 |              |                  |
+------------------+-----------+--------------+------------------+
1 row in set (0.00 sec)

記錄下File和Position的值,以後暫時不要操做主庫,防止主庫狀態發生變化。ide

四、從庫配置

修改主庫MySQL配置文件/ect/my.cnf:網站

[mysqld]
log-bin=mysql-bin
server-id=2

注意主從庫的server-id不要相同。

將主庫信息導入到從庫中:

mysql> CHANGE MASTER TO
    ->     MASTER_HOST='192.168.3.11',
    ->     MASTER_USER='usere',
    ->     MASTER_PASSWORD='password',
    ->     MASTER_LOG_FILE='mysql-bin.000009',
    ->     MASTER_LOG_POS=480;

啓動從庫slave同步:

mysql> start slvae;

五、從庫狀態

mysql> show slave status;

查看 Slave_IO_Running | Slave_SQL_Running,若是出現的結果是Yes | Yes,說明mysql主從配置成功完成了。

六、驗證同步

在主庫新建數據庫,導入sql腳本後,查看從庫也作了相應的改變,說明mysql主從同步成功。

參考資料:

Slaves: https://dev.mysql.com/doc/refman/5.5/en/replication-howto-newservers.html

Mysql主從複製實踐手冊: http://www.open-open.com/lib/view/open1365687211734.html

相關文章
相關標籤/搜索