Table of contents
Self-built target instance (omitted)
1. Generate SQL statements to query user permissions
2. Generate SQL statements for permissions
3. Generate SQL statements to create non-primary key indexes
4. Export source library structure
5. Export source database data
2. Processing structure export files
4. Create users and permissions
Configure replication to the source and the target instance's own slave repository
1. Pilot
Self-built target instance (omitted)
2. Source export
1. Generate SQL statements to query user permissions
mysql -uwxy -p12345 -S /data/18253/mysqldata/mysql.sock -e "
select concat('show grants for \`',user,'\`@\`',host,'\`;') from mysql.user where user not like 'mysql.%';" -N > show_grants.sql
2. Generate SQL statements for permissions
mysql -uwxy -p12345 -S /data/18253/mysqldata/mysql.sock -N < show_grants.sql > grants.sql
sed -i 's/$/&;/g' grants.sql;
3. Generate SQL statements to create non-primary key indexes
mysql -uwxy -p12345 -S /data/18253/mysqldata/mysql.sock -e "
select concat('alter table ',table_schema,'.',table_name,' add ',concat('index ',index_name),' (',col,');') create_index
from (
select table_schema,table_name,index_name,group_concat(column_name order by seq_in_index) col
from information_schema.statistics
where table_schema in ('test', 'test_jhy') and index_name <> 'PRIMARY'
group by table_schema,table_name,index_name
order by table_schema,table_name,index_name) t;" -N > create_index.sql
4. Export source library structure
# max_allowed_packet 不能大于目标库的值
mysqldump -uwxy -p12345 -S /data/18253/mysqldata/mysql.sock --no-data --single-transaction --triggers --routines --events --set-gtid-purged=OFF --master-data=2 -e --max_allowed_packet=1073741824 -e --net_buffer_length=16384 --databases test test_jhy | gzip > dump_db.sql.gz
5. Export source database data
mysqldump -uwxy -p12345 -S /data/18253/mysqldata/mysql.sock --single-transaction --set-gtid-purged=OFF --master-data=2 -e --max_allowed_packet=1073741824 -e --net_buffer_length=16384 --no-create-db --no-create-info --skip-triggers --databases test test_jhy | gzip > dump_data.sql.gz
3. Target import
1. Target instance settings
bulk_insert_buffer_size=1073741824
innodb_buffer_pool_size=34359738368
innodb_flush_log_at_trx_commit=0
max_allowed_packet=1073741824
concurrent_insert=AUTO
innodb_autoinc_lock_mode=2
skip-log-bin
2. Processing structure export files
gunzip dump_db.sql.gz
# 删除除主键外的索引。不能删除主键,否则建表时遇到自增列报错:
# Incorrect table definition; there can be only one auto column and it must be defined as a key
sed -i '/ KEY `/d;' dump_db.sql
# 删除闭括号前的逗号
sed -i ':a;N;$!ba;s/,\n) ENGINE=InnoDB/\n) ENGINE=InnoDB/g' dump_db.sql
3. Import structure
mysql -uwxy -p12345 -S /data/18253/mysqldata/mysql.sock < dump_db.sql
4. Create users and permissions
mysql -uwxy -p12345 -S /data/18253/mysqldata/mysql.sock < grants.sql
5. Import data
gunzip dump_data.sql.gz
mysql -uwxy -p12345 -S /data/18253/mysqldata/mysql.sock < dump_data.sql
6. Add index
mysql -uwxy -p12345 -S /data/18253/mysqldata/mysql.sock < create_index.sql
4. Follow-up
Configure replication to the source and the target instance's own slave repository
(1) Analyzer
mysql -uroot -p12345 -S /data/18251/mysqldata/mysql.sock < analyze_table.sql
(2) Stop the main database
mysqladmin -uroot -p12345 -S /data/18251/mysqldata/mysql.sock shutdown
ps -ef | grep mysqld
(3) Enable binlog
vim my.cnf
(4) Start the main library
mysqld_safe --defaults-file=/home/mysql/my.cnf &
(5) Configure replication to the Tencent Cloud MySQL instance and observe for a period of time
change master to
master_host='172.18.3.1',
master_port=18251,
master_user='root',
master_password='12345',
master_log_file='aaaaa',
master_log_pos= xxxxx;
start slave;
show slave status\G
(6) mysqladmin
-uroot -p12345 -S /data/18251/mysqldata/mysql.sock shutdown
(7) Copy to the slave data directory
cd /data/18251/
rm dump_data.sql
scp -r * 10.10.10.2:/data/18251/
(8) Start the main library but do not enable replication
mysqld_safe --defaults-file=/home/mysql/my.cnf --skip-slave-start &
(9) Determine and record the main database master location
show master status;
(10) Delete auto.cnf from the library
cd /data/18251/mysqldata/
rm auto.cnf
(11) Start the slave library but do not enable replication
mysqld_safe --defaults-file=/home/mysql/my.cnf --skip-slave-start &
(12) Configure the replication
stop slave from the library to the master library;
reset slave all;
change master to
master_host='10.10.10.1',
master_port=18251,
master_user='repl',
master_password='12345',
master_log_file='bbbbb',
master_log_pos=yyyyy;
start slave;
show slave status\G
(13) Start master database replication
start slave;
show slave status\G
(14) Observe the replication status of the slave library until the quasi-real-time replication is normal
show slave status\G
(15) Analyze the slave library
mysql -uroot -p12345 -S /data/18251/mysqldata/mysql.sock < analyze_table.sql