mysql主从复制配置过程及演示

1.设置server-id值并开启binlog功能参数

编辑/data/3306/my.cnf:

[mysqld]

server-id = 6                    #主库和从库的server-id不能相同

log_bin = /data/3306/mysql-bin   #

重启服务

/data/3306/mysql restart

检查思路1:

[[email protected] data]# egrep "log_bin|server-id" 330*/my.cnf

3306/my.cnf:log_bin = /data/3306/mysql-bin

3306/my.cnf:server-id = 6

3307/my.cnf:server-id = 7

检查思路2:

登录:

[[email protected] data]# mysql -S /data/3306/mysql.sock

查看变量:

mysql> show variables like ‘log_bin%‘;#查看主库的binlog是否存在

+---------------------------------+----------------------------+

| Variable_name                   | Value                      |

+---------------------------------+----------------------------+

| log_bin                         | ON                         |

| log_bin_basename                | /data/3306/mysql-bin       |

| log_bin_index                   | /data/3306/mysql-bin.index |

| log_bin_trust_function_creators | OFF                        |

| log_bin_use_v1_row_events       | OFF                        |

+---------------------------------+----------------------------+

5 rows in set (0.00 sec)

查看原主库数据库中原有库

[[email protected] data]# mysql   -S /data/3306/mysql.sock

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 41

Server version: 5.6.34-log Source distribution

Copyright (c) 2000, 2016, 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> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| cc2                |

| mysql              |

| performance_schema |

+--------------------+

4 rows in set (0.00 sec)

mysql>

查看从库中原有库

mysql> show  databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| cc                 |

| mysql              |

| performance_schema |

| test               |

+--------------------+

6 rows in set (0.00 sec)

mysql>

2、建账号授权【主库】

grant replication slave on *.* to ‘rep‘@‘172.16.1.%‘ identified by ‘cc123‘;

flush privileges;

3、锁表导出数据

mysql> flush table with read lock;

Query OK, 0 rows affected (0.00 sec)

查看位置:

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 |      405 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

新开窗口备份:

mysqldump -uroot -p‘cc123‘ -S /data/3306/mysql.sock -A -B |gzip >/server/backup/mysql_bak.$(date +%F).sql.gz

原窗口解锁:

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 |      405 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

mysql> unlock tables;

Query OK, 0 rows affected (0.01 sec)

如下命令可替代3的所有步骤

mysqldump -uroot -p‘cc123‘ --master-data=2 -S /data/3306/mysql.sock -A -B

zcat mysql_bak.2017-05-04.sql.gz >mysql_bak.2017-05-04.sql

4、将数据导入到从库

[[email protected] backup]# mysql -S /data/3307/mysql.sock <mysql_bak.2017-05-04.sql

5、让从库从主库锁表时刻记录的binlog位置点开始向下同步

CHANGE MASTER TO

MASTER_HOST=‘172.16.1.52‘,

MASTER_PORT=3306,

MASTER_USER=‘rep‘,

MASTER_PASSWORD=‘cc123‘,

MASTER_LOG_FILE=‘mysql-bin.000028‘,

MASTER_LOG_POS=1728;

#GTID

这时可见master.info已经产生在从库/data 目录下

[[email protected] data]# cd /data/3307/data/

[[email protected] data]# ls

auto.cnf    cc2          ib_logfile1  mysql             mysql-bin.000003  mysql-bin.000006  mysql-bin.000009  mysql-bin.000012  mysql-bin.000015  performance_schema

binlog.sql  ibdata1      ib_logfile2  mysql-bin.000001  mysql-bin.000004  mysql-bin.000007  mysql-bin.000010  mysql-bin.000013  mysql-bin.000016  test

cc          ib_logfile0  master.info  mysql-bin.000002  mysql-bin.000005  mysql-bin.000008  mysql-bin.000011  mysql-bin.000014  mysql-bin.index

[[email protected] data]#

6、启动同步开关

mysql> start slave;

Query OK, 0 rows affected (0.01 sec)

7、检查

[[email protected] backup]# mysql -S /data/3307/mysql.sock -e "show slave st

atus\G"|egrep -i "Yes|Behind_Master"

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Seconds_Behind_Master: 0

检查3307里是否同步主库

[[email protected] data]# mysql   -S /data/3307/mysql.sock

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 47

Server version: 5.6.34-log Source distribution

Copyright (c) 2000, 2016, 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> show  databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| cc                 |

| cc2                |

| mysql              |

| performance_schema |

| test               |

+--------------------+

6 rows in set (0.00 sec)

mysql>

在cc2库下student表中插入内容

这时我们在往3306里插入内容就会发现已经同步过来了

[[email protected] data]# mysql   -S /data/3306/mysql.sock

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 42

Server version: 5.6.34-log Source distribution

Copyright (c) 2000, 2016, 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> use cc2;

mysql> insert into student(`id`,`name`) VALUES(8,‘bb‘);

mysql> insert into student(`id`,`name`) VALUES(8,‘bb‘);

mysql> insert into student(`id`,`name`) VALUES(8,‘bb‘);

mysql> select * from student;

+----+------+-----+------+

| id | name | age | dept |

+----+------+-----+------+

|  5 | DD   |   0 | NULL |

|  6 | cc   |   0 | NULL |

|  7 | bb   |   0 | NULL |

|  8 | bb   |   0 | NULL |

|  8 | bb   |   0 | NULL |

|  8 | bb   |   0 | NULL |

|  8 | bb   |   0 | NULL |

+----+------+-----+------+

7 rows in set (0.00 sec)

[[email protected] data]# mysql   -S /data/3307/mysql.sock

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 48

Server version: 5.6.34-log Source distribution

Copyright (c) 2000, 2016, 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> use cc2;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> select * from student;

+----+------+-----+------+

| id | name | age | dept |

+----+------+-----+------+

|  5 | DD   |   0 | NULL |

|  6 | cc   |   0 | NULL |

|  7 | bb   |   0 | NULL |

|  8 | bb   |   0 | NULL |

|  8 | bb   |   0 | NULL |

|  8 | bb   |   0 | NULL |

+----+------+-----+------+

6 rows in set (0.00 sec)

mysql>

时间: 2024-10-06 08:26:38

mysql主从复制配置过程及演示的相关文章

Mysql主从复制原理过程

1.自个画的流程图. 2.简单描述mysql主从复制原理过程 2.1(对应上图步骤1) 在SLAVE服务器上执行start slave命令开启主从复制开关,开始进行主从复制. 2.2(对应上图步骤2) 此时,SLAVE服务器的I/O线程会通过在MASTER上已经授权的复制用户权限请求连接MASTER服务器,并请求从指定binlog日志文件的指定位置(日志文件名和位置就是在配置主从配置复制服务时执行change master命令指定的)之后开始发送binlog日志内容. 2.3(对应上图步骤3)

LINUX下PHP+MYSQL+APACHE配置过程

需要软件如下: apache: http://www.apache.org mysql: http://www.mysql.com php: http://www.php.net/downloads.php gd: http://www.boutell.com/gd/#buildgd ZendOptimizer http://www.zend.org/products/zend_optimizer Gettext http://ftp.gnu.org/pub/gnu/gettext/ netpb

mysql 主从复制配置详解

主从复制模型配置过程: 备注: 主节点IP地址是192.168.1.106   从节点的ip地址是192.168.1.107: 主节点: (1)启动二进制日志: 1-1 编辑配置文件 vim /etc/my.cnf配置如下所示: [mysqld] log-bin=master-bin server-id=1 innodb-file-per-table=ON skip_name_resolve=ON systemctl start mariadb.service  1-2 进入mysql使用 my

mysql主从复制配置操作以及主从宕机切换演练

主从复制目的: 主从服务器设置的稳健性得以提升,如果主服务器发生故障,可以把本来作为备份的从服务器提升为新的主服务器.在主从服务器上分开处理用户的请求,读的话,可以直接读取备机数据,可获得更短的响应时间. 主服务器:IP地址192.168.80.129,mysql已经安装,无用户数据. 从服务器:IP地址192.168.80.130,mysql已经安装. 注:数据库版本必须一致. 1.主从复制配置 修改从服务器的配置文件/etc/my.cnf,在mysqld里添加一下属性 [mysqld] lo

linux mysql主从复制配置

1.设置主库master的servie-id值并且开启bin-log功能参数vi /etc/my.cnf修改my.cnf的参数:[mysqld]server-id=1 //每一个库的server-id必须都不一样log-bin=mysql-bin 2.检查参数是否成功设置并重新启动egrep "server-id|log-bin" /etc/my.cnf重启命令见:mysql安装或mysql数据库多实例 3.登录数据库检查参数的更改情况show variables like 'serv

一.Mysql主从复制配置

在我之前的文章四·安装mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz(基于Centos7源码安装 和 九.mysql数据库多实例安装mysqld_multi [start,stop,report] 两篇文章写到了单太服务器多实例的安装,本篇文章是关于主从复制的配置.本次把mysql3306作为主Master,mysql3307和mysql3308作为Slave 一.启动3台mysql服务器 [[email protected] ~]$ mysqld_multi

mysql 主从复制配置

主从复制配置 步骤如下: 主服务器:从服务器ip地址分别为 [python] view plain copy 192.168.145.222.192.168.145.226 1.修改主服务器master: [python] view plain copy vi /etc/my.cnf [mysqld] log-bin=mysql-bin   #[必须]启用二进制日志 server-id=222      #[必须]服务器唯一ID,默认是1,一般取IP最后一段 2.修改从服务器slave: [py

mysql主从复制配置(精简版)

一.首先准备两台服务器,虚拟机即可,以笔者为例:master:192.168.1.105 slave:192.168.1.106 二.保证两台虚拟机能相互ping通,先把防火墙关闭:service iptables stop 三.安装mysql,可参考笔者linux 快速安装mysql 四.配置主从 编辑/etc/my.cnf文件 (1)配置master 添加如下配置: server-id = 1      #Server标识log-bin            #打开 MySQL 的 Bina

mysql主从复制配置

早就想写一篇关于数据库主从复制的文章,今天利用一些琐碎的时间操作了一遍并记录下来: 首先,我们必须思考,主从复制的大概步骤是什么,即使你不懂数据库这块,我想这个步骤好好想下还是知道的,我们知道数据库是跟用户关联的,那么首先必须建立一个用户,然后授权,这个用户在哪里建立呢?数据来源是主库,当然是在主库里面建立用户了,是的,然后需要利用数据库提供的命令在从库里面对在主库建立的用户一个授权操作,然后就OK了,大概步骤如下: 对于主库 编辑/etc/my.cnf,添加以下内容 vi /etc/my.cn