mysql8.0.25版本设置主从数据库,并且从库只读
具体操作步骤
说明:主从数据库版本一致
1.主库创建同步使用的用户
create user 'repl'@'%' identified with 'mysql_native_password' by 'repl_User_123';
GRANT ALL privileges ON *.* TO 'repl'@'%' with grant option;
flush privileges;
# 必要时主库需要开启远程连接,供其他ip的主机连接
该用户权限仅设置replication slave,replication client权限,从库开启同步会报如下错误:
2022-05-19T07:33:32.846062Z 13 [System] [MY-010562] [Repl] Slave I/O thread for channel '': connected to master 'repl@192.168.0.218:3306',replication started in log 'mysql-bin.000007' at position 165418
2022-05-19T07:33:32.846996Z 13 [Warning] [MY-010551] [Repl] "SELECT UNIX_TIMESTAMP()" failed on master, do not trust column Seconds_Behind_Master of SHOW SLAVE STATUS. Error: Aborted connection 1549 to db: 'unconnected' user: 'repl' host: '192.168.0.36' (init_connect command failed) (1184)
2022-05-19T07:33:32.847995Z 13 [ERROR] [MY-010584] [Repl] Slave I/O for channel '': The slave I/O thread stops because a fatal error is encountered when it try to get the value of SERVER_ID variable from master. Error: , Error_code: MY-000000
用户权限中没有with grant option,,从库开启同步会报如下错误:
2022-05-19T08:12:29.643585Z 16 [Warning] [MY-010584] [Repl] Slave: You are not allowed to create a user with GRANT Error_code: MY-001410
2022-05-19T08:12:29.643600Z 16 [ERROR] [MY-010586] [Repl] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.000007' position 378622
2.主库修改配置文件,开启binlog
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid
lower_case_table_names=1
port=3306
default_authentication_plugin=mysql_native_password
default-storage-engine=INNODB
character-set-server=utf8mb4
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
max_connections=10000
max_user_connections=5000
max_connect_errors=100
wait_timeout=86400
interactive_timeout=1800
############ 如下这几行是新增的 ####################
server_id=1
log_bin=/data/mysql/mysql-bin
log_slave_updates=on
binlog_format=row
expire_logs_days=7
skip_slave_start=1
sync_binlog=1
binlog-do-db=park_cloud_db # 只同步这个数据库
# 如下四个默认数据库不同步
binlog-ignore-db=information_schema
binlog-ignore-db=performation_schema
binlog-ignore-db=sys
binlog-ignore-db=mysql
skip-name-resolve
############ 如上这几行是新增的 ####################
[mysql]
default-character-set=utf8mb4
[client]
port=3306
default-character-set=utf8mb4
3.获取主库binlog信息
[15:24:55]mysql> show variables like 'log_bin';
[15:24:55]+---------------+-------+
[15:24:55]| Variable_name | Value |
[15:24:55]+---------------+-------+
[15:24:55]| log_bin | ON |
[15:24:55]+---------------+-------+
[15:28:45]mysql> show variables like 'server_id';
[15:28:45]+---------------+-------+
[15:28:45]| Variable_name | Value |
[15:28:45]+---------------+-------+
[15:28:45]| server_id | 1 |
[15:28:45]+---------------+-------+
[15:28:45]1 row in set (0.00 sec)
[15:38:12]mysql> show master status;
[15:38:12]+------------------+----------+---------------+--------------------------------------------------+-------------------+
[15:38:12]| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
[15:38:12]+------------------+----------+---------------+--------------------------------------------------+-------------------+
[15:38:12]| mysql-bin.000007 | 315692 | park_cloud_db | information_schema,performation_schema,sys,mysql | |
[15:38:12]+------------------+----------+---------------+--------------------------------------------------+-------------------+
[15:38:12]1 row in set (0.00 sec)
记录下 mysql-bin.000007 和 315692
4.从库修改配置文件
[mysqld]
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid
lower_case_table_names=1
port=3306
default_authentication_plugin=mysql_native_password
default-storage-engine=INNODB
character-set-server=utf8mb4
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
max_connections=10000
max_user_connections=5000
max_connect_errors=100
wait_timeout=86400
interactive_timeout=1800
############ 如下这几行是新增的 ####################
server_id=2
log_bin=/data/mysql/mysql-bin
log_slave_updates=on
relay-log-index=/data/mysql/slave-relay-bin.index
relay-log=slave-relay-bin
replicate-do-db=park_cloud_db #要同步的数据库
slave-net-timeout=60
skip-name-resolve
read_only=1 #限定普通用户只读
super_read_only=on #限定root只读
############ 如上这几行是新增的 ####################
[mysql]
default-character-set=utf8mb4
[client]
port=3306
default-character-set=utf8mb4
5.从库导入主库里的要同步的数据库数据,也就是导入park_cloud_db里的数据
6.从库开启同步
stop salve;
CHANGE MASTER TO MASTER_HOST='192.168.0.218',
MASTER_PORT=3306,
MASTER_USER='repl',
MASTER_PASSWORD='repl_User_123',
MASTER_LOG_FILE='mysql-bin.000007',
MASTER_LOG_POS=315692;
start slave;
show slave status\G
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.218
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 742802
Relay_Log_File: slave-relay-bin.000005
Relay_Log_Pos: 75230
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: park_cloud_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: 742802
Relay_Log_Space: 75607
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: 4b4e33f1-cfeb-11eb-b6fc-fa163e56a76b
Master_Info_File: mysql.slave_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:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
ERROR:
No query specified