CentOS8配置MySQL雙主互備(安裝centos8及MySQL)

如果對您有幫助請點贊,謝謝!一個贊至少讓我開心一天。

1安裝操作系統centos8及配置

啓用SSH

  1. 首先,要確保CentOS8安裝了openssh-server,在終端中輸入yum list installed | grep openssh-server
  2. 如果又沒任何輸出顯示錶示沒有安裝openssh-server,輸入 yum install openssh-server
  3. 找到了/etc/ssh/目錄下的sshd服務配置文件sshd_config,將#Port 22去掉註釋
  4. 開啓 sshd 服務,輸入 sudo service sshd start
  5. 檢查 sshd 服務是否已經開啓,輸入ps -e | grep sshd

2安裝MySQL及配置

2.1下載鏡像

wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

2.2安裝鏡像

rpm -ivh mysql80-community-release-el7-3.noarch.rpm

2.3升級系統上的MySQL軟件包

通過以下命令升級MySQL及其相關組件:
方案一.指定更新MySQL服務器(推薦使用):

yum update mysql-server
方案二.通過更新系統上的所有內容來更新MySQL(謹慎使用):
注意,這個方法需要謹慎使用,一般是在空白服務器的時候可以使用,因爲有可能會把你係統中的其他軟件都給升級了
yum update

2.4安裝MySQL服務

yum install mysql-server

2.5初始化數據庫

mysqld --initialize

2.6啓動服務

service mysqld start

Job for mysqld.service failed because the control process exited with error code.
See “systemctl status mysqld.service” and “journalctl -xe” for details.

如果出現異常,執行以下語句:

chown mysql:mysql -R /var/lib/mysql
service mysqld start
systemctl status mysqld
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2019-09-26 17:42:23 CST; 6s ago
  Process: 30399 ExecStopPost=/usr/libexec/mysql-wait-stop (code=exited, status=0/SUCCESS)
  Process: 30550 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
  Process: 30464 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, status=0/SUCCESS)
  Process: 30439 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 30501 (mysqld)
   Status: "SERVER_OPERATING"
    Tasks: 38 (limit: 101224)
   Memory: 379.8M
   CGroup: /system.slice/mysqld.service
           └─30501 /usr/libexec/mysqld --basedir=/usr

9月 26 17:42:22 hx-mysql-01 systemd[1]: Starting MySQL 8.0 database server...
9月 26 17:42:23 hx-mysql-01 systemd[1]: Started MySQL 8.0 database server.

2.7修改初始密碼

獲取密碼

grep "password" /var/log/mysql/mysqld.log

2019-09-26T09:41:56.912605Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ju7Ic#aQwyIJ

登陸

mysql -u root -p
ALTER USER 'root'@'%' IDENTIFIED BY '-你的密碼-';
flush privileges;
exit;

2.8重啓MySQL服務

service mysqld restart;

2.9設置MySQL開機自啓動

systemctl enable mysqld


3配置雙主互備

  • 主機1:172.29.60.145
  • 主機2:172.29.60.146

在安裝172.29.60.145172.29.60.146完成MySQL的安裝,切勿直接複製虛擬主機。

3.1 配置主從文件

在172.29.60.145主機上操作

修改/etc/my.cnf

[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
binlog-ignore-db=mysql,test,infromation_schema,sys,performance_schema
binlog-do-db=test1
master-host=172.29.60.146
master-port=3306
master-user=root
master-pass=KONGKONG
master-retry-count=999
master-connect-retry=60
[mysqld]
log-bin=bin-log
server-id=145

在172.29.60.146主機上操作

修改/etc/my.cnf

[client-server]

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
binlog-ignore-db=mysql,test,infromation_schema,sys,performance_schema
binlog-do-db=test1
master-host=172.29.60.145
master-port=3306
master-user=root
master-pass=KONGKONG
master-retry-count=999
master-connect-retry=60
[mysqld]
log-bin=bin-log
server-id=146

3.2創建互備專用用戶(repl)

在172.29.60.145主機上操作

mysql -u root -p

mysql> CREATE USER 'repl'@'172.29.60.146' IDENTIFIED WITH mysql_native_password BY 'KONGKONG';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'172.29.60.146';
mysql> flush privileges;

在172.29.60.146主機上操作

mysql -u root -p

mysql> CREATE USER 'repl'@'172.29.60.145' IDENTIFIED WITH mysql_native_password BY 'KONGKONG';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'172.29.60.145';
mysql> flush privileges;

3.3修改日誌文件標記

在172.29.60.145主機上操作

mysql> SHOW MASTER STATUS;
+----------------+----------+--------------+------------------+-------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| bin-log.000002 |     3864 |              |                  |                   |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

在172.29.60.146主機上操作

mysql> SHOW MASTER STATUS;
+----------------+----------+--------------+------------------+-------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| bin-log.000003 |     2443 |              |                  |                   |
+----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

在172.29.60.145主機上操作

STOP SLAVE;
CHANGE MASTER TO MASTER_HOST='172.29.60.146',MASTER_USER='repl',MASTER_PASSWORD='KONGKONG',MASTER_LOG_FILE='bin-log.000003',MASTER_LOG_POS=2443;
START SLAVE;

在172.29.60.146主機上操作

STOP SLAVE;
CHANGE MASTER TO MASTER_HOST='172.29.60.145',MASTER_USER='repl',MASTER_PASSWORD='KONGKONG',MASTER_LOG_FILE='bin-log.000002',MASTER_LOG_POS=3864;
START SLAVE;

3.4校驗是否設置成功

  • Slave_IO_Running:Yes
  • Slave_SQL_Running: Yes
    若以上兩個參數均爲Yes則表示同步成功。

在172.29.60.145主機上操作

mysql> SHOW SLAVE STATUS;
+----------------------------------+---------------+-------------+-------------+---------------+-----------------+---------------------+------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+
| Slave_IO_State                   | Master_Host   | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File               | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID                          | Master_Info_File        | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version | Master_public_key_path | Get_master_public_key |
+----------------------------------+---------------+-------------+-------------+---------------+-----------------+---------------------+------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+
| Waiting for master to send event | 172.29.60.146 | repl        |        3306 |            60 | bin-log.000004  |                 155 | hx-mysql-01-relay-bin.000002 |           320 | bin-log.000004        | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 155 |             534 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |              146 | 668ce869-f082-11e9-b456-ae0d77cbedee | mysql.slave_master_info |         0 |                NULL | Slave has read all relay log; waiting for more updates |              86400 |             |                         |                          |                |                    |                    |                   |             0 |                      |              |                    |                        |                     0 |
+----------------------------------+---------------+-------------+-------------+---------------+-----------------+---------------------+------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+
1 row in set (0.00 sec)

操作:172.29.60.146

mysql> SHOW SLAVE STATUS;
+----------------------------------+---------------+-------------+-------------+---------------+-----------------+---------------------+------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+
| Slave_IO_State                   | Master_Host   | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File               | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID                          | Master_Info_File        | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State                                | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version | Master_public_key_path | Get_master_public_key |
+----------------------------------+---------------+-------------+-------------+---------------+-----------------+---------------------+------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+
| Waiting for master to send event | 172.29.60.145 | repl        |        3306 |            60 | bin-log.000003  |                 155 | hx-mysql-02-relay-bin.000002 |           320 | bin-log.000003        | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                 155 |             534 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |              145 | 323720bf-f083-11e9-bc77-4e1da18bea09 | mysql.slave_master_info |         0 |                NULL | Slave has read all relay log; waiting for more updates |              86400 |             |                         |                          |                |                    |                    |                   |             0 |                      |              |                    |                        |                     0 |
+----------------------------------+---------------+-------------+-------------+---------------+-----------------+---------------------+------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+------------------------+-----------------------+


4異常情況

4.1log file不同步

Got fatal error 1236 from master when reading data from binary log: ‘Could not find first log file name in binary log index file’

  • log file不同步,重新執行步驟三

This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD FOR CHANNEL ‘’ first.

  • 執行:stop slave;

The slave I/O thread stops because master and slave have equal MySQL server ids

修改/etc/my.cnf

[client-server]
!includedir /etc/my.cnf.d
binlog-ignore-db=mysql,test,infromation_schema,sys,performance_schema
binlog-do-db=test1
master-host=172.29.60.146
master-port=3306
master-user=root
master-pass=***
master-retry-count=999
master-connect-retry=60
[mysqld]
log-bin=bin-log
server-id=145

Could not execute Write_rows event on table test1.user; Duplicate entry ‘12’

由於master要刪除一條記錄,而slave上找不到故報錯,這種情況主上都將其刪除了,那麼從機可以直接跳過。可用命令:

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