Mysql master-slave database (Tencent Cloud Mysql+CVM server self-built Mysql)

Main library: Tencent Cloud Mysql

From the library: CVM server self-built Mysql

Note: If the master database already has a database that needs to be synchronized and the slave database does not have a corresponding database, manually synchronize the data to the slave database (the configuration here will not automatically synchronize the data that already existed before!);

Here, the database with existing data is synchronized, so the data of the master-slave database on both sides needs to be manually synchronized.

1. The main library creates an account for synchronizing data (use the Navicat tool to operate)

mysql
create user 'userdata'@'%' identified by 'pwd';
grant REPLICATION SLAVE,Replication client,reload  on *.* to 'userdata'@'%' identified by 'pwd';


# REPLICATION SLAVE则是一个必须而基本的权限,它直接授予slave服务器以该账户连接master后可以执行replicate操作的权利
 
# REPLICATION CLIENT 使得用户可以使用SHOW MASTER STATUS和SHOW SLAVE STATUS命令,
#也就是说这个权限是用于授予账户监视Replication状况的权力。

# reload 是 administrative 级的权限,即 server administration;

2. Add configuration from library etc/my.cnf (use Xshell tool to operate)

Using Pagoda or the command line, it is recommended to directly use Pagoda to edit online, which is more convenient and intuitive.

cd /etc
vim /my.cnf

#服务的id
server_id=201
 
#只读
read_only  = 1
 
#端口号
port = 3306
 
#复制要同步的数据库,可以并列写多个
replicate-do-db=db1
 
#不进行同步的表,可以并列写多个
#replicate-wild-ignore-table=db1.table1
 
#忽略复制的数据库,可以并列写多个
replicate-ignore-db = mysql
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
 
#日志格式(Statement,Row,Mixedlevel)
binlog_format  = ROW
 
#开启日志
log-bin         = mysql-bin
 
#操作日志
log-bin-index      = mysql-bin.index
relay-log        = relay-log
relay_log_index     = relay-log.index
 
#GTID配置
#是否开启GTID功能
gtid_mode=on
enforce_gtid_consistency=on
#同步主库操作日志
log-slave-updates = 1
 
#跳过所有错误,这个参数至关重要
slave-skip-errors = all

3. Restart from the library (configuration modification requires a restart to load the configuration file)

service mysqld restart

4. Verify the account of the master library on the slave library server (userdate)

 mysql -u userdata -h cdb-bpg3o5pm.bj.tencentcdb.com  -P 10191 -p
 pwd;

5. Start the master-slave configuration (the corresponding database needs to be created first)

mysqldump --column-statistics=0 -u userdata -P 10191  -p -h cdb-bpg3o5pm.bj.tencentcdb.com --single-transaction --master-data=2 -R --set-gtid-purged=OFF --databases photocloudconsole>mylave.sql;
pwd

 6. Query the main library information

show master status\G

7. Set the main library information from the library to associate

CHANGE MASTER TO 
MASTER_HOST='cdb-bpg3o5pm.bj.tencentcdb.com',
MASTER_USER='userdata',
MASTER_PASSWORD='pwd',
MASTER_LOG_FILE='mysql-bin.000007',
MASTER_LOG_POS=190,
master_port=10191;

8. Start the master-slave

#启动slave服务
start slave;
#停止slave服务
stop slave;
#重置slave服务,重置时必须先停止slave服务
reset slave;

9. Query slave database information

show slave status\G

Guess you like

Origin blog.csdn.net/sunhuansheng/article/details/123415430