數據庫安裝參考:https://blog.csdn.net/STIll_ly/article/details/103561594
環境準備
Linux發行版本:Centos7
Mysql數據庫版本:mysql-5.7.24
主服務器地址:10.10.10.8
從服務器地址:10.10.10.9
配置主服務器
- 首先root用戶登錄mysql,創建一個同步賬號,授予該賬號REPLICATION SLAVE權限,用於從服務器訪問主服務器
[admin@localhost mysql]$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.24 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> CREATE USER 'repl'@'%' IDENTIFIED BY '密碼';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
Query OK, 0 rows affected (0.00 sec)
- 配置主從同步,要在主服務器開啓二進制日誌,也就是binlog,並配置要同步或不同步哪些數據庫.這需要修改my.cnf配置文件
[admin@localhost etc]$ cat my.cnf
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
port = 3306
default-character-set=utf8
[mysqld]
# 一般配置選項
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character-set-server=utf8
default_storage_engine = InnoDB
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
#此參數表示啓用binlog並指定日誌文件路徑
log_bin=master-bin
#此參數指定二進制索引文件路徑
log_bin_index=master-bin.index
#配置服務器的唯一標識,和其他服務器的唯一標識必須不同,從服務器通過該標識找到主服務器
server-id=1
#指定範圍日期內的binlog日誌,過期的日誌會被清除
expire-logs-days=7
#配置不記錄binlog日誌的數據庫,沒有binlog日誌的數據庫則不會被同步
binlog_ignore_db=mysql
#這些數據庫都是mysql系統數據庫
binlog_ignore_db=information_schema
binlog_ignore_db=performance_schema
binlog_ignore_db=sys
#配置記錄binlog日誌的數據庫,有binlog日誌的數據庫會被同步,和binlog_ignore_db參數互斥,只需配置一類即可
#binlog_do_db=
- 配置完成後,重啓mysql服務:
[admin@localhost etc]$ sudo service mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
- 登錄mysql,查看master狀態
mysql> show master status;
+-------------------+----------+--------------+-------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+-------------------------------------------------+-------------------+
| master-bin.000001 | 154 | | mysql,information_schema,performance_schema,sys | |
+-------------------+----------+--------------+-------------------------------------------------+-------------------+
1 row in set (0.00 sec)
記錄下File和Position這兩個值,一會會用到。
配置從服務器
- 修改從數據庫的my.cnf
[root@localhost etc]# cat my.cnf
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
port = 3306
default-character-set=utf8
[mysqld]
# 一般配置選項
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character-set-server=utf8
default_storage_engine = InnoDB
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
#服務器唯一標識
server-id = 2
#啓動中繼日誌服務並設置地址,中繼日誌就是存儲主庫過來的binlog日誌
relay-log = slave-relay-bin
#中繼日誌二進制文件索引地址
relay-log-index = slave-relay-bin.index
- 重啓mysql服務,然後登陸從服務器的mysql,配置主服務器相關設置,並啓動SLAVE
[root@localhost etc]# service mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@localhost etc]# mysql -uroot -p
Enter password:
mysql> change master to master_host='10.10.10.8', master_port=3306, master_user='repl', master_password='scoc2019slave', master_log_file='master-bin.000001', master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
- 查看主從狀態
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.10.10.8
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 321
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 528
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: fb1a625d-1fd2-11ea-9f77-0cda411d2303
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)
Slave_IO_Running和Slave_SQL_Running都爲Yes,表示成功,完美!
- 測試
主服務器創建一個數據庫testdb:
mysql> CREATE DATABASE testdb;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
+--------------------+
5 rows in set (0.00 sec)
登錄從服務器查看數據庫狀態:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testdb |
+--------------------+
5 rows in set (0.01 sec)
在主服務器中創建的數據庫testdb,在從服務器中也能查看到,說明主從同步成功,完美!