MySQL主从架构搭建及实现


Mysql版本:5.6.25)

主服务器:centos6.5 IP:192.168.13.101

从服务器:centos6.5 IP:192.168.13.102

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属性来添加,不能直接使用在添加逗号,在逗号后面添加(binlog-ignore同理)
binlog-do-db = test # 同步test库
binlog-do-db = test1
binlog-do-db = 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二进制日志系统
replicate-do-db = test # 同步test库
#binlog-do-db = test,test1,test2 需要同步多个库的时候直接加逗号添加
replicate-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  

tips:注意查看Slave_IO_Running: Yes Slave_SQL_Running: Yes 这两项必须为Yes,以及Log_File和Log_Pos要与master状态下的File,Position相同。
如果都是正确的,则说明配置成功。
tips:由于我们配置的是一主一从,从库只有备份权限,所以所有的写操作都必须要通过101数据库写入,写入后会自动同步到102服务器数据库,如果通过102写入,就会造成主从数据库数据不一致,在后面的修改、删除时如果因为数据不同步造成操作失败,会导致同步线程中断,谨记!
本人创建了一个“软件开发互助”QQ群,群号:571635842,欢迎各位同行加入并讨论技术方面问题。
二维码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱娃哈哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值