mysql8 的安装
添加mysql环境变量
cmd中切换到bin下
mysqld --initialize --console
mysqld --install MySQL8
net start MySQL8
登录mysql
修改密码
alter user 'root'@'localhost' identified by '123456';
退出后从重新登录
# mysql8 的主从配置案例
master 192.168.2.150
slave 192.168.2.111
# 1.配置master服务器
a.修改my.ini
[mysqld]
server-id = 1 # 数据库ID号 为1时表示为master 其中master_id必须是为1到232-1之间的一个正整数值
log-bin = mysql-bin # 需要同步二进制日志
log_bin-index = master-bin.index
# 需要同步的二进制数据库名
# binlog-do-db=wisdom
# 不同步的二进制数据库名这个同步后听说很麻烦
binlog-ignore-db = mysql
binlog_ignore_db = information_schema #和binlog-do-db一样,可以设置多个
binlog_ignore_db = performance_schema #和binlog-do-db一样,可以设置多个
binlog_ignore_db = sys #和binlog-do-db一样,可以设置多个
# 跳过错误,继续执行复制;
slave-skip-errors=all
# 把更新的记录写到二进制文件中
log-slave-updates
b.创建用于复制操作的用户 注意在创建主从复制的时候 创建用户时,必须明确指定认证插件sha256_password
create user 'slave2'@'%' identified with sha256_password by '123456';
#授予用户复制权限
grant replication slave on *.* to 'slave2'@'%';
flush privileges;
c.查看master的状态
show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000011 | 1192 | | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
# 2.配置slave服务器
a.修改my.ini
[mysqld]
server-id = 2 # 数据库ID号 为1时表示为master 其中master_id必须是为1到232-1之间的一个正整数值
relay-log-index = slave-relay-bin.index
relay-log = slave-relay-bin
# 不同步的二进制数据库名这个同步后听说很麻烦,
replicate-ignore-db = mysql
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
replicate-ignore-db = sys
# 需要同步的二进制数据库名
#replicate-do-db = wisdom
b.重启slave服务器后,登录slave服务器
mysql>change master to
master_host='192.168.2.150',
master_user='slave2',
master_password='123456',
master_port = 3306,
master_log_file='mysql-bin.000011', # 要与master服务器状态File一致
master_log_pos = 1192; # 要与master服务器状态Position一致
c.开启主从同步
start slave;
c.查看slave状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.169.2.150
Master_User: salve1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 156
Relay_Log_File: slave-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
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: 156
Relay_Log_Space: 156
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: NULL
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: 0
Master_UUID:
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave 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:
如果 Slave_IO_Running: Yes Slave_SQL_Running: Yes 表示配置成功;
开始测试
同步报错
Could not execute Write_rows event on table wisdom.eom_user_logs; Duplicate entry '1044' for key 'eom_user_logs.PRIMARY', Error_code: 1062; handler error HA_ERR_FOUND_DUPP_KEY; the event's master log mysql-bin.000011, end_log_pos 295337
mysql> stop slave;
mysql> set global sql_slave_skip_counter=1;
mysql> start slave;
mysql> show slave status;
注意:如果在配置完成后没有主从复制,说明数据库初始化不一样,需要将主数据库备份,还原到从服务器的,重新配置从服务器,即可
mysql 读写分离配置 mysql的读写分离我采用 mysql-proxy中间件 可以再mysql的官网下载mysql-proxy;地址https://downloads.mysql.com/archives/proxy/
解压后
在里面创建mysql-proxy.conf 文件
[mysql-proxy]
# 运行mysql-proxy用户
user = root
# mysql-proxy连接后端mysql服务器的用户
admin-username=root
# mysql-proxy连接后端mysql服务器的密码
admin-password=123456
# 代理的监听地址端口,默认端口4040
proxy-address=0.0.0.0:3307
# 主库服务器+端口
proxy-backend-addresses=192.168.2.150:3306
# 从库服务器+端口,多个从库用,隔开
proxy-read-only-backend-addresses=192.168.2.111:3306
# 根据存放的文件位置自行调整
# admin-lua-script=D:/mysql-proxy/lib/mysql-proxy/lua/admin.lua
# 根据存放的文件位置自行调整
proxy-lua-script=D:/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
# 日志文件存放位置,如果你指定了一个路径,请确保手动创建了对应的文件夹,否则会报错
log-file=D:/mysql-proxy/log/mysql-proxy.log
#定义log日志级别,由高到低分别有(error|warning|info|message|debug)
log-level=debug
# 以守护进程方式运行
daemon=true
#mysql-proxy崩溃时,尝试重启
keepalive=true
启动代理
mysql-proxy --defaults-file="D:/mysql-proxy/mysql-proxy.conf"
配置成功
重新打开一个cmd 窗口 输入 netstat -ano | findstr :3307
如果有端口监听表示配置成功,用客户端链接测试。