MySQL主从备份

来源:互联网 发布:去学校培训seo多少钱 编辑:程序博客网 时间:2024/06/02 03:48

master:主服务器
slave:从服务器

1、开启master的binarylog

打开MySQL的配置文件

# vim /etc/my.cnf

添加以下代码

log-bin=mysql-binbinlog_format=mixedserver-id=1read-only=0binlog-do-db=<dbname>

重启master数据库

# /etc/inint.d/mysqld restart

2、导出master数据库

锁定master数据库

mysql> FLUSH TABLES WITH READ LOCK;

导出master数据库

# mysqldump --master-data -uroot -p <dbname> > <path>+<dbname>.sql

记录master的日志信息

mysql> show master status\G*************************** 1. row ***************************             File: mysql-bin.000001         Position: 154     Binlog_Do_DB: <dbname> Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec)

解除锁定

mysql> UNLOCK TABLES;

3、开启slave的binarylog

打开MySQL的配置文件

# vim /etc/my.cnf

添加以下代码

log-bin=mysql-binbinlog_format=mixedserver-id=2replicate-do-db=<dbname>relay_log=mysql-relay-binlog-slave-updates=OFF

重启slave数据库

# /etc/inint.d/mysqld restart

4、导入master的数据到slave中

在slave中创建相应的数据库

CREATE DATABASE `<dbname>` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci

导入数据库

mysql> source <path>+<dbname>.sql

5、在master中创建同步账户

mysql> grant replication slave on *.* to '<master_user>'@'<slave_host>' identified by '<master_password>';

如:允许所有ip使用该用户名和密码登陆

mysql> grant replication slave on *.* to 'db_backup'@'%' identified by 'root';

6、在slave中开启同步

mysql> CHANGE MASTER TO        MASTER_HOST='<master_host>',        MASTER_USER='<master_user>',        MASTER_PASSWORD='<master_password>',        MASTER_LOG_FILE='mysql-bin.000001',        MASTER_LOG_POS=154;

此处的MASTER_LOG_FILE、MASTER_LOG_POS的值参考2中的master日志信息

mysql> show master status\G
***************** 1. row *****************
File: mysql-bin.000001
Position: 154
Binlog_Do_DB: < dbname >
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

重启slave数据库

# /etc/inint.d/mysqld restart

查看slave的slave线程是否开启

mysql> show slave status\G*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: <master_host>                  Master_User: <master_user>                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000003          Read_Master_Log_Pos: 639               Relay_Log_File: mysqld-relay-bin.001075                Relay_Log_Pos: 4        Relay_Master_Log_File: mysql-bin.000003             Slave_IO_Running: Yes            Slave_SQL_Running: Yes              Replicate_Do_DB: <dbname>          Replicate_Ignore_DB:            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: 233              Relay_Log_Space: 15668              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: 0Master_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:              Master_Info_File: /var/lib/mysql/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:                 Auto_Position: 0         Replicate_Rewrite_DB:                  Channel_Name:            Master_TLS_Version: 1 row in set (0.00 sec)

Slave_IO_State: Waiting for master to send event
表示线程已开启,等待master的数据

7、遇到的问题

1、在my.cnf中加入log-bin=mysql-bin后仍无法开启binarylog

  • 添加后需重启MySQL
  • 存在多个my.cnf,用find / -name “my.cnf” 找到其他的my.cnf,一般来说,其中有socket=…的一般都是有效的配置文件
  • 在my.cnf中添加的代码位置不对,尽量上移,在[mysqld]的最后面即可

2、slave的状态中有Slave_IO_Running: Connecting 错误

  • 检查< master_host > 、< master_user >、< master_password >是否手误填写错误
  • master服务器的3306端口没有允许外部访问,在master中添加防火墙例外。然后用telnet <master_host> 3306测试是否连通
  • 检查master中创建的同步账户的< slave_host >
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 2岁半宝宝太调皮怎么办 2岁宝宝晚上睡觉晚怎么办 三周岁半宝宝入园后不合群怎么办 数学懒于思考的孩子怎么办 2岁宝宝爱哭不讲道理怎么办 孩子不讲道理一直哭怎么办 白天不烧晚上烧怎么办 两岁宝宝出虚汗怎么办 两岁宝宝出水痘怎么办 两岁宝宝爱看手机怎么办 两岁宝宝太好动怎么办 五岁宝宝不会数数怎么办 四岁宝宝算数不好怎么办 两个月宝宝体内有火怎么办 两个月宝宝有火怎么办 2岁宝宝起眼屎怎么办 一周岁的宝宝皮肤过敏怎么办 两岁宝宝脾气倔不听话怎么办 22个月宝宝打人怎么办 两岁宝宝会打人怎么办 3岁哭闹倔强不止怎么办 我儿子二十三岁不爱说话怎么办 2岁宝宝不听话脾气大怎么办 2岁宝宝调皮不听话怎么办 2岁的宝宝不听话怎么办 2岁宝宝总是不听话怎么办 2岁宝宝淘气不听话怎么办 两岁宝宝不听大人的话怎么办? 来月经奶量减少怎么办 月经来了奶少怎么办 来例假奶水少了怎么办 两岁宝宝吐口水怎么办 3岁宝宝不愿自己吃饭怎么办 婆家的人很烦人怎么办 三十了还没结婚怎么办 两岁宝贝断奶粉怎么办 宝宝断了母乳不吃奶粉怎么办 一岁宝宝不爱吃辅食怎么办 断奶后宝宝抗拒奶瓶怎么办 两岁宝宝断奶后不喝奶粉怎么办 两岁宝宝断奶不吃奶粉怎么办