MySQL5.7 Replication主从复制配置教程

Posted

tags:

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

       最近配置mysql5.7主从复制的时候碰到了些问题,老老实实按老版本的步骤配置会有错误,所以下面介绍一下5.7的配置步骤,

大体步骤跟老版本的还是一样的,只是有一些小区别,不说了,言归正传。

    官方文档:http://dev.mysql.com/doc/refman/5.7/en/replication.html

    系统环境:win7


我只安装了核心的server,其他配套工具没安装。安装后发现根目录下有一个my-default.ini文件,我就把该文件改名成my.ini,打开这个
文件文件内容如下

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. Its a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# server_id = .....
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# bin log file name of the log to create
# log_bin= .....
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

把如下个属性改动一下,具体如下

server_id = 101 注意这个101是唯一标识你这个mysql服务器的,一般取你电脑ip的最后几位,以便好区分。
log_bin=mysql-bin 注意mysql-bin是一个存放二进制log文件的路径,我这里指定了一个mysql当前安装目录的mysql-bin文件夹
port=3306     3306是你设定的端口

改完之后在mysql command line client(mysql命令行客户端)执行如下两个命令中一个,初始化data目录

在window系统:
mysqld --initialize-insecure; 解释:自动生成无密码的root用户,
mysqld --initialize; 解释: 自动生成带随机密码的root用户,
在 Unix 或者Unix相关,相似的系统:
mysqld --initialize --user=mysql
bin/mysqld --initialize-insecure --user=mysql

如上操作完成后,需要用上面生存的用户名登录服务,如下

 

  • 如果你用 --initialize 初始化data目录,请用如下命令登录服务

     mysql -u root -p
    输入随机生成的密码

    如果你不知道这个随机密码,请查看error log文件

  • 如果用 --initialize-insecure 初始化data目录,请用root用登录,并不需要输入密码就可以登录,如下命令:

     mysql -u root --skip-password

用没有密码的root用户登录后可以给这个用户设置密码,设置方法如下命令:

 ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘new_password‘;

另外,如果 mysqld 无法识别你的mysql安装目录和mysql数据存放目录,请手工指定:方法有二

第一种:即在输入命令的时候多加两个参数--basedir和--datadir,具体如下

mysqld --initialize --user=mysql
         --basedir=your mysql install dir e.g:/opt/mysql/mysql
        --datadir=your mysql data dir e.g:/opt/mysql/mysql/data

第二种:在你的my.ini文件增加两个属性:

basedir= you mysql install dir e.g:/opt/mysql/mysql
datadir=you mysql data dir e.g:/opt/mysql/mysql/data

更详细参考:http://dev.mysql.com/doc/refman/5.7/en/data-directory-initialization-mysqld.html

 配置完后你可以重启服务了,命令:mysqld restart --console;或者手动关掉 mysqld进程后执行命令:mysqld --console;,请用同样的方法设置 master和slave

但是两者的端口要不同。

 

        至此,剩下的操作跟老版本一样了,剩下的操作请参考其他文章,例如[z]如何在一台windows主机上搭建mysql主从复制 这篇文件,从如下文字开始:

3、在主库添加一个用户 repl 并指定replication权限
create user repl@127.0.0.1 identified by asdf;

GRANT REPLICATION SLAVE ON *.* TO repl@127.0.0.1; -- --这里我指定数据库(test.*)时报错,而指定全库(*.*)时会成功。

4、保持主从mysql的test数据库初始状态一致。

.....................

 

附:

1、用 show variables like ‘%log_bin%‘;查看状态:

配置失败的状态如下:

+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| log_bin | OFF |
| log_bin_basename | |
| log_bin_index | |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+-------+
6 rows in set, 1 warning (0.01 sec)

 

配置成功的类似是如下:

+---------------------------------+---------------------------------------------
-----------------+
| Variable_name | Value
|
+---------------------------------+---------------------------------------------
-----------------+
| log_bin | ON
|
| log_bin_basename | C:\\Program Files\\MySQL\\MySQL Server 5.7\\data
\\mysql-bin |
| log_bin_index | C:\\Program Files\\MySQL\\MySQL Server 5.7\\data
\\mysql-bin.index |
| log_bin_trust_function_creators | OFF
|
| log_bin_use_v1_row_events | OFF
|
| sql_log_bin | ON
|
+---------------------------------+---------------------------------------------
-----------------+
6 rows in set, 1 warning (0.01 sec)

 

 

2、如果执行mysqld --console,报类似如下错,手动停掉mysqld进程后再执行。

2016-02-06T13:23:13.951203Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is
deprecated. Please use --explicit_defaults_for_timestamp server option (see doc
umentation for more details).
2016-02-06T13:23:13.951203Z 0 [Warning] NO_ZERO_DATE, NO_ZERO_IN_DATE and E
RROR_FOR_DIVISION_BY_ZERO sql modes should be used with strict mode. They will
be merged with strict mode in a future release.
2016-02-06T13:23:13.951203Z 0 [Warning] NO_AUTO_CREATE_USER sql mode was not s
et.
2016-02-06T13:23:13.951203Z 0 [Warning] Insecure configuration for --secure-file
-priv: Current value does not restrict location of generated files. Consider set
ting it to a valid, non-empty path.
2016-02-06T13:23:13.952203Z 0 [Note] mysqld (mysqld 5.7.10-log) starting as proc
ess 38432 ...
2016-02-06T13:23:13.976205Z 0 [Note] InnoDB: Mutexes and rw_locks use Windows in
terlocked functions
2016-02-06T13:23:13.976205Z 0 [Note] InnoDB: Uses event mutexes
2016-02-06T13:23:13.977205Z 0 [Note] InnoDB: Memory barrier is not used
2016-02-06T13:23:13.977205Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-02-06T13:23:13.977205Z 0 [Note] InnoDB: Number of pools: 1
2016-02-06T13:23:13.978205Z 0 [Note] InnoDB: Not using CPU crc32 instructions
2016-02-06T13:23:14.005206Z 0 [Note] InnoDB: Initializing buffer pool, total siz
e = 128M, instances = 1, chunk size = 128M
2016-02-06T13:23:14.019207Z 0 [Note] InnoDB: Completed initialization of buffer
pool
2016-02-06T13:23:14.092211Z 0 [Note] InnoDB: Highest supported file format is Ba
rracuda.
2016-02-06T13:23:14.239220Z 0 [Note] InnoDB: Creating shared tablespace for temp
orary tables
2016-02-06T13:23:14.240220Z 0 [Note] InnoDB: Setting file .\\ibtmp1 size to 12
MB. Physically writing the file full; Please wait ...
2016-02-06T13:23:14.450232Z 0 [Note] InnoDB: File .\\ibtmp1 size is now 12 MB.
2016-02-06T13:23:14.453232Z 0 [Note] InnoDB: 96 redo rollback segment(s) found.
96 redo rollback segment(s) are active.
2016-02-06T13:23:14.453232Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are
active.
2016-02-06T13:23:14.463233Z 0 [Note] InnoDB: 5.7.10 started; log sequence number
1210265
2016-02-06T13:23:14.464233Z 0 [Note] InnoDB: Loading buffer pool(s) from C:\\Prog
ram Files\\MySQL\\MySQL Server 5.7\\data\\ib_buffer_pool
2016-02-06T13:23:14.464233Z 0 [Note] Plugin FEDERATED is disabled.
2016-02-06T13:23:14.464233Z 0 [Note] InnoDB: not started
mysqld: Table mysql.plugin doesnt exist
2016-02-06T13:23:14.465233Z 0 [ERROR] Cant open the mysql.plugin table. Please
run mysql_upgrade to create it.
2016-02-06T13:23:14.478233Z 0 [Note] InnoDB: Buffer pool(s) load completed at 16
0206 21:23:14
2016-02-06T13:23:14.586240Z 0 [Warning] Gtid table is not ready to be used. Tabl
e mysql.gtid_executed cannot be opened.
2016-02-06T13:23:14.609241Z 0 [Warning] Failed to set up SSL because of the foll
owing SSL library error: SSL context is not usable without certificate and priva
te key
2016-02-06T13:23:14.609241Z 0 [Note] Server hostname (bind-address): *; port:
3306
2016-02-06T13:23:14.611241Z 0 [Note] IPv6 is available.
2016-02-06T13:23:14.612241Z 0 [Note] - :: resolves to ::;
2016-02-06T13:23:14.612241Z 0 [Note] Server socket created on IP: ::.
2016-02-06T13:23:14.615241Z 0 [Warning] Failed to open optimizer cost constant t
ables

2016-02-06T13:23:14.617241Z 0 [ERROR] Fatal error: Cant open and lock privilege
tables: Table mysql.user doesnt exist
2016-02-06T13:23:14.619242Z 0 [ERROR] Aborting

2016-02-06T13:23:14.619242Z 0 [Note] Binlog end
2016-02-06T13:23:14.640243Z 0 [Note] Shutting down plugin ngram
2016-02-06T13:23:14.640243Z 0 [Note] Shutting down plugin partition
2016-02-06T13:23:14.640243Z 0 [Note] Shutting down plugin BLACKHOLE
2016-02-06T13:23:14.641243Z 0 [Note] Shutting down plugin ARCHIVE
2016-02-06T13:23:14.641243Z 0 [Note] Shutting down plugin PERFORMANCE_SCHEMA
2016-02-06T13:23:14.641243Z 0 [Note] Shutting down plugin MRG_MYISAM
2016-02-06T13:23:14.641243Z 0 [Note] Shutting down plugin MyISAM
2016-02-06T13:23:14.641243Z 0 [Note] Shutting down plugin INNODB_SYS_VIRTUAL
2016-02-06T13:23:14.642243Z 0 [Note] Shutting down plugin INNODB_SYS_DATAFILES

2016-02-06T13:23:14.642243Z 0 [Note] Shutting down plugin INNODB_SYS_TABLESPACE
S2016-02-06T13:23:14.643243Z 0 [Note] Shutting down plugin INNODB_SYS_FOREIGN_CO
LS2016-02-06T13:23:14.643243Z 0 [Note] Shutting down plugin INNODB_SYS_FOREIGN
2016-02-06T13:23:14.643243Z 0 [Note] Shutting down plugin INNODB_SYS_FIELDS
2016-02-06T13:23:14.643243Z 0 [Note] Shutting down plugin INNODB_SYS_COLUMNS
2016-02-06T13:23:14.643243Z 0 [Note] Shutting down plugin INNODB_SYS_INDEXES
2016-02-06T13:23:14.644243Z 0 [Note] Shutting down plugin INNODB_SYS_TABLESTATS2016-02-06T13:23:14.644243Z 0 [Note] Shutting down plugin INNODB_SYS_TABLES
2016-02-06T13:23:14.644243Z 0 [Note] Shutting down plugin INNODB_FT_INDEX_TABLE2016-02-06T13:23:14.644243Z 0 [Note] Shutting down plugin INNODB_FT_INDEX_CACHE2016-02-06T13:23:14.644243Z 0 [Note] Shutting down plugin INNODB_FT_CONFIG
2016-02-06T13:23:14.645243Z 0 [Note] Shutting down plugin INNODB_FT_BEING_DELET
ED2016-02-06T13:23:14.645243Z 0 [Note] Shutting down plugin INNODB_FT_DELETED
2016-02-06T13:23:14.645243Z 0 [Note] Shutting down plugin INNODB_FT_DEFAULT_STO
PWORD2016-02-06T13:23:14.645243Z 0 [Note] Shutting down plugin INNODB_METRICS
2016-02-06T13:23:14.645243Z 0 [Note] Shutting down plugin INNODB_TEMP_TABLE_INF
O2016-02-06T13:23:14.646243Z 0 [Note] Shutting down plugin INNODB_BUFFER_POOL_ST
ATS2016-02-06T13:23:14.646243Z 0 [Note] Shutting down plugin INNODB_BUFFER_PAGE_LR
U2016-02-06T13:23:14.646243Z 0 [Note] Shutting down plugin INNODB_BUFFER_PAGE
2016-02-06T13:23:14.646243Z 0 [Note] Shutting down plugin INNODB_CMP_PER_INDEX_
RESET2016-02-06T13:23:14.646243Z 0 [Note] Shutting down plugin INNODB_CMP_PER_INDEX

2016-02-06T13:23:14.647243Z 0 [Note] Shutting down plugin INNODB_CMPMEM_RESET
2016-02-06T13:23:14.647243Z 0 [Note] Shutting down plugin INNODB_CMPMEM
2016-02-06T13:23:14.647243Z 0 [Note] Shutting down plugin INNODB_CMP_RESET
2016-02-06T13:23:14.647243Z 0 [Note] Shutting down plugin INNODB_CMP
2016-02-06T13:23:14.647243Z 0 [Note] Shutting down plugin INNODB_LOCK_WAITS
2016-02-06T13:23:14.648243Z 0 [Note] Shutting down plugin INNODB_LOCKS
2016-02-06T13:23:14.648243Z 0 [Note] Shutting down plugin INNODB_TRX
2016-02-06T13:23:14.648243Z 0 [Note] Shutting down plugin InnoDB
2016-02-06T13:23:14.648243Z 0 [Note] InnoDB: FTS optimize thread exiting.
2016-02-06T13:23:14.648243Z 0 [Note] InnoDB: Starting shutdown...
2016-02-06T13:23:14.748249Z 0 [Note] InnoDB: Dumping buffer pool(s) to C:\\Progra
m Files\\MySQL\\MySQL Server 5.7\\data\\ib_buffer_pool
2016-02-06T13:23:14.751249Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 16
0206 21:23:14
2016-02-06T13:23:16.137328Z 0 [Note] InnoDB: Shutdown completed; log sequence nu
mber 1210284
2016-02-06T13:23:16.138328Z 0 [Note] InnoDB: Removed temporary tablespace data f
ile: "ibtmp1"
2016-02-06T13:23:16.138328Z 0 [Note] Shutting down plugin MEMORY
2016-02-06T13:23:16.138328Z 0 [Note] Shutting down plugin CSV
2016-02-06T13:23:16.139328Z 0 [Note] Shutting down plugin sha256_password
2016-02-06T13:23:16.139328Z 0 [Note] Shutting down plugin mysql_native_password2016-02-06T13:23:16.139328Z 0 [Note] Shutting down plugin binlog
2016-02-06T13:23:16.139328Z 0 [Note] mysqld: Shutdown complete

 原创文章,转载请注明:http://www.cnblogs.com/langtianya/p/5185577.html 

以上是关于MySQL5.7 Replication主从复制配置教程的主要内容,如果未能解决你的问题,请参考以下文章

MySQL主从复制(Replication for Backup)

Mysql5.7 ssl主从复制

MySQL Replication--复制异常1

MySQL 5.7 Replication 相关新功能说明

MySQL5.7新特性:lossless replication 无损复制

mysql 5.7 centos 7 主从安装配置详解 .md