教程MySQL配置主从同步
Posted 方块云计算
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了教程MySQL配置主从同步相关的知识,希望对你有一定的参考价值。
今天小编为大家讲解一下如何在Centos7上面配置mysql主从同步。此教程为初级教程,不感兴趣请自觉跳过!
环境:virtualBox + CentOS7 + MariaDB
1、配置固定IP
vim /etc/sysconfig/network-scripts/ifcfg-enp0s3
#修改以下内容
BOOTPROTO=static
ONBOOT=on #开启启动
#添加以下内容
IPADDR=192.168.232.21
GETWAY=192.168.232.1
NETMASK=255.255.255.0
主服务器IP:192.168.232.21,从服务器IP:192.168.232.22
2、安装MariaDB
yum install mariadb mariadb.server
#启动数据库
systemctl enable mariadb.service #开机启动
systemctl [start|stop|restart] mariadb.service #开启、关闭、重启服务器
#设置密码
mysqladmin -u root password 123456
3、创建测试数据库
mysql -uroot -p123456
create database if not exists demo collate='utf8_general_ci' character set='utf8';
4、主数据库配置
vim /etc/my.cnf
#在 [mysqld]下 添加以下内容
server-id=1
binlog-do-db=demo
relay-log=/var/lib/mysql/mysql-relay-bin
relay-log-index=/var/lib/mysql/mysql-relay-bin.index
log-error=/var/lib/mysql/mysql.err
master-info-file=/var/lib/mysql/mysql-master.info
relay-log-info-file=/var/lib/mysql/mysql-relay-log.info
log-bin=/var/lib/mysql/mysql-bin
#重启mysql
systemctl restart mariadb.service
#登录mysql
mysql -uroot -proot
#赋予从数据库角色权限
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'192.168.232.22' IDENTIFIED BY '123456';
#刷新权限
FLUSH PRIVILEGES;
#刷新表读锁权限
FLUSH TABLES WITH READ LOCK;
#显示状态
show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 481 | demo | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
5、从服务器配置
vim /etc/my.cnf
#在 [mysqld]下 添加以下内容
server-id=2
replicate-do-db=demo
relay-log=/var/lib/mysql/mysql-relay-bin
relay-log-index=/var/lib/mysql/mysql-relay-bin.index
log-error=/var/lib/mysql.err
master-info-file=/var/lib/mysql/mysql-master.info
relay-log-info-file=/var/lib/mysql/mysql-relay-log.info
log-bin=/var/lib/mysql/mysql-bin
#重启
systemctl restart mariadb.service
6、连接主服务器
mysql -uroot -p123456
#关闭slave
stop slave;
#设置连接主服务信息
change master to master_host='192.168.232.21',
master_user='slave_user',
master_password='123456',
master_log_file='mysql-bin.000001',
master_log_pos=481;
#启动slave
start slave;
#查看状态
show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.232.21
Master_User: slave_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 481
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: demo
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 481
Relay_Log_Space: 823
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: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
7、验证
#主数据库创建测试表
use demo
create table if not exists test(id int(1) primary key auto_increment not null);
#打开从服务器看是否创建test表
use demo
show tables;
+----------------+
| Tables_in_demo |
+----------------+
| test |
+----------------+
1 row in set (0.00 sec)
至此,mysql主从服务器配置成功。大家有时间最好自己实际操作一下,很多东西只有在实际操作的时候才能体会。
方块云原创,更多内容请关注微信:fktech,转载请注明出处,谢谢!
以上是关于教程MySQL配置主从同步的主要内容,如果未能解决你的问题,请参考以下文章