1、mysql8的安装:
2、Mysql开启多端口
2.1、将安装的mysql文件再次复制一份出来,然后修改配置文件。然后修改my.ini文件。一共4个地方需要修改:
2.2、 修改完成后,创建对应的接口实例。
mysqld --install mysql3307
在服务列表就可以看到了
在“运行”中输入“regedit”进入注册表,修改mysql相关的注册信息,如下图所示:
注册表目录“HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\mysql3307”
2.4、启动mysql,这样就可以实现2个端口的mysql了。
3、mysql主从配置
3.1、修改主服务器master:
修改my.ini文件:
[mysqld]
log-bin=mysql-bin //[必须]启用二进制日志
server-id=320 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
3.2、修改从服务器slave
修改my.ini文件:
[mysqld]
log-bin=mysql-bin //[不是必须]启用二进制日志
server-id=112 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
3.3、重启两台服务器的mysql
3.4、在主服务器上建立帐户并授权slave
创建一个可以跨域的账户,以保证从服务器可以登陆主服务器的mysql数据库 。
----创建从服务器同步用的mysql用户
create user 'rep'@'%' identified by '1234567';
----授权创建的用户权限
grant replication slave,replication client on *.* to 'rep'@'%';
“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如192.168.145.226,加强安全。
3.5、登录主服务器的mysql,查询master的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 1255 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化。记录file,Position数据 文件“mysql-bin.000003”和偏移量“1255”;
3.6、配置从服务器Slave
change master to master_host='192.168.8.32',master_user='rep',master_password='1234567',
master_log_file='mysql-bin.000003',master_log_pos=1255;
3.7、启动从服务器复制功能
mysql> start slave;
3.8、检查从服务器复制功能状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.8.32
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1012
Relay_Log_File: DESKTOP-M21FB31-relay-bin.000002
Relay_Log_Pos: 1181
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 1012
Relay_Log_Space: 1401
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 222
Master_UUID: 2729c605-f2b4-11ec-9fdc-507b9df067b4
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.01 sec)
注:Slave_IO_Running:写入状态。Slave_SQL_Running:本地数据库运行状态。当2个状态均为yes时,证明数据库主从状态设置成功。
①如果Slave_IO_Running状态为no的时候,可能是server uuid的值就和主服务器是一样的。因为是复制来的,可能一样。这个时候,可以将从服务器里面的auto.cnf文件删除。重启服务就会生成一个新的。
mysql> show variables like 'datadir';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| datadir | D:\mysql8-3307 |
+---------------+-----------------+
1 row in set (0.01 sec)
————————————————
mysql> select uuid();
+--------------------------------------+
| uuid() |
+--------------------------------------+
| dabbdaac-f2c6-11ec-8507-507b9df067b4 |
+--------------------------------------+
1 row in set (0.00 sec)
————————————————
②如果Slave_SQL_Running状态为No时,一般是主从数据库的初始数据不一致导致的。
解决上面问题之后。再次执行3.6配置从服务器Slave,当2个都为yes的时候,主从服务器配置完成。可以在主库新建一个数据库。从库就会自动创建。这时候不要去操作从库,因为修改会导致主从失败。