centos7恢复mysql数据库_MySQL数据库升级迁移填坑记
原庫:*.*.101.73/74 ???
?? 系統(tǒng)環(huán)境: Suse 12.4
?? MySQL: 5.7.29
新庫:*.*.110.46/47
?? 系統(tǒng)環(huán)境:CentOS7.7 64位
?? MySQL版本: 5.7.30
[一、數(shù)據(jù)庫升級遷移場景]因業(yè)務(wù)側(cè)在*.*.101.73/74 mysql數(shù)據(jù)庫服務(wù)器上部署了java應(yīng)用程序、Hadoop+Hbase數(shù)據(jù)庫等大數(shù)據(jù)環(huán)境,導(dǎo)致主機(jī)內(nèi)存突然暴增告急,經(jīng)雙方排查,發(fā)現(xiàn)數(shù)據(jù)庫進(jìn)程本身才占用內(nèi)存8.5%,大部分都是由應(yīng)用緩存占用了內(nèi)存。經(jīng)與局方及業(yè)務(wù)側(cè)溝通,局方敦促業(yè)務(wù)側(cè)將數(shù)據(jù)庫服務(wù)器從73/74服務(wù)器遷移到*.*.110.46/47服務(wù)器上,我方負(fù)責(zé)實施數(shù)據(jù)庫的遷移操作。
[二、遷移采坑問題表現(xiàn)]本次遷移使用的MySQL自帶的備份工具mysqldump從原庫雙主(*.*.101.73/74)導(dǎo)出數(shù)據(jù),通過nfs共享文件系統(tǒng)上傳到資源池新庫雙主(*.*.110.46/47)。
在資源池新庫分別將73、74數(shù)據(jù)庫的備份文件導(dǎo)入 46、47新庫,并啟動雙主復(fù)制進(jìn)程:
mysql> change master to master_host='*.*.110.46',master_user='repl',master_password='xxxxxx',master_port=3306,master_auto_position=1;
結(jié)果報錯如下:
ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log
[三、遷移采坑問題分析過程]從報錯信息來看,起初以為是執(zhí)行復(fù)制的語句重復(fù)制賬號信息有誤,然后核對了repl賬號的口令是正確的,并查看了復(fù)制賬號repl的權(quán)限信息:
mysql>show grants for ‘repl’@’*.*.110.%’;
結(jié)果顯示沒有repl用戶的權(quán)限信息記錄。接著查看系統(tǒng)表user中數(shù)據(jù)信息,竟然沒有導(dǎo)入數(shù)據(jù)前創(chuàng)建的repl用戶記錄,哦,奇怪。
突然想到,由于我們備份的是原庫中所有表(--all-databases),導(dǎo)出的dump文件中包含有重新創(chuàng)建表結(jié)構(gòu)的語句,所以馬上在資源池雙主庫新建復(fù)制賬號repl:
grant? replication? slave? on *.* to? 'repl'@'*.*.110.%'? identified by? 'xxxxxx';
flush privileges;
然后重新執(zhí)行復(fù)制語句并開啟復(fù)制進(jìn)程依然報剛才的錯。然后就想到此次遷移是從Suse 12.4 ?MySQL-5.7.29 遷移到CentOS7.7 MySQL-5.7.30, 以為是版本不兼容。
接著將資源池46/47的MySQL版本降為 mysql 5.7.29。分別重新導(dǎo)入數(shù)據(jù)到新庫46/47上,導(dǎo)入數(shù)據(jù)庫的過程中46服務(wù)器導(dǎo)入正常,而發(fā)現(xiàn)47庫上通過source導(dǎo)入時非常的慢,每條執(zhí)行返回10-30秒,當(dāng)時沒有查具體原因,有可能是網(wǎng)絡(luò)卡頓吧。
最后查看原庫74/74的數(shù)據(jù)庫配置文件,返現(xiàn)沒有開啟GTID全局復(fù)制方式(說明,目前這邊項目MySQL數(shù)據(jù)庫幾乎都使用的基于GTID全局事務(wù)復(fù)制協(xié)議做的同步),而我執(zhí)行的復(fù)制語句中有“master_auto_position=1”,原來新庫上執(zhí)行的復(fù)制機(jī)制跟原庫不一致,這就是剛才開啟復(fù)制進(jìn)程報錯的根本原因。
[四、數(shù)據(jù)遷移采坑處理]通過以上分析,我們得知,既然原庫使用的是binlog和pos做的同步,那么我們新庫也同樣按照這個方式來配置復(fù)制。其次由于剛才使用mysql內(nèi)置工具導(dǎo)入數(shù)據(jù)時很緩慢,所以我們準(zhǔn)備采用percona提供的xtrabackup 工具來做數(shù)據(jù)備份和恢復(fù)。
4.1、首先檢查新舊庫上是否有創(chuàng)建備份賬號,結(jié)果現(xiàn)實沒有新新建
? create user 'bkuser'@'localhost' identified by 'xxxxxx';
? grant reload,lock tables,replication client,process on *.* to 'bkuser'@'localhost';
? flush privileges;
4.2、原庫上使用xtrabackup備份雙主數(shù)據(jù)
分別在原庫73/74上使用xtrabackup做全量備份。
73服務(wù)器上:
innobackupex --defaults-file=/home/mysql/my_cnf/my.cnf --host=*.*.101.73 --user=bkuser --password=xxxxxx --port=3306 --socket=/app/gzyd/data/mysql/tmp/mysql.sock --no-timestamp /mysqlbackup/73_xtra_base_20200623
74服務(wù)器上:
innobackupex --defaults-file=/home/mysql/my_cnf/my.cnf --host=*.*.101.74 --user=bkuser --password=xxxxxx --port=3306 --socket=/app/gzyd/data/mysql/tmp/mysql.sock --no-timestamp /mysqlbackup/74_xtra_base_20200623
4.3、新庫上恢復(fù)雙主數(shù)據(jù)
1)導(dǎo)入數(shù)據(jù)前記錄binlog文件及同步位置(master_log_pos和master_log_file)
# 46/47庫上執(zhí)行
mysql> flush table with read lock;
mysql> show master status;
注:記得記錄下master狀態(tài)信息,后面執(zhí)行復(fù)制的時候要用到。
mysql> unlock table;
4.4、全量恢復(fù)
分別在原庫73/74上使用xtrabackup做全量恢復(fù)
1)在46庫上執(zhí)行恢復(fù)操作
innobackupex --defaults-file=/home/mysql/my_cnf/my.cnf? --use-memory=2G --apply-log? /mysqlbackup/73_xtra_base_20200623
mysqladmin? --login-path=myconn shutdown immediate
mv /data/mysql/data /data/mysql/data-bak20200624
mkdir /data/mysql/data
innobackupex --defaults-file=/home/mysql/my_cnf/my.cnf? --copy-back /mysqlbackup/73_xtra_base_20200623
chown -R mysql.mysql?? /data/mysql/data???
mysqld_safe? --defaults-file=/home/mysql/my_cnf/my.cnf? &
2)在47庫上恢復(fù)操作同上
4.5、新庫上配置雙主復(fù)制
1)在46/47服務(wù)器上新建復(fù)制賬號
注:由于在原庫導(dǎo)出的是所有庫,備份文件中含有重新創(chuàng)建表結(jié)構(gòu)的語句,所以在新庫恢復(fù)數(shù)據(jù)后需要重新創(chuàng)建復(fù)制賬號:
grant? replication? slave? on *.* to? 'repl'@'*.*.110.%'? identified by? 'xxxxxx';
flush privileges;
2)配置46->47方向主從
?登錄47服務(wù)器,執(zhí)行復(fù)制語句:
stop slave;?
change master to master_host='*.*.110.46',master_user='repl',master_password='xxxxxx',master_port=3306,master_log_file='bin.000001',master_log_pos=448;
start slave;
show slave status\G;
3)配置47->46方向主從
?登錄46服務(wù)器,執(zhí)行復(fù)制語句:
stop slave;
change master to master_host='*.*.110.47',master_user='repl',master_password='repQAv2wsx@gzydxk',master_port=3306,master_log_file='bin.000001',master_log_pos=1066;
start slave;
show slave status\G;??
4.6、新庫雙主測試
1)主主庫46上試著寫入測試數(shù)據(jù)
mysql> create database chg;
mysql> use chg;
mysql> create table t1(id int, name varchar(30));
mysql> insert into t1(id,name) values(1,'zhangsan');
mysql> insert into t1(id,name) values(2,'lisi');
然后到重復(fù)47上查看新插入的兩條數(shù)據(jù)是否同步過來:
mysql> show databases;
+--------------------+
| Database?????????? |
+--------------------+
| information_schema |
| chg??????????????? |
| mysql????????????? |
| performance_schema |
| smzrz????????????? |
| sys??????????????? |
+--------------------+
6 rows in set (0.00 sec)
mysql> use chg;
mysql> show tables;
+---------------+
| Tables_in_chg |
+---------------+
| t1??????????? |
+---------------+
1 row in set (0.00 sec)
mysql> select? * from t1;
+------+----------+
| id?? | name???? |
+------+----------+
|??? 1 | zhangsan |
|??? 2 | lisi???? |
+------+----------+
2 rows in set (0.00 sec)
2)主主庫46上試著寫入測試數(shù)據(jù)
mysql> create database chg2;
mysql> use chg2;
mysql> create table t2(id int,name varchar(20));
mysql> insert into t2(id,name) values(1,'derek');
mysql> insert into t2(id,name) values(2,'john');
然后到重復(fù)47上查看新插入的兩條數(shù)據(jù)是否同步過來:
mysql> use chg2;
mysql> show tables;
+----------------+
| Tables_in_chg2 |
+----------------+
| t2???????????? |
+----------------+
1 row in set (0.00 sec)
mysql> select * from t2;
+------+-------+
| id?? | name? |
+------+-------+
|??? 1 | derek |
|??? 2 | john? |
+------+-------+
[五、問題規(guī)避]MySQL數(shù)據(jù)庫類似的升級遷移操作注意事項:
①升級遷移操作前仔細(xì)檢查當(dāng)前數(shù)據(jù)庫配置文件(my,cnf),關(guān)注關(guān)鍵性的參數(shù)配置。
②自此檢查數(shù)據(jù)庫的架構(gòu),如:具體使用哪種復(fù)制模式等。
③升級遷移變更前做好充分的數(shù)據(jù)測試。
總結(jié)
以上是生活随笔為你收集整理的centos7恢复mysql数据库_MySQL数据库升级迁移填坑记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 这张磁盘有写保护_架构师不得不了解的硬件
- 下一篇: 如何避免mysql回表查询_mysql如