最簡單的mysql主從複製

一、準備:

1、兩臺物理服務器,ip地址如下:
①、192.168.1.1
②、192.168.1.2

兩臺服務器上都必須關閉SELinux

  • 關閉selinux:
    1、永久有效:修改/etc/sysconfig/selinux
    將文本中的SELINUX=enforcing,改爲SELINUX=disabled。然後重啓
    2、即時有效:setenforce 0
    3、查看狀態:getenforce 如果爲Permissive或者Disabled則已關閉

2、在兩臺服務器上都裝上mysql服務器,我這裏用的是Percona Server
安裝方法可以見我的博客
兩個mysql服務端口都是39847,並且已開放端口
用戶名、密碼都是:root、123456

二、Mysql主服務器配置

192.168.1.1這臺服務器爲主服務器

  • 第一步:修改my.conf文件:
    在[mysqld]段下添加:
# 進行復制的庫(可以不用)
binlog-do-db=db1
# 忽略複製的庫(可以不用)
binlog-ignore-db=mysql
#啓用二進制日誌
log-bin=mysql-bin
#服務器唯一ID,一般取IP最後一段
server-id=134

全部配置如下:

# Generated by Percona Configuration Wizard (http://tools.percona.com/) version REL5-20120208
# Configuration name master generated for [email protected] at 2018-11-28 09:40:47

[mysql]

# CLIENT #
port                           = 39847
socket                         = /var/lib/mysql/mysql.sock

[mysqld]

# GENERAL #
user                           = mysql
default-storage-engine         = InnoDB
socket                         = /var/lib/mysql/mysql.sock
pid-file                       = /var/lib/mysql/mysql.pid
server_id                      = 10975
port                           = 39847

# MyISAM #
key-buffer-size                = 32M
myisam-recover-options         = FORCE,BACKUP

# SAFETY #
max-allowed-packet             = 16M
max-connect-errors             = 1000000

# DATA STORAGE #
datadir                        = /var/lib/mysql/

# BINARY LOGGING #
#啓用二進制日誌
log-bin                        = /var/lib/mysql/mysql-bin
expire-logs-days               = 14
sync-binlog                    = 1
# 進行復制的庫(可以不用)
# binlog-do-db				   = db1
# 忽略複製的庫(可以不用)
binlog-ignore-db               = information_schema
binlog-ignore-db               = mysql
binlog-ignore-db               = performance_schema
binlog-ignore-db               = sys

# CACHES AND LIMITS #
tmp-table-size                 = 32M
max-heap-table-size            = 32M
query-cache-type               = 0
query-cache-size               = 0
max-connections                = 500
thread-cache-size              = 50
open-files-limit               = 65535
table-definition-cache         = 1024
table-open-cache               = 2048

# INNODB #
innodb-flush-method            = O_DIRECT
innodb-log-files-in-group      = 2
innodb-log-file-size           = 256M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table          = 1
innodb-buffer-pool-size        = 8G

# LOGGING #
log-error                      = /var/lib/mysql/mysql-error.log
log-queries-not-using-indexes  = 1
slow-query-log                 = 1
slow-query-log-file            = /var/lib/mysql/mysql-slow.log
  • 第二步:重啓mysql服務
service mysqld restart
  • 第三步:建立帳戶並授權給slave在192.168.1.2上可以連接
# 授權
mysql>GRANT SELECT, PROCESS, SUPER ,FILE,REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'192.168.1.2' IDENTIFIED BY '123456';

# 刷新權限
mysql> FLUSH PRIVILEGES;

一般不用root帳號,“%”表示所有客戶端都可能連,只要帳號,密碼正確,此處可用具體客戶端IP代替,如192.168.145.226,加強安全。

  • 第四步:查詢master的狀態
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      899 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

三、Mysql從服務器配置

  • 第一步:修改my.conf文件:
    在[mysqld]段下添加:
#服務器唯一ID,一般取IP最後一段
server-id=434

全部配置如下:

# Generated by Percona Configuration Wizard (http://tools.percona.com/) version REL5-20120208
# Configuration name slave generated for [email protected] at 2018-11-28 09:16:21

[mysql]

# CLIENT #
port                           = 39847
socket                         = /var/lib/mysql/mysql.sock

[mysqld]

# GENERAL #
user                           = mysql
default-storage-engine         = InnoDB
socket                         = /var/lib/mysql/mysql.sock
pid-file                       = /var/lib/mysql/mysql.pid
server_id                      = 11462
port                           = 39847

# MyISAM #
key-buffer-size                = 32M
myisam-recover-options         = FORCE,BACKUP

# SAFETY #
max-allowed-packet             = 16M
max-connect-errors             = 1000000

# DATA STORAGE #
datadir                        = /var/lib/mysql/

# BINARY LOGGING #
log-bin                        = /var/lib/mysql/mysql-bin
expire-logs-days               = 14
sync-binlog                    = 1

# REPLICATION #
relay-log                      = /var/lib/mysql/relay-bin
slave-net-timeout              = 60

# CACHES AND LIMITS #
tmp-table-size                 = 32M
max-heap-table-size            = 32M
query-cache-type               = 0
query-cache-size               = 0
max-connections                = 500
thread-cache-size              = 50
open-files-limit               = 65535
table-definition-cache         = 1024
table-open-cache               = 2048

# INNODB #
innodb-flush-method            = O_DIRECT
innodb-log-files-in-group      = 2
innodb-log-file-size           = 256M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table          = 1
innodb-buffer-pool-size        = 8G

# LOGGING #
log-error                      = /var/lib/mysql/mysql-error.log
log-queries-not-using-indexes  = 1
slow-query-log                 = 1
slow-query-log-file            = /var/lib/mysql/mysql-slow.log

  • 第二步:重啓mysql服務
service mysqld restart
  • 第三步:建立連接
mysql>change master to master_host='192.168.1.1',master_port=39847,master_user='slave',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=899;
mysql>start slave;

注意語句中間不要斷開,master_port爲mysql服務器端口號(無引號),master_user爲執行同步操作的數據庫賬戶,“120”無單引號(此處的899就是show master status 中看到的position的值,這裏的mysql-bin.000001就是file對應的值)。

  • 第四步:檢查從服務器複製功能狀態
mysql> show slave status\G;
……………………(省略部分)
Slave_IO_Running: Yes //此狀態必須YES
Slave_SQL_Running: Yes //此狀態必須YES
……………………(省略部分)

注:Slave_IO及Slave_SQL進程必須正常運行,即YES狀態,否則都是錯誤的狀態(如:其中一個NO均屬錯誤)。

錯誤處理:
如果出現此錯誤:
Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be
different for replication to work.
因爲是mysql是克隆的系統所以mysql的uuid是一樣的,所以需要修改。
解決方法:
刪除/var/lib/mysql/auto.cnf文件,重新啓動服務。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章