mysql主從複製集羣搭建

mysql主從複製集羣搭建(基於日誌點的複製)

安裝mysql

【注意】防火牆開啓的情況下默認的mysql的3306端口會被阻止,所以需要我們手動設置3306端口在防火牆列表中的列外。

mysql配置步驟

  • 在主master端建立複製用戶(此用戶是slave端用來讀取master端binarylog日誌所使用)
  • 備份master端的數據,並在slave端恢復。
  • 使用change master命令配置複製。

配置示例

  • 集羣有三個節點,節點A、節點B、節點C
    其中A、B互爲主從,C爲從節點,B爲C的主節點;

節點A配置my.cnf文件

這裏寫圖片描述

配置文件中增加的內容如下:

#table name as lowercase
  lower_case_table_names=1
  max_allowed_packet = 200M

  #master conf
  server-id=154
  log-bin=mysql-bin
  log-bin-index=mysql-bin.index
  binlog-do-db = softcentric
  binlog-ignore-db = mysql
  binlog-ignore-db=information_schema
  binlog-ignore-db=performance_schema
  log-slave-updates
  sync_binlog = 1
  auto_increment_offset = 1
  auto_increment_increment = 2
  replicate-do-db = softcentric
  replicate-ignore-db = mysql,information_schema,performance_schema

節點B配置my.cnf文件

lower_case_table_names=1
max_allowed_packet = 200M
#slave conf
server-id=157
log-bin=mysql-bin
log-bin-index=mysql-bin.index
binlog-do-db = softcentric
binlog-ignore-db = mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
replicate-do-db = softcentric
replicate-ignore-db = mysql,information_schema,performance_schema
log-slave-updates
sync_binlog = 1
auto_increment_offset = 2
auto_increment_increment = 2

節點C配置my.cnf文件

lower_case_table_names=1
max_allowed_packet = 200M
#slave conf
server-id=158
log-bin=mysql-bin
log-bin-index=mysql-bin.index
relay-log=slave-relay-bin
relay-log-index=slave-relay-bin.index
binlog-do-db = softcentric
binlog-ignore-db = mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema

節點A上進入mysql 命令行:

  • 第一步:創建用於複製的用戶
mysql> create user 'repl_user'@'192.168.31.15%' identified by '123456';
  • 賦予用戶複製的權限
# 在節點A的mysql上授權,讓節點B(157)主機使用的repl_user用戶有複製的權限

mysql> grant replication slave,replication client on *.* to 'repl_user'@'192.168.31.157';
mysql> flush privileges;
mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000003
         Position: 154
     Binlog_Do_DB: softcentric
 Binlog_Ignore_DB: mysql,information_schema,performance_schema
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified

在節點B(157)上指定master通過設置參數,其中master_log_filemaster_log_pos兩個參數來自上一步的輸出中的(FilePosition)的值。

mysql> change master to master_host='192.168.31.154',master_port=3306,master_user='repl_user',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=154;
  • 節點B啓動slave並查看狀態:
mysql>slave start;
mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000003
         Position: 154
     Binlog_Do_DB: softcentric
 Binlog_Ignore_DB: mysql,information_schema,performance_schema
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.31.154
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: softcentric
          Replicate_Ignore_DB: mysql,information_schema,performance_schema

在master A 上導入數據庫表,在節點B(slave)上查看同步的結果

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| softcentric        |
| sys                |
+--------------------+
5 rows in set (0.02 sec)

#這裏多了一個softcentric數據庫

mysql> use softcentric;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+------------------------+
| Tables_in_softcentric  |
+------------------------+
| act_evt_log            |
| act_ge_bytearray       |
| act_ge_property        |
| act_hi_actinst         |
| act_hi_attachment      |
| act_hi_comment         |
| act_hi_detail          |
| act_hi_identitylink    |
| act_hi_procinst        |
| act_hi_taskinst        |
| act_hi_varinst         |
| act_id_group           |

上面我們看到,數據庫中的表已經創建,數據已經複製完成。

在slave節點C上(節點B 157作爲節點C的master)設置master的參數:

mysql> change master to master_host='192.168.31.157',master_port=3306,master_user='repl_user',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=154;

查看同步的結果:

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.31.157
                  Master_User: repl_user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: slave-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
.....

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| softcentric        |
| sys                |
+--------------------+
5 rows in set (0.07 sec)

mysql> use softcentric;show tables;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
+------------------------+
| Tables_in_softcentric  |
+------------------------+
| act_evt_log            |
| act_ge_bytearray       |
| act_ge_property        |
| act_hi_actinst         |
| act_hi_attachment      |
| act_hi_comment         |
| act_hi_detail          |

參考內容
mycat+mysql集羣:實現讀寫分離,分庫分表
高可用mysql 書籍

發佈了69 篇原創文章 · 獲贊 23 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章