同一台电脑windows环境下的mysql的主从配置

Posted companion

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了同一台电脑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的主从配置的主要内容,如果未能解决你的问题,请参考以下文章

如何在同一台电脑(windows系统)上同时搭建python2和python3环境

如何在一台windows主机上安装多个mysql服务

MySQL数据库主从同步

win10 环境配置emacs

同一台电脑管理多个`SSH KEY`

mysql主从配置