目的: 1.两台数据库服务器,一主一备,当主数据库服务器增删改数据后,备机数据库服务器同步进行数据增删改 2.达到热备数据库的目的
准备: 两台web服务器,此处使用 192.168.2.1和192.168.2.2测试使用
两台服务器部署MySQL(略过)
主数据库服务器(192.168.2.1)配置 1.开启log-bin日志
编辑my.cnf vi /etc/my.cnf
server-id=10
log-bin=/var/lib/mysql/mysql-bin
binlog-ignore-db=mysql binlog_format=mixed expire_logs_days=10
重启MySQL systemctl restart mysqld
2.查看log-bin所在目录,日志是否生成 ll /var/lib/mysql/mysql-bin 如存在mysql-bin.000002 说明开启成功
3.新建用户并设置全部权限 mysql>grant all on . to ruc@”%” identified by “ruc@2021”;
4.刷新用户权限 mysql>flush privileges;
5.远程连接测试数据库是否可连接 打开从数据库服务器(192.168.2.2) mysql -uruc -p -h 192.168.2.1 如此时报错: ERROR 2003 (HY000): Can’t connect to MySQL server on ‘192.168.2.186’ (113) 说明是因为防火墙未开放,可先关闭防火墙
连接成功表示账号创建成功
6.查看bin-log日志位置 登录主数据库服务器 mysql -uroot -pGpower@2021
mysql>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 585 | | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
7.配置从数据库服务器
编辑my.cnf文件,增加server-id
server-id=11
保存重启数据库
systemctl restart mysqld
8.执行同步 登录从数据库服务器(root登录)
执行以下命令
host为主数据库服务器ip地址 user为主数据库服务器创建的数据库用户 port为主数据库端口 password为主数据库用户密码(创建的普通用户) log-file为主数据库二进制文件 log_pos为初始值
mysql>change master to master_host=’192.168.2.186’,master_user=’ruc’,master_port=3306,master_password=’Gpower@2021’,master_log_file=’mysql-bin.000002’,master_log_pos=585;
启动slave mysql>start slave;
查看同步状态 mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.186
Master_User: ruc
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 585
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
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: 585
Relay_Log_Space: 531
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: 10
Master_UUID: d90803f7-c1d4-11eb-8422-000c298d1dfe
Master_Info_File: /var/lib/mysql/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:
1 row in set (0.00 sec)
可以查看到IO和SQL均为开启状态
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
9.测试数据库同步
登录主数据库,并创建数据库 mysql>create database cms8 CHARACTER SET utf8mb4;
登录从数据库服务器,查看数据库 mysql>show databases;
可以看到从数据库里也存在cms8库
10.主从数据库部署完成