mysql主从配置

Posted 逍遥子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql主从配置相关的知识,希望对你有一定的参考价值。

1.配置环境:
Centos 7 64位 基础网络版  mysql5.7

2.hosts文件
[[email protected] log]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6


172.16.160.201 master
172.16.160.202 slave

3.配置文件修改
[[email protected] log]# cat /etc/my.cnf

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

# zhuyr
character_set_server=utf8
init_connect=‘SET NAMES utf8‘

user=mysql
log-bin = master-bin
log-bin-index = master-bin.index
server_id = 1
innodb_log_file_size = 256M
expire-logs-days = 1
port = 3306
tmpdir = /tmp
basedir = /usr


注:log-bin、log-bin-index如不提供值,默认值为hostname-bin。为了避免由于系统名更改可能会日志文件名的混乱,所以最好为服务器创建一个机器
人无关的唯一的服务器名。

[[email protected] log]# cat /etc/my.cnf
[mysqld]

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

# zhuyr
character_set_server=utf8
init_connect=‘SET NAMES utf8‘

user=mysql
basedir = /usr
tmpdir = /tmp
port = 3306
server_id = 2
relay_log_index = slave_relay_bin.index
relay_log = slave_relay_bin
innodb_log_file_size = 256M
expire-logs-days = 1

[[email protected] log]#

4.建立主从连接
在master创建一个复制用户
mysql> grant replication slave on *.* to [email protected] identified by ‘Maks.123‘;
完成用户的创建后,请记得刷新系统权限表;
mysql>flush privileges;


在slave端执行设备连接
--使用change master to命令指向Master
mysql> change master to master_host=‘master‘, master_port=3306,master_user=‘repl_user‘,master_password=‘Maks.123‘;
--启动复制
mysql> start slave;


5.测试复制

--在Master端创建数据库或表
mysql> create database licz;
Query OK, 1 row affected (0.00 sec)

--在Slave端查看是否同步
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)


[[email protected] log]# cat /var/log/mysqld.log

2017-04-12T07:37:07.081984Z 1 [ERROR] Slave I/O for channel ‘‘: error connecting to master ‘[email protected]:3306‘ - retry-time: 60
retries: 8, Error_code: 1130
2017-04-12T07:38:07.082880Z 1 [ERROR] Slave I/O for channel ‘‘: error connecting to master ‘[email protected]:3306‘ - retry-time: 60
retries: 9, Error_code: 1130
2017-04-12T07:38:56.190835Z 7 [Note] Access denied for user ‘[email protected]‘@‘localhost‘ (using password: YES)
2017-04-12T07:39:07.083771Z 1 [ERROR] Slave I/O for channel ‘‘: error connecting to master ‘[email protected]:3306‘ - retry-time: 60
retries: 10, Error_code: 1130
2017-04-12T07:40:07.084596Z 1 [ERROR] Slave I/O for channel ‘‘: error connecting to master ‘[email protected]:3306‘ - retry-time: 60
retries: 11, Error_code: 1130
2017-04-12T07:41:07.085259Z 1 [Note] Slave I/O thread for channel ‘‘: connected to master ‘[email protected]:3306‘,replication started
in log ‘FIRST‘ at position 4

 













































































以上是关于mysql主从配置的主要内容,如果未能解决你的问题,请参考以下文章

mysql主从配置

Mysql主从配置

8.31 MySQL主从配置准备工作配置主配置从测试主从同步

MySQL传统主从配置

Redis主从复制的配置过程

MySQL的主从介绍配置主配置从测试主从同步