最近配置mysql5.7主从复制的时候碰到了些问题,老老实实按老版本的步骤配置会有错误,后来自己查看了官方文档,才解决了问题,在这里总结一下5.7的配置步骤,
大体步骤跟老版本的还是一样的,只是有一些小区别,下面进行具体介绍。
官方文档:http://dev.mysql.com/doc/refman/5.7/en/replication.html
系统环境:win7
我只安装了mysql 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. It's 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 % of total RAM for dedicated server, else %.
# 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是唯一标识你这个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; 解释:初始化data目录的同时,会自动生成无密码的root用户,
mysqld --initialize; 解释:初始化data目录的同时,会自动生成带随机密码的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主从复制 这篇文件,从如下文字开始:
、在主库添加一个用户 repl 并指定replication权限
create user 'repl'@'127.0.0.1' identified by 'asdf'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'127.0.0.1'; -- --这里我指定数据库(test.*)时报错,而指定全库(*.*)时会成功。 、保持主从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进程后再执行。
--06T13::.951203Z [Warning] TIMESTAMP with implicit DEFAULT value is
deprecated. Please use --explicit_defaults_for_timestamp server option (see doc
umentation for more details).
--06T13::.951203Z [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.
--06T13::.951203Z [Warning] 'NO_AUTO_CREATE_USER' sql mode was not s
et.
--06T13::.951203Z [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.
--06T13::.952203Z [Note] mysqld (mysqld 5.7.-log) starting as proc
ess ...
--06T13::.976205Z [Note] InnoDB: Mutexes and rw_locks use Windows in
terlocked functions
--06T13::.976205Z [Note] InnoDB: Uses event mutexes
--06T13::.977205Z [Note] InnoDB: Memory barrier is not used
--06T13::.977205Z [Note] InnoDB: Compressed tables use zlib 1.2.
--06T13::.977205Z [Note] InnoDB: Number of pools:
--06T13::.978205Z [Note] InnoDB: Not using CPU crc32 instructions
--06T13::.005206Z [Note] InnoDB: Initializing buffer pool, total siz
e = 128M, instances = , chunk size = 128M
--06T13::.019207Z [Note] InnoDB: Completed initialization of buffer
pool
--06T13::.092211Z [Note] InnoDB: Highest supported file format is Ba
rracuda.
--06T13::.239220Z [Note] InnoDB: Creating shared tablespace for temp
orary tables
--06T13::.240220Z [Note] InnoDB: Setting file '.\ibtmp1' size to
MB. Physically writing the file full; Please wait ...
--06T13::.450232Z [Note] InnoDB: File '.\ibtmp1' size is now MB.
--06T13::.453232Z [Note] InnoDB: redo rollback segment(s) found.
redo rollback segment(s) are active.
--06T13::.453232Z [Note] InnoDB: non-redo rollback segment(s) are
active.
--06T13::.463233Z [Note] InnoDB: 5.7. started; log sequence number --06T13::.464233Z [Note] InnoDB: Loading buffer pool(s) from C:\Prog
ram Files\MySQL\MySQL Server 5.7\data\ib_buffer_pool
--06T13::.464233Z [Note] Plugin 'FEDERATED' is disabled.
--06T13::.464233Z [Note] InnoDB: not started
mysqld: Table 'mysql.plugin' doesn't exist
--06T13::.465233Z [ERROR] Can't open the mysql.plugin table. Please
run mysql_upgrade to create it.
--06T13::.478233Z [Note] InnoDB: Buffer pool(s) load completed at
::
--06T13::.586240Z [Warning] Gtid table is not ready to be used. Tabl
e 'mysql.gtid_executed' cannot be opened.
--06T13::.609241Z [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
--06T13::.609241Z [Note] Server hostname (bind-address): '*'; port: --06T13::.611241Z [Note] IPv6 is available.
--06T13::.612241Z [Note] - '::' resolves to '::';
--06T13::.612241Z [Note] Server socket created on IP: '::'.
--06T13::.615241Z [Warning] Failed to open optimizer cost constant t
ables --06T13::.617241Z [ERROR] Fatal error: Can't open and lock privilege
tables: Table 'mysql.user' doesn't exist
--06T13::.619242Z [ERROR] Aborting --06T13::.619242Z [Note] Binlog end
--06T13::.640243Z [Note] Shutting down plugin 'ngram'
--06T13::.640243Z [Note] Shutting down plugin 'partition'
--06T13::.640243Z [Note] Shutting down plugin 'BLACKHOLE'
--06T13::.641243Z [Note] Shutting down plugin 'ARCHIVE'
--06T13::.641243Z [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
--06T13::.641243Z [Note] Shutting down plugin 'MRG_MYISAM'
--06T13::.641243Z [Note] Shutting down plugin 'MyISAM'
--06T13::.641243Z [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
--06T13::.642243Z [Note] Shutting down plugin 'INNODB_SYS_DATAFILES' --06T13::.642243Z [Note] Shutting down plugin 'INNODB_SYS_TABLESPACE
S'
--06T13::.643243Z [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_CO
LS'
--06T13::.643243Z [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
--06T13::.643243Z [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
--06T13::.643243Z [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
--06T13::.643243Z [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
--06T13::.644243Z [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS
'
--06T13::.644243Z [Note] Shutting down plugin 'INNODB_SYS_TABLES'
--06T13::.644243Z [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE
'
--06T13::.644243Z [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE
'
--06T13::.644243Z [Note] Shutting down plugin 'INNODB_FT_CONFIG'
--06T13::.645243Z [Note] Shutting down plugin 'INNODB_FT_BEING_DELET
ED'
--06T13::.645243Z [Note] Shutting down plugin 'INNODB_FT_DELETED'
--06T13::.645243Z [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STO
PWORD'
--06T13::.645243Z [Note] Shutting down plugin 'INNODB_METRICS'
--06T13::.645243Z [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INF
O'
--06T13::.646243Z [Note] Shutting down plugin 'INNODB_BUFFER_POOL_ST
ATS'
--06T13::.646243Z [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LR
U'
--06T13::.646243Z [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
--06T13::.646243Z [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_
RESET'
--06T13::.646243Z [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX' --06T13::.647243Z [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
--06T13::.647243Z [Note] Shutting down plugin 'INNODB_CMPMEM'
--06T13::.647243Z [Note] Shutting down plugin 'INNODB_CMP_RESET'
--06T13::.647243Z [Note] Shutting down plugin 'INNODB_CMP'
--06T13::.647243Z [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
--06T13::.648243Z [Note] Shutting down plugin 'INNODB_LOCKS'
--06T13::.648243Z [Note] Shutting down plugin 'INNODB_TRX'
--06T13::.648243Z [Note] Shutting down plugin 'InnoDB'
--06T13::.648243Z [Note] InnoDB: FTS optimize thread exiting.
--06T13::.648243Z [Note] InnoDB: Starting shutdown...
--06T13::.748249Z [Note] InnoDB: Dumping buffer pool(s) to C:\Progra
m Files\MySQL\MySQL Server 5.7\data\ib_buffer_pool
--06T13::.751249Z [Note] InnoDB: Buffer pool(s) dump completed at
::
--06T13::.137328Z [Note] InnoDB: Shutdown completed; log sequence nu
mber
--06T13::.138328Z [Note] InnoDB: Removed temporary tablespace data f
ile: "ibtmp1"
--06T13::.138328Z [Note] Shutting down plugin 'MEMORY'
--06T13::.138328Z [Note] Shutting down plugin 'CSV'
--06T13::.139328Z [Note] Shutting down plugin 'sha256_password'
--06T13::.139328Z [Note] Shutting down plugin 'mysql_native_password
'
--06T13::.139328Z [Note] Shutting down plugin 'binlog'
--06T13::.139328Z [Note] mysqld: Shutdown complete
原创文章,转载请注明:http://www.cnblogs.com/langtianya/p/5185577.html
相关:
最后简单介绍一下主从复制和读写分离
1 主从复制,是用来建立一个和主数据库完全一样的数据库环境,称为从数据库;主数据库一般是实时的业务数据库,从数据库的作用和使用场合一般有几个: 一是作为后备数据库,主数据库服务器故障后,可切换到从数据库继续工作; 二是可在从数据库作备份、数据统计等工作,这样不影响主数据库的性能;
2 读写分离,是指读与写分别使用不同的数据库,当然一般是在不同服务器上的;在同一台服务器上的读写环境,估计只是用来测试吧。 一般读写的数据库环境配置为,一个写入的数据库,一个或多个读的数据库,各个数据库分别位于不同的服务器上,充分利用服务器性能和数据库性能;当然,其中 会涉及到如何保证读写数据库的数据一致,这个就可以利用主从复制技术来完成。 一般应用场合为:业务吞吐量很大