环境:操作系统:CentOs6.5
Mysql版本:5.6.25
环境:操作系统:CentOs6.5 Mysql版本:5.6.25
主服务器:centos6.5 IP:192.168.13.101
从服务器:centos6.5 IP:192.168.13.102
目录:
1、主服务器配置
2、从服务器配置
3、配置主从同步
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 statusG;
结果如下:
*************************** 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写入,就会造成主从数据库数据不一致,在后面的修改、删除时如果因为数据不同步造成操作失败,会导致同步线程中断,谨记!