forked from dimganbi/BlogCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_master_slave_sync.sh
61 lines (51 loc) · 1.83 KB
/
mysql_master_slave_sync.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 博客:MySQL学习小记(5)、主从服务器同步
# ex2tron 2017年7月29日
# http://ex2tron.xin
#(一)、主机配置
# 创建同步账号
> grant replication slave on *.* to 'rep_slave'@'192.168.1.%' identified by 'repslavepwd';
> flush privileges;
# 修改my.ini配置文件
[mysqld]
; 数据库唯一ID,主从机不能重复
server-id = 1
; 指定二进制日志存放路径,只有文件名的话,默认存放在mysql的data目录下
log-bin = mysql-bin
; 以下为可选配置项
; 需要同步的数据库名,如果有多个,按此格式多写几行即可,不指定的话,默认全部同步
; binlog-do-db = database
; 不需要备份的数据库名
; binlog-ignore-db = database
# 重启MySQL服务
> net stop wampmysqld64
> net start wampmysqld64
# 查看主机状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 3046 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#(二)、从机配置
# 修改my.cnf配置文件
[mysqld]
# 数据库唯一ID,主从机不能重复
server-id = 2
# 重启MySQL服务
$ sudo service mysql restart
# 配置同步信息
> stop slave;
> change master to master_host = '192.168.1.102',
-> master_user = 'rep_slave',
-> master_password = 'repslavepwd',
-> master_log_file = 'mysql-bin.000002',
-> master_log_pos = 3046;
> start slave;
# 显示从机状态
> show slave status\G;
#(三)、测试同步
# 主机上新建一个数据库
> create database testsync;
# 从机上查看数据库
> show databases;