1 主从作用
2 实时灾备,用于故障切换
3 读写分离,提供查询服务
4 备份,避免影响业务
5 主从形式
6 一主一从
7 主主复制
8 一主多从---扩展系统读取的性能,因为读是在从库读取的
9 多主一从---5.7开始支持
10 联级复制、
11 主从复制步骤:
12 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
13 从库生成两个线程,一个I/O线程,一个SQL线程
14 I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
15 SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的主从复制配置
16 主从复制配置步骤:
17 确保从数据库与主数据库里的数据一样
18 在主数据库里创建一个同步账号授权给从数据库使用
19 配置主数据库(修改配置文件)
20 配置从数据库(修改配置文件)
21 需求:
22 搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
23 数据库角色
24 IP
25 有无数据
26 主数据库
27 192.168.24.132
28 无数据
29 从数据库
30 192.168.24.131
31 无数据
32 1.准备两台虚拟机,一台主机名为wang,一台主机名为ming,。在wang上创建一个同步账号。
33
34 第一种,数据库中么有内容
35 查看主数据库的内容
36 MariaDB [(none)]> show databases;
37 +--------------------+
38 | Database |
39 +--------------------+
40 | information_schema |
41 | mysql |
42 | performance_schema |
43 | test |
44 +--------------------+
45 4 rows in set (0.00 sec)
46 查看从数据库的内容
47 MariaDB [(none)]> show databases;
48 +--------------------+
49 | Database |
50 +--------------------+
51 | information_schema |
52 | mysql |
53 | performance_schema |
54 | test |
55 +--------------------+
56 4 rows in set (0.00 sec)
57 完全备份数据库
58 给从授权一个允许访问的用户
59 MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.24.141' identified by 'repl123';
60 Query OK, 0 rows affected (0.00 sec)
61 刷新
62 mysql> flush privileges ;
63 Query OK, 0 rows affected (0.00 sec)
64 关闭防护墙
65 [root@wang ~]# systemctl stop firewalld.service
66 [root@wang ~]# setenforce 0
67 进去ming机器,关闭防火墙
68 [root@wang ~]# systemctl stop firewalld.service
69 [root@wang ~]# setenforce 0
70 从库连接进主库
71 [root@wang ~]# mysql -urepl -prepl123 -h192.168.24.132
72 Welcome to the MariaDB monitor. Commands end with ; or \g.
73 Your MariaDB connection id is 5
74 Server version: 5.5.68-MariaDB MariaDB Server
75 Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
76 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
77 MariaDB [(none)]> quit
78 Bye
79 配置主数据库 的文件
80 [root@wang ~]# vim /etc/my.cnf
81 //在[mysqld]这段的后面加上如下内容
82 [mysqld]
83 log-bin=mysql-bin //启用binlog日志
84 server-id=1 //数据库服务器唯一标识符,主库的server-id值必须比从库的大
85 重启服务
86 [root@wang ~]# systemctl restart mariadb.service
87 [root@wang ~]# ss -antl
88 State Recv-Q Send-Q Local Address:Port Peer Address:Port
89 LISTEN 0 128 *:22 *:*
90 LISTEN 0 100 127.0.0.1:25 *:*
91 LISTEN 0 50 *:3306 *:*
92 LISTEN 0 128 :::22 :::*
93 LISTEN 0 100 ::1:25 :::*
94 查看主库的状态
95 MariaDB [(none)]> show master status;
96 +------------------+----------+--------------+--------------
97 | File | Position | Binlog_Do_DB | Binlog_Ignore
98 +------------------+----------+--------------+--------------
99 | mysql-bin.000001 | 245 | |
100 +------------------+----------+--------------+--------------
101 1 row in set (0.00 sec)
102 配置从数据库
103 [root@ming ~]# vim /etc/my.cnf
104 添加内容
105 server-id=2 //设置从库的唯一标识符,从库的server-id值必须小于主库的值并且与主库尽量差几个数
106 relay-log=mysql-relay-bin //启用中继日志relay-log
107 重启服务
108 [root@ming ~]# systemctl restart mariadb.service
109 [root@ming ~]# ss -antl
110 State Recv-Q Send-Q Local Address:Port Peer Address:Port
111 LISTEN 0 128 *:22 *:*
112 LISTEN 0 100 127.0.0.1:25 *:*
113 LISTEN 0 50 *:3306 *:*
114 LISTEN 0 128 :::22 :::*
115 LISTEN 0 100 ::1:25 :::*
116 //配置并启动主从复制
117 MariaDB [(none)]> change master to master_host='192.168.24.132',master_user='repl',master_password='repl123',master_log_file='mysql_bin.000001',master_log_pos=245;
118 Query OK, 0 rows affected (0.06 sec)
119 MariaDB [(none)]> start slave;
120 Query OK, 0 rows affected (0.02 sec)
121 查看服务状态
122
123 MariaDB [(none)]> show slave status\G
124 lave_IO_Running: Yes #这两行是yes就代表成功
125 Slave_SQL_Running: Yes
126 验证结果
127 [root@wang~]# mysql -uroot -p990304
128 MariaDB [(none)]> create database wangming
129 MariaDB [(none)]> use wangming;
130 MariaDB[wangming]> create table wangming(id int not null primary key auto_increment,name varchar(100) not null,age tinyint);
131 MariaDB [wangming]> insert student(name,age) values('tom',20),('jerry',23),('lisi',18);
132
133 [root@ming~]# mysql -uroot -p990304
134 MariaDB [(none)]> show databases;
135 +--------------------+
136 | Database |
137 +--------------------+
138 | information_schema |
139 | mysql |
140 | performance_schema |
141 | test |
142 |wangming |
143 +--------------------+
144 MariaDB [(none)]> usewangming;
145 MariaDB [wangming]> select * from wangming;
146 +----+------+------+
147 | id | name | age |
148 +----+------+------+
149 | 1 | tom | 20 |
150 | 2 | jerry | 23 |
151 | 3 lisi | 18 |
152 +----+------+------+
153 同步成功
154
155
156 数据库角色
157 IP
158 有无数据
159 主数据库
160 192.168.44.128
161 有数据
162 从数据库
163 192.168.44.131
164 无数据
165
166 .准备两台虚拟机,一台主机名为wang,一台主机名为ming。
167 关闭防火墙
168 root@wang ~]# systemctl stop firewalld.service
169 [root@wang ~]# setenforce 0
170
171 root@ming ~]# systemctl stop firewalld.service
172 [root@ming ~]# setenforce 0
173 编辑表内容
174 [root@wang ~]# mysql -uroot -p990304
175 MariaDB [(none)]> create database info;
176 MariaDB [(none)]> use info;d
177 MariaDB [info]> create table basic(id int not null primary key auto_increment,name varchar(50),job varchar(50));
178 MariaDB [info]> insert basic(name,job) values('tom','engineer'),('jerry','office');
179 MariaDB [info]> create table salary(name varchar(50),salary float);
180 MariaDB [info]> insert salary(name,salary) values('tom',8000),('jerry',8500),('zhangshan',9000),('lisi',7500);
181 查看库内信息
182 MariaDB [info]> show tables;
183 +----------------+
184 | Tables_in_info |
185 +----------------+
186 | basic |
187 | salary |
188 +----------------+
189 2 rows in set (0.00 sec)
190 查看主的库
191 [root@wang ~]# mysql -uroot -p990304 -e 'show databases;'
192 +--------------------+
193 | Database |
194 +--------------------+
195 | information_schema |
196 | info |
197 | mysql |
198 | performance_schema |
199 | test |
200 +--------------------+
201 查看从的库
202 [root@ming ~]# mysql -uroot -p990304 -e'show databases;'
203 +--------------------+
204 | Database |
205 +--------------------+
206 | information_schema |
207 | mysql |
208 | performance_schema |
209 | test |
210 +--------------------+,
211 锁定数据库,禁止写入,确保数据备份完整
212 MariaDB [(none)]> flush tables with read lock;
213 Query OK, 0 rows affected (0.00 sec)
214 完全备份
215 [root@wang ~]# mysqldump -uroot --all-databases > all.sql
216 [root@wang ~]# ll
217 总用量 508
218 -rw-r--r--. 1 root root 516957 12月 3 08:23 all.sq
219 利用scp传输到从
220 l[root@wang ~]# scp all.sql 192.168.24.141:/root/
221 root@192.168.24.141's password:
222 all.sql 100% 505KB 13.2MB/s 00:00
223 从查看是否接收
224 [root@ming ~]# ls
225 all.sql
226 备份到从数据库
227 [root@ming ~]# mysql < all.sql
228 [root@ming ~]# mysql -uroot -p990304 -e'show databases;'
229 +--------------------+
230 | Database |
231 +--------------------+
232 | information_schema |
233 | info |
234 | mysql |
235 | performance_schema |
236 | test |
237 +--------------------+
238 关闭读锁状态,推出锁定数据库即可
239 MariaDB [(none)]> quit
240 Bye
241 授权信息
242 MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.24.141' identified by 'repl123';
243 MariaDB [(none)]> flush privileges;
244 编辑配置文件,增加
245 [root@wang ~]# vim /etc/my.cnf
246 log-bin=mysql-bin
247 server-id=10
248
249 重启服务
250 [root@wang ~]# systemctl restart mariadb
251 [root@wang ~]# mysql
252 MariaDB [(none)]> show master status;
253 +------------------+----------+--------------+------------------+
254 | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
255 +------------------+----------+--------------+------------------+
256 | mysql-bin.000005 | 1102 | | |
257 +------------------+----------+--------------+------------------+
258 1 row in set (0.00 sec)
259 更改从的配置文件
260 编辑配置文件,增加
261 [root@wang ~]# vim /etc/my.cnf
262 log-bin=mysql-bin
263 server-id=10
264 root@ming ~]# systemctl restart mariadb
265 MariaDB [(none)]> change master to
266 -> master_host='192.168.24.132',
267 -> master_user='repl',
268 -> master_password='repl123',
269 -> master_log_file='mysql_bin.000005',
270 -> master_log_pos=1102;
271 Query OK, 0 rows affected (0.03 sec)
272 MariaDB [(none)]> start slave;
273 Query OK, 0 rows affected (0.01 sec)
274 MariaDB [(none)]> show slave status \G
275 Slave_IO_Running: Yes
276 Slave_SQL_Running: Yes
277 验证数据库是否主从配置成功
278 MariaDB [(none)]> use info;
279 Reading table information for completion of table and column names
280 You can turn off this feature to get a quicker startup with -A
281
282 Database changed
283 MariaDB [info]> show tables;
284 +----------------+
285 | Tables_in_info |
286 +----------------+
287 | basic |
288 | salary |
289 +----------------+
290 2 rows in set (0.000 sec)
291
292 MariaDB [info]> select * from basic;
293 +----+-------+----------+
294 | id | name | job |
295 +----+-------+----------+
296 | 1 | tom | engineer |
297 | 2 | jerry | office |
298 +----+-------+----------+
299
300 //在主数据库插入数据
301 [root@master ~]# mysql -uroot -p123
302 MariaDB [(none)]> show databases;
303 +--------------------+
304 | Database |
305 +--------------------+
306 | info |
307 | information_schema |
308 | mysql |
309 | performance_schema |
310 +--------------------+
311 MariaDB [(none)]> use info;
312 MariaDB [info]> insert basic(name,job) values('zhangshan','cleanner');
313
314 //在从数据库中查看数据是否同步
315 MariaDB [info]> select * from basic;
316 +----+-----------+----------+
317 | id | name | job |
318 +----+-----------+----------+
319 | 1 | tom | engineer |
320 | 2 | jerry | office |
321 | 3 | zhangshan | cleanner |
322 +----+-----------+----------+