Mysql 主从备份

0、mysql 主从同步原理:

  • master 将操作记录到二进制日志(binary log)中;
  • slave IO 线程 将master的binary log events读写到它的中继日志(relay log);
  • slave SQL进程读取中继日志,将重做记录数据到数据库中。

 

 

MySQL的主从同步是一个很成熟的架构,优点为:

  • 在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力;
  • 在从主服务器进行备份,避免备份期间影响主服务器服务;
  • 当主服务器出现问题时,可以切换到从服务器。

 

1、主服务器配置

1.1 创建同步账号并指定服务器地址

[root@localhost~]>mysql -uroot -p
mysql>use mysql;
mysql>grant replication salve on *.* to 'sync_user'@'192.168.13.102' identified by '12345678';
mysql>flush privileges;#刷新权限
tips:创建一个sync_user用户,只能从192.168.13.102地址访问主服务器192.16.13.101的数据库,并且只具有数据库备份权限。

1.2 修改/etc/my.conf文件

vi /etc/my.conf

[mysqld]下添加以下参数
server-id = 1
log-bin = mysql-bin #启动MySQL二进制日志系统
binlog-do-db = test # 同步test库
#binlog-do-db = test,test1,test2 需要同步多个库的时候直接加逗号添加
binlog-ignore-db = mysql # 不同步mysql数据库,如果有多个库,在逗号后面添加即可。
[root@localhost~] service mysqld restart #重启数据库服务

1.3 查看主服务器的master状态

mysql> show master status;

+------------------+----------+--------------+------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001|      120 | test| mysql            |
+------------------+----------+--------------+------------------+

1.4 导出数据库

导出数据库前先锁定数据库

mysql> flush table with read lock; #数据库只读锁定命令,防止导出数据时有数据写入
mysql> mysqldump -uroot -p test>/data/mysql_backup_data/test.sql #导出数据库结构及数据。
mysql> mysqldump-uroot -p -ntd -R test>/data/mysql_backup_data/test_func.sql #导出存储过程及函数
tips:-ntd 导出存储过程,-R 导出函数

2、从服务器配置

2.1 修改/etc/my.conf配置文件

[root@localhost~]vi /etc/my.conf

在[mysqld]下添加以下参数
server-id = 2 #设置从服务器id,必须与主服务器不同
log-bin = mysql-bin #启动MySQL二进制日志系统
binlog-do-db = test # 同步test库
#binlog-do-db = test,test1,test2 需要同步多个库的时候直接加逗号添加
binlog-ignore-db = mysql # 不同步mysql数据库,如果有多个库,在逗号后面添加即可。
[root@localhost~] service mysqld restart #重启数据库服务

2.2 导入数据库

[root@localhost~] mysql -uroot -p

mysql>use test
mysql>source /data/mysql_backup_data/test.sql

3、配置主从同步

3.1 登录从服务器数据库

[root@localhost~ ]mysql -uroot -p

mysql>use mysql
mysql>stop slave;
mysql>change master to
      master_host='192.168.1.101',
      master_user='sync_user',
      master_password='12345678',
      master_log_file='mysql-bin.000001',
      master_log_pos=120;  #log_file与log_pos是主服务器master状态下的File与Position
mysql>start slave;
mysql>show slave status\G;

结果如下:

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.101
Master_User: testuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000012
Read_Master_Log_Pos: 120
Relay_Log_File: orange-2-relay-bin.000003
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000012
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: orange
Replicate_Ignore_DB: mysql,test,information_schema,performance_schema
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: 120
Relay_Log_Space: 1320
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: 773d2987-6821-11e6-b9e0-00163f0004f9
Master_Info_File: /home/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
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

 

其中 show master status;不能显示的问题:

一定是在[mysqld]下加入内容才行。其他地方也不会显示的。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值