环境:
主库数据库版本:5.6.40
从库数据库版本:5.7.29
主库:192.168.1.85
从库:192.168.1.118
-----------------------------主库-------------------------
1.主库已经安装mysql,并启动
root账号启动
/opt/mysql5640/bin/mysqld_safe --defaults-file=/opt/mysql5640/conf/my.cnf --user=mysql &
2.查看数据库版本
/opt/mysql5640/bin/mysql -h localhost -uroot -pmysql -S /opt/mysql5640/data/mysql.sock
mysql> select version();
+------------+
| version() |
+------------+
| 5.6.40-log |
+------------+
1 row in set (0.00 sec)
-----------------------------从库-------------------------
3.从库也安装好数据库并启动
root账号启动
/opt/mysql5729/bin/mysqld_safe --defaults-file=/opt/mysql5729/conf/my.cnf --user=mysql &
4.查看从库数据库版本
登陆
/opt/mysql5729/bin/mysql -h localhost -uroot -pmysql -S /opt/mysql5729/mysql.sock
查看版本:
mysql> select version();
+------------+
| version() |
+------------+
| 5.7.29-log |
+------------+
1 row in set (0.06 sec)
-----------------------------主库备份-------------------------
1.确保主库已经有了复制账号
mysql> show grants for 'repl'@'%';
+-------------------------------------------------------------------------------------------------------------------------------------+
| Grants for repl@% |
+-------------------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'repl'@'%' IDENTIFIED BY PASSWORD '*E74858DB86EBA20BC33D0AECAE8A8108C56B17FA' |
+-------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
若没有的话需要在主库上创建
grant replication slave, replication client on *.* to 'repl'@'%' identified by '密码';
2.xtrabackup备份主库
/opt/xtrabackup247/bin/innobackupex --defaults-file=/opt/mysql5640/conf/my.cnf --user=root --password=mysql -P3306 --socket=/opt/mysql5640/data/mysql.sock /opt/xtrbackup_file
压缩备份包
[root@localhost xtrbackup_file]# cd /opt/xtrbackup_file
[root@localhost xtrbackup_file]# tar -cvf mysql5640.tar ./2021-09-24_10-31-01
将压缩包拷贝到目标机器
[root@localhost xtrbackup_file]# scp mysql5640.tar root@192.168.1.118:/tmp/
-----------------------------从库部署-------------------------
1.停掉数据库
[root@localhost data]#/opt/mysql5729/bin/mysqladmin -h localhost -uroot -pmysql -S /opt/mysql5729/mysql.sock shutdown
2.备份原有数据目录并创建新的数据目录
[root@localhost mysql5729]# mv data bakdata
[root@localhost mysql5729]# mkdir data
3.解压主库拷贝过来的备份文件
[root@localhost tmp]# cd /tmp
[root@localhost tmp]# tar -xvf mysql5640.tar
3.恢复
/opt/xtrabackup247/bin/innobackupex --defaults-file=/opt/mysql5729/conf/my.cnf --user=root --apply-log /tmp/2021-09-24_10-31-01
/opt/xtrabackup247/bin/innobackupex --defaults-file=/opt/mysql5729/conf/my.cnf --user=root --copy-back --rsync /tmp/2021-09-24_10-31-01
4.修改数据库目录权限
[root@localhost /]# cd opt
[root@localhost opt]# chown -R mysql:mysql ./mysql5729
5.启动数据库
/opt/mysql5729/bin/mysqld_safe --defaults-file=/opt/mysql5729/conf/my.cnf --user=mysql &
查看启动日志
2021-09-23T03:05:55.769275Z 0 [ERROR] Native table 'performance_schema'.'user_variables_by_thread' has the wrong structure
2021-09-23T03:05:55.769305Z 0 [ERROR] Native table 'performance_schema'.'status_by_account' has the wrong structure
2021-09-23T03:05:55.769337Z 0 [ERROR] Native table 'performance_schema'.'status_by_host' has the wrong structure
2021-09-23T03:05:55.769365Z 0 [ERROR] Native table 'performance_schema'.'status_by_thread' has the wrong structure
2021-09-23T03:05:55.769398Z 0 [ERROR] Native table 'performance_schema'.'status_by_user' has the wrong structure
2021-09-23T03:05:55.769430Z 0 [ERROR] Native table 'performance_schema'.'global_status' has the wrong structure
2021-09-23T03:05:55.769461Z 0 [ERROR] Native table 'performance_schema'.'session_status' has the wrong structure
2021-09-23T03:05:55.769488Z 0 [ERROR] Native table 'performance_schema'.'variables_by_thread' has the wrong structure
2021-09-23T03:05:55.769519Z 0 [ERROR] Native table 'performance_schema'.'global_variables' has the wrong structure
2021-09-23T03:05:55.769572Z 0 [ERROR] Native table 'performance_schema'.'session_variables' has the wrong structure
2021-09-23T03:05:55.787972Z 0 [ERROR] Incorrect definition of table mysql.db: expected column 'User' at position 2 to have type char(32), found type char(16).
2021-09-23T03:05:55.788026Z 0 [ERROR] mysql.user has no `Event_priv` column at position 28
2021-09-23T03:05:55.788955Z 0 [ERROR] Event Scheduler: An error occurred when initializing system tables. Disabling the Event Scheduler.
2021-09-23T03:05:55.789215Z 0 [Note] /opt/mysql5729/bin/mysqld: ready for connections.
Version: '5.7.29-log' socket: '/opt/mysql5729/mysql.sock' port: 13306 MySQL Community Server (GPL)
发现数据库是启动了,但是因为版本不一致,系统表结构不一致导致错误信息
6.登陆查看
/opt/mysql5729/bin/mysql -h localhost -uroot -pmysql -S /opt/mysql5729/mysql.sock
7.不管报错信息看是否能部署主从
change master to master_host='192.168.1.85',
master_port=3306,
master_user='repl',
master_password='mysql',
master_log_file='binlog.000012',
master_log_pos=151;
开始文件和位置可以从备份信息中获取到
[root@localhost 2021-09-24_10-31-01]# more xtrabackup_binlog_info
binlog.000012 151
8.启动
mysql> start slave;
Query OK, 0 rows affected (2.68 sec)
9.查看应用情况
mysql> show slave status G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.85
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000012
Read_Master_Log_Pos: 151
Relay_Log_File: relaylog-binlog.000003
Relay_Log_Pos: 314
Relay_Master_Log_File: binlog.000012
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: information_schema,performance_schema,sys
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: 151
Relay_Log_Space: 521
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: 0b23e9fb-0ef2-11e9-8450-525400f3712a
Master_Info_File: /opt/mysql5729/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: 7cb70453-175d-11ec-bd21-525400add774:1-5
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
-----------------------------验证-------------------------
1.主库上创建表并写入数据
/opt/mysql5640/bin/mysql -h localhost -uroot -pmysql -S /opt/mysql5640/data/mysql.sock
mysql> use db_hxl;
mysql> create tb_rep_test(id int,name varchar(30));
insert into tb_rep_test values(1,'name1');
insert into tb_rep_test values(2,'name2');
insert into tb_rep_test values(3,'name3');
insert into tb_rep_test values(4,'name4');
insert into tb_rep_test values(5,'name5');
2.从库上验证
/opt/mysql5729/bin/mysql -h localhost -uroot -pmysql -S /opt/mysql5729/mysql.sock
mysql> use db_hxl;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+------------------+
| Tables_in_db_hxl |
+------------------+
| rule_01 |
| tb_rep_test |
| tb_test |
+------------------+
3 rows in set (0.00 sec)
mysql> select * from tb_rep_test;
+------+-------+
| id | name |
+------+-------+
| 1 | name1 |
| 2 | name2 |
| 3 | name3 |
| 4 | name4 |
| 5 | name5 |
+------+-------+
5 rows in set (0.00 sec)
发现从库也看到相应的表和数据了,说明mysql不同版本之间是可以做主从复制的,下面我们修复从库系统表的错误
4.升级处理
因为从库的数据文件是从从库拷贝过来的,里面的表字典信息还是5.6的,现在需要进行升级处理,数据库开启的情况下执行如下命令:
/opt/mysql5729/bin/mysql_upgrade -s -pmysql -P13306 -S /opt/mysql5729/mysql.sock
参数说明:
-s, --upgrade-system-tables
[root@localhost bin]# /opt/mysql5730/bin/mysql_upgrade -s -pmysql -P13306 -S /opt/mysql5729/mysql.sock
[root@localhost logfile]# /opt/mysql5729/bin/mysql_upgrade -s -pmysql -P13306 -S /opt/mysql5729/mysql.sock
mysql_upgrade: [Warning] Using a password on the command line interface can be insecure.
The --upgrade-system-tables option was used, databases won't be touched.
Checking if update is needed.
Checking server version.
Running queries to upgrade MySQL server.
Upgrading the sys schema.
Upgrade process completed successfully.
Checking if update is needed.
看到后台执行
mysql> show processlist;
+----+-------------+-----------+---------+---------+--------+--------------------------------------------------------+-------------------------------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+-----------+---------+---------+--------+--------------------------------------------------------+-------------------------------------------------------------------------+
| 2 | root | localhost | db_test | Query | 0 | starting | show processlist |
| 3 | system user | | NULL | Connect | 1240 | Waiting for master to send event | NULL |
| 4 | system user | | NULL | Connect | -86366 | Slave has read all relay log; waiting for more updates | NULL |
| 5 | root | localhost | mysql | Query | 160 | copy to tmp table | ALTER TABLE general_log
MODIFY thread_id BIGINT(21) UNSIGNED NOT NULL |
主库5.6版本的结构
mysql> show create table mysql.general_log;
+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| general_log | CREATE TABLE `general_log` (
`event_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`user_host` mediumtext NOT NULL,
`thread_id` bigint(21) unsigned NOT NULL,
`server_id` int(10) unsigned NOT NULL,
`command_type` varchar(64) NOT NULL,
`argument` mediumtext NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='General log' |
+-------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
从库5.7版本的结构
mysql> show create table mysql.general_log;
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| general_log | CREATE TABLE `general_log` (
`event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
`user_host` mediumtext NOT NULL,
`thread_id` bigint(21) unsigned NOT NULL,
`server_id` int(10) unsigned NOT NULL,
`command_type` varchar(64) NOT NULL,
`argument` mediumblob NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='General log' |
+---
5.重启动
/opt/mysql5729/bin/mysqladmin -h localhost -uroot -pmysql -S /opt/mysql5729/mysql.sock shutdown
/opt/mysql5729/bin/mysqld_safe --defaults-file=/opt/mysql5729/conf/my.cnf --user=mysql &
查看启动日志信息,发现已经没有报错误了
2021-09-23T05:39:12.348497Z 0 [Note] InnoDB: PUNCH HOLE support available
2021-09-23T05:39:12.348593Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2021-09-23T05:39:12.348601Z 0 [Note] InnoDB: Uses event mutexes
2021-09-23T05:39:12.348608Z 0 [Note] InnoDB: GCC builtin __sync_synchronize() is used for memory barrier
2021-09-23T05:39:12.348613Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2021-09-23T05:39:12.348618Z 0 [Note] InnoDB: Using Linux native AIO
2021-09-23T05:39:12.349783Z 0 [Note] InnoDB: Number of pools: 1
2021-09-23T05:39:12.349960Z 0 [Note] InnoDB: Using CPU crc32 instructions
2021-09-23T05:39:12.357767Z 0 [Note] InnoDB: Initializing buffer pool, total size = 4G, instances = 8, chunk size = 128M
2021-09-23T05:39:13.302821Z 0 [Note] InnoDB: Completed initialization of buffer pool
2021-09-23T05:39:13.618851Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2021-09-23T05:39:13.639346Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2021-09-23T05:39:13.980081Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2021-09-23T05:39:13.980193Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2021-09-23T05:39:14.111332Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2021-09-23T05:39:14.112828Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2021-09-23T05:39:14.112851Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2021-09-23T05:39:14.113198Z 0 [Note] InnoDB: Waiting for purge to start
2021-09-23T05:39:14.163450Z 0 [Note] InnoDB: 5.7.29 started; log sequence number 2965976
2021-09-23T05:39:14.163836Z 0 [Note] InnoDB: Loading buffer pool(s) from /opt/mysql5729/data/ib_buffer_pool
2021-09-23T05:39:14.163980Z 0 [Note] Plugin 'FEDERATED' is disabled.
2021-09-23T05:39:14.171157Z 0 [Note] InnoDB: Buffer pool(s) load completed at 210923 13:39:14
2021-09-23T05:39:14.242885Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2021-09-23T05:39:14.242916Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2021-09-23T05:39:14.245146Z 0 [Warning] CA certificate ca.pem is self signed.
2021-09-23T05:39:14.245777Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2021-09-23T05:39:14.246648Z 0 [Note] Server hostname (bind-address): '*'; port: 13306
2021-09-23T05:39:14.246713Z 0 [Note] IPv6 is available.
2021-09-23T05:39:14.246728Z 0 [Note] - '::' resolves to '::';
2021-09-23T05:39:14.246751Z 0 [Note] Server socket created on IP: '::'.
2021-09-23T05:39:14.252960Z 0 [Warning] 'proxies_priv' entry '@ root@localhost.localdomain' ignored in --skip-name-resolve mode.
2021-09-23T05:39:14.367239Z 0 [Note] Event Scheduler: Loaded 0 events
2021-09-23T05:39:14.367438Z 1 [Note] Event Scheduler: scheduler thread started with id 1
2021-09-23T05:39:14.367520Z 0 [Note] /opt/mysql5729/bin/mysqld: ready for connections.
Version: '5.7.29-log' socket: '/opt/mysql5729/mysql.sock' port: 13306 MySQL Community Server (GPL)
6.启动复制
/opt/mysql5729/bin/mysql -h localhost -uroot -pmysql -S /opt/mysql5729/mysql.sock
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
主库的配置文件
[root@localhost xtrbackup_file]# more /opt/mysql5640/conf/my.cnf
[mysqld]
port=3306
server-id=1
datadir=/opt/mysql5640/data
socket=/opt/mysql5640/data/mysql.sock
character-set-server=utf8
max_connections = 1500
skip-external-locking
key_buffer_size=16M
max_allowed_packet=16M
myisam_sort_buffer_size=16M
query_cache_size=32M
read_buffer_size=2M
sort_buffer_size=2M
#table_cache=512
#thread_cache=20
#thread_concurrency=4
interactive_timeout=86400
wait_timeout=86400
#log_slow_queries=1
innodb_file_per_table=1
#innodb_additional_mem_pool_size=16M
innodb_buffer_pool_size=128M
#innodb_file_io_threads=4
innodb_flush_log_at_trx_commit=2
innodb_lock_wait_timeout=50
innodb_log_buffer_size=128M
innodb_log_file_size=128M
innodb_log_files_in_group=5
innodb_log_group_home_dir=/opt/mysql5640/redolog
innodb_thread_concurrency=8
log_bin_trust_function_creators=1
event_scheduler=1
max_binlog_size=100M
log-bin=/opt/mysql5640/mysqllog/binlog/binlog.bin
slow_query_log_file=/opt/mysql5640/mysqllog/logfile/slow-query.log
long_query_time=1
log-error=/opt/mysql5640/mysqllog/logfile/mysql-err.log
binlog_format=row
expire_logs_days=7
binlog_cache_size=4MB
skip-host-cache
skip-name-resolve
#read-only
skip-slave-start
relay-log-index=/opt/mysql5640/mysqllog/relaylog/slave-relay-bin.index
relay-log=/opt/mysql5640/mysqllog/relaylog/slave-relay
slave_net_timeout=60
log_slave_updates=1
##lower_case_table_names=1
##language=/opt/mysql5640/share/english
lc-messages-dir=/opt/mysql5640/share
explicit_defaults_for_timestamp=true
#innodb_stats_persistent=0
#secure-file-priv=/db/mysqloutput
##general_log = 1
##general_log_file = /opt/mysql5640/all.log
##log_output=FILE,TABLE
enforce_gtid_consistency= 1
gtid_mode=ON
[client]
port=3306
socket=/opt/mysql5640/data/mysql.sock
loose-default-character-set = utf8
default-character-set=utf8
[mysql]
no-auto-rehash
port=3306
socket=/opt/mysql5640/data/mysql.sock
[mysqldump]
max_allowed_packet=16M
从库的配置文件
[root@localhost data]# more /opt/mysql5729/conf/my.cnf
[mysqld]
port=13306
server-id=110
basedir=/opt/mysql5729
datadir=/opt/mysql5729/data
socket=/opt/mysql5729/mysql.sock
max_connections = 1500
character_set_server=utf8mb4
collation-server=utf8mb4_general_ci
init_connect='SET collation_connection = utf8mb4_general_ci'
init_connect='SET NAMES utf8mb4'
interactive_timeout=86400
wait_timeout=86400
skip-external-locking
key_buffer_size= 128M
max_allowed_packet=32M
query_cache_size=32M
read_buffer_size=2M
sort_buffer_size=128M
join_buffer_size= 128M
innodb_file_per_table= 1
innodb_open_files= 5000
innodb_buffer_pool_size= 4G
innodb_write_io_threads= 16
innodb_read_io_threads= 16
innodb_thread_concurrency = 0
innodb_purge_threads= 1
innodb_flush_log_at_trx_commit= 2
innodb_log_buffer_size=16M
innodb_log_file_size=512M
innodb_log_files_in_group= 5
innodb_max_dirty_pages_pct= 90
innodb_lock_wait_timeout= 120
bulk_insert_buffer_size= 64M
myisam_sort_buffer_size=64M
myisam_max_sort_file_size= 10G
myisam_repair_threads= 1
log_bin_trust_function_creators=1
event_scheduler=1
max_binlog_size=100M
binlog_format=row
log-bin=/opt/mysql5729/mysqllog/binlog/binlog.bin
slow_query_log=on
slow_query_log_file=/opt/mysql5729/mysqllog/logfile/slow-query.log
long_query_time=1
log_queries_not_using_indexes=on
log-error=/opt/mysql5729/mysqllog/logfile/mysql-err.log
binlog_cache_size=4MB
skip-host-cache
skip-name-resolve
expire_logs_days=15
skip-slave-start
relay-log-index=/opt/mysql5729/mysqllog/relaylog/slave-relay-bin.index
relay-log=/opt/mysql5729/mysqllog/relaylog/relaylog-binlog
replicate-ignore-db=information_schema,performance_schema,sys
slave_net_timeout=60
language=/opt/mysql5729/share/english
early-plugin-load=""
explicit_defaults_for_timestamp=true
log_slave_updates=1
gtid_mode=ON
enforce_gtid_consistency = ON
lower_case_table_names=1
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
[client]
port = 13306
socket=/opt/mysql5729/mysql.sock
default-character-set = utf8mb4
[mysqldump]
quick
max_allowed_packet = 32M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
--The End --