17.1 MySQL主从介绍17.2 准备工作17.3 配置主17.4 配置从17.5 测试主从同步

17.1 MySQL主从介绍

1. MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的

2. MySQL主从是基于binlog的,主上须开启binlog才能进行主从。

3. 主从过程大致有3个步骤

1)主将更改操作记录到binlog里

2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里

3)从根据relaylog里面的sql语句按顺序执行

4. 主上有一个log dump线程,用来和从的I/O线程传递binlog

5. 从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另外一个SQL线程用来把relaylog里面的sql语句落地

MySQL主从原理图 :

clipboard.png

 

1.在两个centos系统安装mysql,安装方法见 :

11.3 MySQL安装(上);11.4 MySQL安装(中);11.5 MySQL安装下

2.查看第一台和第二台服务器mysql是否启动。


[root@aminglinux-128 ~]# ps aux |grep mysql
root       1088  0.0  0.0 115432   288 ?        S    9月01   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/aminglinux-128.pid
mysql      1303  0.0 31.8 1313108 317424 ?      Sl   9月01   1:22 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=aminglinux-128.err --pid-file=/data/mysql/aminglinux-128.pid --socket=/tmp/mysql.sock --port=3306
root      24929  0.0  0.0 112720   984 pts/2    R+   16:26   0:00 grep --color=auto mysql
[root@localhost ~]# ps aux |grep mysql
root      12891  0.0  0.1  11812  1612 pts/0    S    16:28   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysqld.pid
mysql     13071 60.5 17.5 1119428 175124 pts/0  Sl   16:28   0:10 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=/data/mysql/mysqld.pid --socket=/tmp/mysql.sock --port=3306
root      13129  0.0  0.0 112720   980 pts/0    S+   16:28   0:00 grep --color=auto mysql

3.在第一台服务器上进行设置。

[root@aminglinux-128 ~]#  vim /etc/my.cnf

1.jpg

2. 重启mysql 

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

3. 查看/data/mysql/目录下,生成以(log_bin)haozc开头的文件 

[root@aminglinux-128 ~]# ls /data/mysql/aminglinux.*
/data/mysql/aminglinux.000001  /data/mysql/aminglinux.index

4. 进入 /data/mysql/目录 :

[root@aminglinux-128 ~]# cd /data/mysql/
[root@aminglinux-128 mysql]# 
[root@aminglinux-128 mysql]#

5. 创建一个新的库库名为aminglinux 

[root@aminglinux-128 mysql]# mysql -uroot -phaomima -e "create database aminglinux"
Warning: Using a password on the command line interface can be insecure.

ceshiku备份恢复aminglinux库,作为测试数据

6. 备份mysql数据库,做测试 

[root@aminglinux-128 mysql]#  mysqldump -uroot -phaomima ceshiku > /tmp/ceshiku.sql
Warning: Using a password on the command line interface can be insecure.

7. 备份的ceshiku.sql库文件,恢复到aminglinux库 

[root@aminglinux-128 mysql]# mysql -uroot -phaomima aminglinux < /tmp/ceshiku.sql

8. hao1机器主  所有mysql数据库备份/tmp/下 :

[root@aminglinux-128 mysql]# ls
aminglinux         aminglinux-128.err  aminglinux.index  ceshiku  ibdata1      ib_logfile1  mysql2              test
aminglinux.000001  aminglinux-128.pid  auto.cnf          db1      ib_logfile0  mysql        performance_schema

2.jpg

[root@aminglinux-128 mysql]# mysqldump -uroot -phaomima aminglinux > /tmp/haozc.sql
Warning: Using a password on the command line interface can be insecure.
[root@aminglinux-128 mysql]# mysqldump -uroot -phaomima mysql2 > /tmp/mysql2.sql
Warning: Using a password on the command line interface can be insecure.
[root@aminglinux-128 mysql]# mysqldump -uroot -phaomima db1 > /tmp/db1.sql
Warning: Using a password on the command line interface can be insecure.
[root@aminglinux-128 mysql]# mysqldump -uroot -phaomima ceshiku > /tmp/ceshiku.sql
Warning: Using a password on the command line interface can be insecure.

9. 进入mysql(root用户下):

[root@aminglinux-128 mysql]# mysql -uroot -phaomima
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.

10. 创建repl用户,针对ip是aminglinux2ip 

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

11. 表状态锁上暂时不能再写入数据 

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

12. 查看红框中两个数值hao2机器需要用这两个数值 

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

17.4 配置从

1. 编辑/etc/my.cnf配置文件 :

添加server-id=129

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

[client]
port            = 3306
socket          = /tmp/mysql.sock
[mysqld]
port                            = 3306
socket                          = /tmp/mysql.sock
server-id=129
basedir                              = /usr/local/mysql
datadir                              = /data/mysql
pid-file                             = /data/mysql/mysqld.pid
server-id                                                       = 306
character-set-server                 = utf8

2. 重启mysql 

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

3. scp命令复制aminglinux-128/tmp/所有以.sql备份的mysql数据库文件,粘贴本机amingliux-129/tmp/目录下 :

[root@localhost ~]# scp 192.168.193.128:/tmp/*.sql /tmp/
The authenticity of host '192.168.193.128 (192.168.193.128)' can't be established.
ECDSA key fingerprint is SHA256:/pKbINKTISanvNQ+5fJAqgFOnBJ7wbI68LKeFPZcVA0.
ECDSA key fingerprint is MD5:06:1f:8f:91:36:47:28:0c:72:08:6b:9f:28:b0:49:19.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.193.128' (ECDSA) to the list of known hosts.
[email protected]'s password: 
ceshiku.sql                                                                                           100% 1807   879.9KB/s   00:00    
db1.sql                                                                                               100% 1258   890.6KB/s   00:00    
haozc.sql                                                                                             100% 1265   856.6KB/s   00:00    
mysql2.sql                                                                                            100% 1261   773.6KB/s   00:00    
mysql.sql                                                                                             100%  646KB  10.0MB/s   00:00    
user.sql

4. 如果执行找不到mysql -uroot命令,执行下面命令 :

[root@aminglinux-129 ~]# alias 'mysql=/usr/local/mysql/bin/mysql'

[root@aminglinux-129 ~]# alias 'mysqldump=/usr/local/mysql/bin/mysqldump'

[root@aminglinux-129~]# ln -s /usr/local/mysql/bin/mysql  /usr/bin


5. 登录mysql(登录root用户) :

mysql -uroot -phaomima

6. hao2创建hao1主一样的mysql用户haozc 

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

mysql> create database mysql2;
ERROR 1007 (HY000): Can't create database 'mysql2'; database exists
mysql> create database mysql2;
ERROR 1007 (HY000): Can't create database 'mysql2'; database exists
mysql> create database ceshiku;
ERROR 1007 (HY000): Can't create database 'ceshiku'; database exists
mysql>  create database zrlog;
Query OK, 1 row affected (0.00 sec)

7. 把aminglinux-128机器 同步.sql 库文件,恢复到创建对应名称库下 :

mysql> mysql -uroot zrlog < /tmp/zrlog.sql

不往下运行

8. 登录mysql执行下面命令 :

mysql>  change master to master_host='192.168.193.128',master_user='repl', master_password='haomima', master_log_file='aminglinux.000001'', master_log_pos=444;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> stop slave
    -> start slave
mysql> show slave status\G

3.jpg

10. 在aminglinux-128机器上,把之前锁定解锁(恢复写入的操作) 

[root@aminglinux-128 ~]# mysql -uroot -phaomima
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.39-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

17.5 测试主从同步

配置参数

1. 服务器上 :

binlog-do-db=      //仅同步指定的库(其他库不同步)

binlog-ignore-db= //忽略指定库(其他库都同步)

2. 服务器上 :

replicate_do_db=   //(不常用)

replicate_ignore_db=   //(不常用)

replicate_do_table=   //(不常用)

replicate_ignore_table=   //(不常用)

replicate_wild_do_table=   //如aming.%, (支持通配符%)

replicate_wild_ignore_table=

测试主从:

1. aminglinux-128机器上操作 :mysql -uroot -phaomima

切换数据库 

mysql> use aminglinux
Database changed



猜你喜欢

转载自blog.51cto.com/13107353/2169830