Mysql主从配置,一主一从

十年热恋 提交于 2020-02-26 00:41:21

主库IP:192.168.1.156 Hostname:CNDB01
从库IP:192.168.1.157 Hostname:CNDB02
系统:Centos7.4
Mysql:Mariadb 5.7

1.安装:
yum -y install mysql

2.配置主服务器mysql文件:
#vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
[mysqld]
server-id = 1 #主为1
log-bin = mysql-bin #开启二进制日志
skip-gran-tables #开启无密码登录,不启用会有可能连接不上
!includedir /etc/my.cnf.d

3.配置数据库参数,创建sync同步账户,并且授权slave权限
#systemctl start mysql
#mysql -u root -p
MySQL [(none)]> grant replication slave on . to 'sync'@'192.168.1.156' identified by 'Aa123456!';
MySQL [(none)]>exit
设置完重启数据库
#systemctl stop mysql
#systemctl start mysql

4.再次连接数据库查看设置好的主服务请的参数,记住表中的2个参数,需要在从服务器中用到
#mysql -u root -p
MySQL [(none)]> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

5.从库配置安装:
yum -y install mysql

6.配置主服务器mysql文件:
#systemctl start mysql
#vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
[mysqld]
server-id = 2 #从为2
!includedir /etc/my.cnf.d

7.设置完重启数据库
#systemctl stop mysql
#systemctl start mysql

8.连接数据库配置数据库
#systemctl start mysql
#mysql -u root -p
MySQL [(none)]> change master to
master_host='192.168.1.156',master_user='sync',master_password=' Aa123456!',master_log_file='mysql-bin.000004',master_log_pos=154;
MySQL [(none)]>slave start;
MySQL [(none)]>show slave status\G (\G不要加分号;)

9测试:
在主服务器上新建库和表
#mysql -uroot -p
MySQL [(none)]> create database mysql_test charset=utf8;
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mysql_test |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)

MySQL [(none)]> use mysql_test;
Database changed
MySQL [mysql_test]> create table t0 (id varchar(20),name varchar(30));
Query OK, 0 rows affected (0.00 sec)

MySQL [mysql_test]> show tables;
+----------------------+
| Tables_in_mysql_test |
+----------------------+
| t0 |
+----------------------+
1 row in set (0.00 sec)

在从库上查询

Mysql主从配置,一主一从

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!