MySQL主从介绍

MySQL主从配置_第1张图片

MySQL主从配置_第2张图片

准备工作、配置主机

MySQL主从配置_第3张图片

1.两台主备机都安装好MySQL。

2.编辑配置文件:

[root@weixing01 ~]# vim /etc/my.cnf

增加下面两行

[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=130
log_bin=weixing1

3.重启MySQL

[root@weixing01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

4.查看新生成的文件:关键文件

[root@weixing01 ~]# cd /data/mysql/
[root@weixing01 mysql]# ls
auto.cnf  ibdata1      ib_logfile1  performance_schema  weixing01.err  weixing1.000001  zrlog
db1       ib_logfile0  mysql        test                weixing01.pid  weixing1.index
[root@weixing01 mysql]# ls -lt
总用量 110744
-rw-rw---- 1 mysql mysql 50331648 4月   2 22:53 ib_logfile0
-rw-rw---- 1 mysql mysql 12582912 4月   2 22:53 ibdata1
-rw-rw---- 1 mysql mysql    66484 4月   2 22:53 weixing01.err
-rw-rw---- 1 mysql mysql        5 4月   2 22:53 weixing01.pid
-rw-rw---- 1 mysql mysql       18 4月   2 22:53 weixing1.index
-rw-rw---- 1 mysql mysql      120 4月   2 22:53 weixing1.000001
drwx------ 2 mysql mysql      324 3月  30 22:41 zrlog
drwx------ 2 mysql mysql       48 3月  22 23:40 db1
-rw-rw---- 1 mysql mysql       56 3月  22 22:25 auto.cnf
drwx------ 2 mysql mysql     4096 3月  22 22:22 mysql
drwx------ 2 mysql mysql     4096 3月  22 22:22 performance_schema
-rw-rw---- 1 mysql mysql 50331648 3月  22 22:22 ib_logfile1
drwx------ 2 mysql mysql        6 3月  22 22:22 test

5.创建测试数据:

[root@weixing01 mysql]# mysqldump -uroot -pwei12273914 zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@weixing01 mysql]# du -sh /tmp/zrlog.sql 
12K /tmp/zrlog.sql
[root@weixing01 mysql]# mysql -uroot -pwei12273914 -e "create database weix"
Warning: Using a password on the command line interface can be insecure.
[root@weixing01 mysql]# mysql -uroot -pwei12273914 weix < /tmp/zrlog.sql 
Warning: Using a password on the command line interface can be insecure.
[root@weixing01 mysql]# ls -lt
总用量 110692
-rw-rw---- 1 mysql mysql 50331648 4月   2 23:00 ib_logfile0
-rw-rw---- 1 mysql mysql 12582912 4月   2 23:00 ibdata1
-rw-rw---- 1 mysql mysql    10255 4月   2 23:00 weixing1.000001
drwx------ 2 mysql mysql      324 4月   2 23:00 weix
-rw-rw---- 1 mysql mysql    66484 4月   2 22:53 weixing01.err
-rw-rw---- 1 mysql mysql        5 4月   2 22:53 weixing01.pid
-rw-rw---- 1 mysql mysql       18 4月   2 22:53 weixing1.index
drwx------ 2 mysql mysql      324 3月  30 22:41 zrlog
drwx------ 2 mysql mysql       48 3月  22 23:40 db1
-rw-rw---- 1 mysql mysql       56 3月  22 22:25 auto.cnf
drwx------ 2 mysql mysql     4096 3月  22 22:22 mysql
drwx------ 2 mysql mysql     4096 3月  22 22:22 performance_schema
-rw-rw---- 1 mysql mysql 50331648 3月  22 22:22 ib_logfile1
drwx------ 2 mysql mysql        6 3月  22 22:22 test

6.创建同步用户:

mysql> grant replication slave on *.* to 'repl'@'192.168.188.132' identified by 'weixing01';
Query OK, 0 rows affected (0.00 sec)

7.锁住表:

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

8.查看现在状态:

mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| weixing1.000001 |    10467 |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

9.数据备份:

[root@weixing01 mysql]# mysqldump -uroot -pwei12273914 test > /tmp/test.sql
Warning: Using a password on the command line interface can be insecure.
[root@weixing01 mysql]# mysqldump -uroot -pwei12273914 db1 > /tmp/db1.sql
Warning: Using a password on the command line interface can be insecure.

配置从机

MySQL主从配置_第4张图片

1.修改配置文件:

[root@weixing01 mysql]# vi /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=132

2.重启服务:

[root@weixing01 mysql]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

3.拷贝主机上备份的文件:

[root@weixing01 mysql]# scp 192.168.188.130:/tmp/*.sql /tmp/
The authenticity of host '192.168.188.130 (192.168.188.130)' can't be established.
ECDSA key fingerprint is SHA256:SL6ZOvpHHtEoro8AzeNBXqrM3mr2oXbPaSeXO+LQr1U.
ECDSA key fingerprint is MD5:26:0e:7a:96:d6:3a:c5:57:57:2b:6d:1a:1e:42:c9:01.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added '192.168.188.130' (ECDSA) to the list of known hosts.
[email protected]'s password: 
db1.sql                                            100% 1771   340.5KB/s   00:00    
test.sql                                           100% 1258     1.2MB/s   00:00    
zrlog.sql                                          100% 9873     5.9MB/s   00:00

4.进入mysql:创建表

mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)

mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

5.恢复备份的数据:

[root@weixing01 mysql]# mysql -uroot zrlog < /tmp/zrlog.sql 
[root@weixing01 mysql]# mysql -uroot db1 < /tmp/db1.sql 
[root@weixing01 mysql]# mysql -uroot weix < /tmp/zrlog.sql