Windows环境下配置MySQL主从复制详细教程
Posted 奋斗de小青年
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Windows环境下配置MySQL主从复制详细教程相关的知识,希望对你有一定的参考价值。
一、下载mysql
下载地址:MySQL :: MySQL Community Downloads
1、点击箭头所指
2、默认页面是下载mysql8,如果想下载mysql5点击箭头所指
3、点击箭头所指选择需要的版本
4、点击箭头所指可以选择32位或64位
5、然后点击download
二、解压压缩包
1、解压压缩包----->放置任意磁盘
2、新建mysql配置文件ini
1)、新建文本文档,命名为my.ini
2)、my.ini内容如下
[mysqld]
# 设置mysql的安装目录
basedir=D:\\\\soft\\\\mysql-5.7.39
# 设置mysql数据库的存放目录
datadir=D:\\\\soft\\\\mysql-5.7.39\\\\data
#设置3306端口
port=3306
#主服务器唯一ID
server-id=1
#启用二进制日志
log-bin=txmysql-bin
#设置不要复制的数据库(可设置多个)
binlog-ignore-db=mysql
#设置需要复制的数据库
binlog-do-db=mydb1
#设置binlog格式,MIXED,ROW,STATEMENT
binlog_format=STATEMENT
sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
3、安装mysql,win10系统点击搜索cmd,然后以管理员身份运行
4、切换bin目录,运行mysqld install命令安装
5、输入命令启动mysql
net start mysql
6、使用命令进入mysql管理界面
mysql -u root -p
7、修改密码为: 123456
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
8、刷新权限
flush privileges;
9、重启mysql
net stop mysql
net start mysql
10、第一个mysql安装完成
三、安装第二个MySQL服务
1、第一个MySQL服务安装完毕后,将MySQL的文件夹重新复制一份,并重命名为mysql-5.7.39-slave。修改下该文件夹下的my.ini文件,
[mysqld]
# 设置mysql的安装目录
basedir=D:\\\\soft\\\\mysql-5.7.39-slave
# 设置mysql数据库的存放目录
datadir=D:\\\\soft\\\\mysql-5.7.39-slave\\\\data
#设置3307端口
port=3307
#从服务器唯一ID
server-id=2
#启用中继日志
relay-log=mysql-relay
sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
2、同样以管理身份运行cmd,切换到mysql-5.7.39-slave/bin目录下,执行:
mysqld.exe --install mysqlc2 --defaults-file=D:\\soft\\mysql-5.7.39-slave\\my.ini
说明:D:\\soft\\mysql-5.7.39-slave\\my.ini是你自己的路径 ,mysqlc2是起的别名
然后以mysqlc2启动服务
net start mysqlc2
四、主从复制配置
1、用navicat连接上主数据库进行操作(一条条执行)
#创建用户
CREATE USER 'slave'@'localhost' IDENTIFIED BY '123456';
#授权用户
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'localhost';
#刷新权限
FLUSH PRIVILEGES;
show master status;
2、从库slave指向master地址,在从库中使用命令:
#master_host 主数据库地址
CHANGE MASTER TO master_host ='127.0.0.1',
#上一步中主数据库创建的从库用户
master_user ='slave',
#上一步中主数据库创建的从库密码
master_password ='123456',
#上一步中保存的file
master_log_file ='txmysql-bin.000004',
#上一步中保存的position
master_log_pos = 567;
3、执行命令启动
start slave
4、使用命令show slave status查看从库状态
show slave status
5、问题:
如果是Slave_IO_Running:No,可能是由于uuid相同,而导致异常(因为直接复制的)。
修改D:\\soft\\mysql-5.7.39-slave\\data\\auto.cnf,把里面的server-uuid修改成不一样的值即可。重新启动从库mysql服务
6、可以以下边的两个error查询解决办法:
最后我们可以在主库新增、修改和删除,会发现从库跟着变化了。此时,主从复制配置完成。
同一台电脑windows环境下的mysql的主从配置
1.下载5.6.zip包
从mysql的官网下载5.6对应的zip包,因为5.7版本解压后没有.ini文件,所以改成安装5.6版本了。
2.配置
解压zip包,并复制一份待配置从库
在我的mysql中需要D:\\software\\mysql\\mysql1和D:\\software\\mysql\\mysql2两个
3.安装mysql主服务器
更改D:\\software\\mysql\\mysql1路径下的my-default.ini文件my.ini
在文件中添加如下配置: 注意这个路径的方向一定要是右斜杠,因为这个方向折腾了一下午
basedir=D:/software/mysql/mysql1
datadir=D:/software/mysql/mysql1/data
binlog-do-db=test
binlog-ignore-db=mysql
log-bin=mysql-bin
server-id=1
然后安装mysql: 以管理员身份运行cmd窗口,然后切换到压缩包所在的bin目录
然后输入命令mysqld install 出现以下成功提示代表安装成功:
如果提示“Service successfully installed”,则说明mysql服务安装成功,默认的服务名称为MySQL,端口号为3306,密码默认为空
连接mysql主服务器
mysql -uroot -p -P3306
1
然后创建新用户:
create user \'mytest\'@\'127.0.0.1\' identified by \'123456\';
1
注:
@后面的ip地址为允许连接的客户端的ip地址。mytest是用户名,123456是密码
然后,给新用户配置主从复制的权限:
grant replication slave on *.* to \'mytest\'@\'127.0.0.1\' identified by \'123456\';
1
注:
@后面的ip地址为允许连接的客户端的ip地址,如果改为 ‘%’,就表示客户端没有ip地址的限制
然后查看主服务master的状态:
mysql> show master status;
注意:
记住file,和position,因为等下配置从数据库的时候会用到
4.安装mysql从服务器
找到mysql2的配置文件模板D:\\software\\mysql\\mysql2 目录下的 my-default.ini,重命名为 my.ini,并修改如下这几项:
[client]
port = 3307
[mysqld]
port= 3307
basedir=D:/software/mysql/mysql2
datadir=D:/software/mysql/mysql2/data
server-id = 2
注意:在添加上述几行内容的时候 是在已有的[mysql]后面添加的,并不是在my.ini的末尾添加
保存my.ini后,用 管理员身份 运行cmd,并切换到D:\\software\\mysql\\mysql2\\bin目录下,运行
mysqld install MySQL2 --defaults-file="D:\\software\\mysql\\mysql2\\my.ini"
代表安装成功了
连接从服务器
mysql -uroot -p -P3307
如果报错:ERROR 2003 (HY000): Can\'t connect to MySQL server on \'localhost\' (10061)
则启动mysql2服务 net start mysql2
在services.msc中查看mysql和mysql2保证都是已启动的状态
在此之后输入mysql -uroot -p -P3307 则可连接上从库成功
从库上输入命令:
change master to master_host=\'127.0.0.1\',master_port=3306,master_user=\'mytest\',master_password=\'111111\',master_log_file=\'mysql-bin.000003\',master_log_pos=120;
这里的master_log_file和master_log_pos对应刚才show master status记下的参数。
在从库开启从数据库复制功能: 输入命令 start slave;
查看从服务器的slave状态: show slave status;
+----------------+-------------+-------------+-------------+---------------+------------------+---------------------+----------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+--------------------------------------------------------------------------------------------------------------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------------------------+-----------+----------
-----------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File | SQL_Delay | SQL_Remai
ning_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position |
+----------------+-------------+-------------+-------------+---------------+------------------+---------------------+----------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+--------------------------------------------------------------------------------------------------------------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------------------------+-----------+----------
-----------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+
| | 127.0.0.1 | mytest | 3306 | 60 | mysql-bin.000003 | 120 | GHS-20181030HMJ-relay-bin.000001 | 4 | mysql-bin.000003 | No | Yes | | | | | | | 0 | | 0 | 120 | 120 | None | | 0 | No | | | | | | NULL | No | 1236 | Got fatal error 1236 from master when reading data from binary log: \'Misconfigured master - server_id was not set\' | 0 | | | 1 | 32944960-304b-11e9-9dc5-84ef184c5602 | D:\\software\\mysql\\mysql2\\data\\master.info | 0 |
NULL | Slave has read all relay log; waiting for the slave I/O thread to update it | 86400 | | 190215 13:57:47 | | | | | | 0 |
+----------------+-------------+-------------+-------------+---------------+------------------+---------------------+----------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+--------------------------------------------------------------------------------------------------------------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+-------------------------------------------+-----------+----------
-----------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+
此处的io/running是no证明同步有问题,必须两个值都是yes的时候才证明同步是没有问题的,那么有问题可以查看mysql2下的data里面以err为结尾的日志
2019-02-15 13:57:47 7072 [Note] Slave SQL thread initialized, starting replication in log \'mysql-bin.000003\' at position 120, relay log \'.\\GHS-20181030HMJ-relay-bin.000001\' position: 4
2019-02-15 13:57:47 7072 [ERROR] Error reading packet from server: Misconfigured master - server_id was not set (server_errno=1236)
2019-02-15 13:57:47 7072 [ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: \'Misconfigured master - server_id was not set\', Error_code: 1236
2019-02-15 13:57:47 7072 [Note] Slave I/O thread exiting, read up to log \'mysql-bin.000003\', position 120
日志里面提示在mysql1中的my.ini文件中未设置server-id的值,现在添加上
但是添加上之后发现io依然是no,那需要做如下操作:
从库stop slave
主库服务在服务进程中停止后重启服务,然后从库中重新执行change命令
再重新start slave;
然后show slave status;此时查看已经都是yes;
故第一次设置的时候务必要添加server-id=1
=========================================================验证主从配置成功==========================================================
在主的test数据库中创建表;
create table tb_emp( id int primary key auto_increment, Name varchar(18), sex varchar(2) );
可以发现在从库中show tables;命令执行之后 有表tb_emp的存在;
以上是关于Windows环境下配置MySQL主从复制详细教程的主要内容,如果未能解决你的问题,请参考以下文章