使用Innobackupex快速搭建(修复)MySQL主从架构

MySQL的主从搭建大家有很多种方式,传统的mysqldump方式是很多人的选择之一。但对于较大的数据库则该方式并非理想的选择。使用Xtrabackup可以快速轻松的构建或修复mysql主从架构。本文描述了使用innobackupex快速来搭建或修复主从架构。供大家参考。

1、基于主库做一个完整备份# mkdir -p /log/bakforslave# innobackupex –user=root -password=*** –socket=/tmp/mysql.sock \–defaults-file=/etc/my.cnf /log/bakforslave –parallel=3 –safe-slave-backup –no-timestamp2、复制数据库到备机# tar -czvf bakforslave.tar.gz ./bakforslave/# scp bakforslave.tar.gz robin@172.16.10.51:~# scp /etc/my.cnf robin@172.16.10.51:~/mymaster.cnf3、在备机上恢复###备机解压打包的备份文件# mv /home/robin/bakforslave.tar.gz /data# cd /data# tar -xvf bakforslave.tar.gz### prepare 备份# innobackupex –user=root -password=*** –socket=/tmp/mysql.sock –defaults-file=/home/robin/mymaster.cnf \ –apply-log –use-memory=4GB /data/bakforslave###如果是修复从库,从库为启动状态应先停止从库,再做如下操作,否则可以跳过 # service mysqld stop###还原备份的数据文件# mv mysqldata mysqldatabk# mv bakforslave mysqldata# chown -R mysql:mysql mysqldata###如果是新搭建的从库,此时可以修改主库的my.cnf为本机的my.cnf,###如果为修复,则可以直接使用原有的配置文件或根据需要修改。# cp /home/robin/mymaster.cnf /etc/my.cnf# vi /etc/my.cnf###此处应修改使用一个不同的server_id,,同时可以根据需要修改相关路径及端口配置等。# service mysqld start ###修改完毕后可以启动mysqld4、主库授权用于复制的用户mysql> grant replication slave,replication client on *.* to repl2@’172.16.10.%’ identified by ‘***’;### 验证shell 提示符下登陆到主库# mysql -urepl2 -p -h172.16.10.885、启动slave# more /data/mysqldata/xtrabackup_binlog_infomysql-bin.00013673752825mysql> CHANGE MASTER TOMASTER_HOST=’172.16.10.88′,–Author: LeshamiMASTER_USER=’repl2′,–Blog : MASTER_PASSWORD=’***’,MASTER_LOG_FILE=’mysql-bin.000136′,MASTER_LOG_POS=73752825;mysql> start slave;6、验证结果mysql> show slave status \G*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 172.16.10.88Master_User: repl2Master_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000136Read_Master_Log_Pos: 96592981Relay_Log_File: mysqld-relay-bin.000002Relay_Log_Pos: 72113Relay_Master_Log_File: mysql-bin.000136Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: test,bs_com,bs_sysmsg,bs_bakReplicate_Ignore_DB: mysqlReplicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 73824655Relay_Log_Space: 22840613Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 3815Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 2Master_UUID: afd6bca4-6636-11e3-9d60-74867ae1c47cMaster_Info_File: /data/mysqldata/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: updatingMaster_Retry_Count: 86400Master_Bind:Last_IO_Error_Timestamp:Last_SQL_Error_Timestamp:Master_SSL_Crl:Master_SSL_Crlpath:Retrieved_Gtid_Set:Executed_Gtid_Set:Auto_Position: 01 row in set (0.00 sec)

离开你的那一天开始,左心房渐渐停止跳动…

使用Innobackupex快速搭建(修复)MySQL主从架构

相关文章:

你感兴趣的文章:

标签云: