Ubuntu16.04搭建 Mysql 5.7 主从服务器

Ubuntu16.04搭建 Mysql 5.7 主从服务器

参考文章:
https://www.cnblogs.com/zhenyuyaodidiao/p/4635458.html
https://www.cnblogs.com/lixiaolun/p/7797997.html

1.两台mysql服务器安装同一版本mysql数据库:

$ sudo apt install mysql mysql-server

2.启动mysql服务:

$ sudo service mysql start

3.分别在两个数据库中创建同样的库和表(创建库和表在此略过)

主mysql服务器(192.168.20.190这台服务器):

4.修改主mysql配置文件, 支持主从模式, 主要配置如下几个参数(binlog_do_db记录了需要做主从同步的数据库名称):

$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

添加如下内容:
server-id = 190
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = flarum

5.重启主数据库

$ sudo service mysql restart

6.登录主mysql服务器, 并在mysql控制台下输入授权主从模式的命令(此处使用的是root用户, 授权192.168.20.40使用flarum库, 如果想凭用户密码登录, 可以授权’%’)

$ mysql -u root -p

mysql> grant replication slave, replication client on *.* to 'root'@'192.168.20.40' identified by 'p@ssw0rd';
mysql> grant all on flarum.* to root@'192.168.20.40' identified by 'p@ssw0rd';
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | flarum | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

从mysql服务器(192.168.20.40这台服务器):

7.修改从mysql配置文件, 支持主从模式, 主要配置如下几个参数(binlog_do_db记录了需要做主从同步的数据库名称):

$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

添加如下内容:
server-id = 40
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = flarum

8.登录从mysql服务器, 并在mysql控制台下输入匹配主从模式的命令(此处使用的是root用户, 将当前数据库作为从库匹配到主数据库192.168.20.190的mysql服务上)

$ mysql -u root -p

mysql> change master to master_host='192.168.20.190', master_user='root', master_password='p@ssw0rd', master_log_file='mysql-bin.000001', master_log_pos=154;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.20.190
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: devops-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1146
Last_Error: Error executing row event: 'Table 'flarum.flarum_users' doesn't exist'
Skip_Counter: 0
Exec_Master_Log_Pos: 154
Relay_Log_Space: 31387
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 1146
Last_SQL_Error: Error executing row event: 'Table 'flarum.flarum_users' doesn't exist'
Replicate_Ignore_Server_Ids:
Master_Server_Id: 190
Master_UUID: c13d4190-5fbf-11e8-96d4-408d5ca4c1e2
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp: 180626 06:03:14
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

注:Slave_IO及 Slave_SQL进程必须正常运行,即 YES状态,否则都是错误的状态。

9.重启从数据库

$ sudo service mysql restart

至此 mysql主从服务已经搭建完成

猜你喜欢

转载自blog.csdn.net/xls6006/article/details/80848626