mysql主从

avatar
作者
筋斗云
阅读量:30

1. 主从简介

在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一种隐患。

想几个问题:

1.1 主从作用1.2 主从形式

mysql privileges_mysql privileges_mysql privileges

2. 主从复制原理

mysql privileges_mysql privileges_mysql privileges

主从复制步骤:

3. 主从复制配置

主从复制配置步骤:

确保从数据库与主数据库里的数据一样在主数据库里创建一个同步账号授权给从数据库使用配置主数据库(修改配置文件)配置从数据库(修改配置文件)

需求:

搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作

环境说明:

数据库角色

IP

应用与系统版本

有无数据

主数据库

172.16.12.128

centos7/redhat7

mysql-5.7

有数据

从数据库

172.16.12.129

centos7/redhat7

mysql-5.7

无数据

3.1 mysql安装

分别在主从两台服务器上安装mysql-5.7版本,此处略过安装步骤,若有疑问请参考《mysql基础》与《mysql进阶》两篇文章。

3.2 mysql主从配置3.2.1 确保从数据库与主数据库里的数据一样

为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中

//先查看主库有哪些库

[root@localhost ~]# mysql -uroot -pwangqing123! -e 'show databases;'mysql: [Warning] Using a password on the command line interface can be insecure.

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

| Database |

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

| information_schema |

| mysql |

| performance_schema |

| student |

| sys |

| teacher |

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

//再查看从库有哪些库

[root@localhost ~]# mysql -uroot -pwangqing123! -e 'show databases;'mysql: [Warning] Using a password on the command line interface can be insecure.

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

| Database |

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

| information_schema |

| mysql |

| performance_schema |

| sys |

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

//全备主库//全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致

mysql> FLUSH TABLES WITH READ LOCK;

Query OK, 0 rows affected (0.00 sec)//此锁表的终端必须在备份完成以后才能退出

//备份主库并将备份文件传送到从库

[root@localhost ~]# mysqldump -uroot -pwangqing123! --all-databases > /opt/all-201808191200.sqlmysqldump: [Warning] Using a password on the command line interface can be insecure.

[root@localhost ~]# ls /opt/

all-201808191200.sql

[root@localhost ~]# scp /opt/all-201808191200.sql root@172.16.12.129:/opt/

root@172.16.12.129's password:

all-201808191200.sql 100% 786KB 10.6MB/s 00:00

//解除主库的锁表状态,直接退出交互式界面即可

mysql> quit

Bye

//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致

[root@localhost ~]# mysql -uroot -pwangqing123! < /opt/all-201808191200.sql

mysql: [Warning] Using a password on the command line interface can be insecure.

[root@localhost ~]# mysql -uroot -pwangqing123! -e 'show databases;'

mysql: [Warning] Using a password on the command line interface can be insecure.

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

| Database |

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

| information_schema |

| mysql |

| performance_schema |

| student |

| sys |

| teacher |

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

3.2.2 在主数据库里创建一个同步账号授权给从数据库使用

mysql> CREATE USER 'repl'@'172.16.12.129' IDENTIFIED BY 'repl123';

Query OK, 0 rows affected (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'172.16.12.129';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

3.2.3 配置主数据库

[root@localhost ~]# vim /etc/my.cnf//在[mysqld]这段的后面加上如下内容

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.socklog-bin=mysql-bin //启用binlog日志

server-id=1//数据库服务器唯一标识符,主库的server-id值必须比从库的大

symbolic-links=0

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

//重启mysql服务

[root@localhost ~]# systemctl restart mysqld

[root@localhost ~]# ss -antl

State Recv-Q Send-Q Local Address:Port Peer Address:Port

LISTEN 0128 *:22 *:*

LISTEN 0100127.0.0.1:25 *:*

LISTEN 0128 :::22 :::*

LISTEN 0100 ::1:25 :::*

LISTEN 080 :::3306 :::*

//查看主库的状态

mysql> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000001 | 154 | | | |

+------------------+----------+--------------+------------------+-------------------+1 row in set (0.01 sec)

3.2.4 配置从数据库

[root@localhost ~]# vim /etc/my.cnf//添加如下内容

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

server-id=2//设置从库的唯一标识符,从库的server-id值必须小于主库的该值

relay-log=mysql-relay-bin //启用中继日志relay-log

symbolic-links=0

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

//重启从库的mysql服务

[root@localhost ~]# systemctl restart mysqld

[root@localhost ~]# ss -antl

State Recv-Q Send-Q Local Address:Port Peer Address:Port

LISTEN 0128 *:22 *:*

LISTEN 0100127.0.0.1:25 *:*

LISTEN 0128 :::22 :::*

LISTEN 0100 ::1:25 :::*

LISTEN 080 :::3306 :::*

//配置并启动主从复制

mysql> CHANGE MASTER TO

-> MASTER_HOST='172.16.12.128',

-> MASTER_USER='repl',

-> MASTER_PASSWORD='repl123',

-> MASTER_LOG_FILE='mysql-bin.000001',

-> MASTER_LOG_POS=154;

Query OK, 0 rows affected, 2 warnings (0.33 sec)

mysql> start slave;

Query OK, 0 rows affected (0.01 sec)

//查看从服务器状态

mysql> show slave status \G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 172.16.12.128

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 154

Relay_Log_File: mysql-relay-bin.000002

Relay_Log_Pos: 320

Relay_Master_Log_File: mysql-bin.000001

Slave_IO_Running: Yes //此处必须为Yes

Slave_SQL_Running: Yes //此处必须为Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

3.2.5 测试验证

在主服务器的student库的bj2表中插入数据:

mysql> select * from bj2;Empty set (0.00 sec)

mysql> insert into bj2 values (1,'sean',20),(2,'tom',23),(3,'jerry',30);

Query OK, 3 rows affected (0.00 sec)

Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from bj2;

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

| id | name | age |

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

| 1 | sean | 20 |

| 2 | tom | 23 |

| 3 | jerry | 30 |

+----+-------+------+3 rows inset (0.00 sec)

在从数据库中查看数据是否同步:

mysql> use student;

Reading table information for completion of table and column names

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

Database changed

mysql> select * from bj2;

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

| id | name | age |

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

| 1 | sean | 20 |

| 2 | tom | 23 |

| 3 | jerry | 30 |

+----+-------+------+3 rows inset (0.00 sec)

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!