CentOS7-MySQL5.7.29主準備同期の構築

7371 ワード

一、主備モード
コールド・スタンバイ・モードとも呼ばれます.一般的に言えば、2台のサーバーにMySQLがインストールされています.1台はプライマリ・サーバー、1台はスタンバイ・サーバーとして使用されています.スタンバイ・サーバーはプライマリ・サーバーのMySQLデータを同期させます.プライマリ・サーバーが故障したり、停止したりした場合、スタンバイ・サーバーをトップにして、データが失われないことを保証します.
二、注意事項
仮想マシンが直接コピーしたサーバの場合は、MySQLのuuid値の変更に注意してください.2台は同じではありません.
vi /usr/local/mysql/data/auto.cnf
#                
#          MySQL

もう一つ注意しなければならないのはファイアウォールです.MySQLのポートをファイアウォールに追加するには、こちらを直接閉じます.
#        ,       active (running)   
systemctl status firewalld.service
#      ,       inactive (dead)   
systemctl stop firewalld.service

三、構築開始
1、環境
システム:CentOS 7.0
        MySQL:5.7.29
プライマリサーバIP:192.168.74.132
予備サーバーIP:192.168.74.140
2、ライブラリtestの作成
2台のサーバがそれぞれデータベースtestとテーブルを作成
#    
create database test;
#    
use test;
create table tb_mobile( mobile VARCHAR(20) comment'    ', time timestamp DEFAULT now() comment'  ' );

3、ホストに同期ユーザーを作成する
#     
grant replication slave on *.* to 'demo'@'192.168.74.140' identified by '1qaz2wsx';
#     
flush privileges;

4、作成したばかりのdemoユーザーでバックアップにリモートログインする
#            MySQL
mysql -h192.168.74.132 -udemo -p

5、ホストのmyを修正する.cnfファイル
# ps:        !includedir /etc/my.cnf.d     

[client]
default-character-set=utf8
socket=/usr/local/mysql/data/mysql.sock
[mysqld]
#   ID
server-id = 1
log-bin=mysql-bin
#         
binlog-do-db = test
#          
binlog-ignore-db = mysql
port=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/usr/local/mysql/mysql-log/error.log
pid-file=/usr/local/mysql/data/mysql.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

6、MySQLを再起動する
7、ホスト上でデータベーステーブルをロックする
flush tables with read lock;

8、ホスト状態の表示
show master status\G;



#      ,     File      Position   
mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 154
     Binlog_Do_DB: test
 Binlog_Ignore_DB: mysql
Executed_Gtid_Set: 
1 row in set (0.00 sec)

9、予備サーバーのmyを修正する.cnfファイル
[client]
default-character-set=utf8
socket=/usr/local/mysql/data/mysql.sock
[mysqld]
#        
server-id = 2
log-bin=mysql-bin
replicate-do-db = test
replicate-ignore-db = mysql,information_schema,performance_schema
port=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/data/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/usr/local/mysql/mysql-log/error.log
pid-file=/usr/local/mysql/data/mysql.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

10、バックアップMySQLサービスの再起動
11、スタンバイでMySQLに入ってchange masterコマンドを実行する
#     slave
stop slave;

#     
change master to master_host='192.168.74.132',master_user='demo',master_password='1qaz2wsx',master_log_file='mysql-bin.000001',master_log_pos=154;

#     slave
start slave;



#       ,   
stop slave
#    slave
reset slave
#         -    -      

12、slaveの状態を見る
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.74.132
                  Master_User: demo
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001

#        yes     

             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql,information_schema,performance_schema
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 531
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: be8b52d3-7f04-11ea-a454-000c29e3bb87
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

四、同期テストを開始する
1、先にホストでロック解除を実行する
unlock tables;

2、ホストにデータを追加する
mysql> use test;
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> 
mysql> 
mysql> 
mysql> insert into tb_mobile values('1111',now());
Query OK, 1 row affected (0.01 sec)

mysql> select * from tb_mobile;
+--------+---------------------+
| mobile | time                |
+--------+---------------------+
| 1111   | 2020-04-16 22:06:46 |
+--------+---------------------+
1 row in set (0.00 sec)

3、スペアで同期データを表示する
mysql> use test;
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> select * from tb_mobile;
+--------+---------------------+
| mobile | time                |
+--------+---------------------+
| 1111   | 2020-04-16 22:06:46 |
+--------+---------------------+
1 row in set (0.00 sec)

4、同期成功.の最後の部分