首页 > 代码库 > mysql主从配置
mysql主从配置
1.配置环境:
Centos 7 64位 基础网络版 mysql5.7
2.hosts文件
[root@mysql02 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.配置文件修改
[root@mysql01 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。为了避免由于系统名更改可能会日志文件名的混乱,所以最好为服务器创建一个机器
人无关的唯一的服务器名。
[root@mysql02 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
[root@mysql02 log]#
4.建立主从连接
在master创建一个复制用户
mysql> grant replication slave on *.* to repl_user@slave 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)
[root@mysql02 log]# cat /var/log/mysqld.log
2017-04-12T07:37:07.081984Z 1 [ERROR] Slave I/O for channel ‘‘: error connecting to master ‘repl_user@master: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 ‘repl_user@master:3306‘ - retry-time: 60
retries: 9, Error_code: 1130
2017-04-12T07:38:56.190835Z 7 [Note] Access denied for user ‘root@master‘@‘localhost‘ (using password: YES)
2017-04-12T07:39:07.083771Z 1 [ERROR] Slave I/O for channel ‘‘: error connecting to master ‘repl_user@master: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 ‘repl_user@master: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 ‘repl_user@master:3306‘,replication started
in log ‘FIRST‘ at position 4
mysql主从配置