一、安装mysql
##################下载mysql5.7
wget http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
##################rpm安装生成yum仓库
rpm -ivh mysql57-community-release-el7-9.noarch.rpm
##################yum安装mysql
yum -y install mysql mysql-server --nogpgcheck
二、配置主从
2.1、修改初始化密码且打开远程登录
此操作需要在主从两库都进行操作
[root@database-2 tmp]# cat /var/log/mysqld.log | grep pass
[root@database-2 tmp]# mysql -uroot -p'ERgdrBVl<57;' ########## 'ERgdrBVl<57;'系统随机生成的密码
#############修改密码
alter user 'root'@'localhost' identified by 'youpassword';
#############开启远程登录
grant all privileges on *.* to root@"需要登录的IP" identified by "youpassword" WITH GRANT OPTION;
2.2、修改配置文件
vim /etc/my.cnf
############主库配置文件master
server_id=1
log-bin=mysql-bin
############从库配置文件slave
log_bin=mysql-bin
server_id=2
relay_log=mysql-relay-bin
log_slave_updates=1
read_only=1
2.3、创建用户并且配置主从
主库操作
########################在master上操作
mysql> grant replication client,replication slave on *.* to rep@'172.16.18.%' identified by 'password'; #从库IP,我这里写的是主库和从库的一个网段
mysql> flush tables with read lock; #锁表,因为我这是生产中配置的主从,所以要保持主从数据一致,先进行锁表然后再导出数据,然后将数据导入从库
####################查看master信息
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 5420 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
[root@database-1 ~]# mysqldump -uroot -p'密码' -S /data/mysql/mysql.sock --all-databases >all.sql
-S 是指定mysql的sock是因为我的mysql没有在默认的路径下
从库操作
mysql> source /tmp/all.sql #导入数据,这时候要给主库解表
#################!!!!!!!!!!####################主库操作
mysql> unlock tables; #######解表,主库操作
mysql> CHANGE MASTER TO
MASTER_HOST='172.16.18.6', #########主库IP
MASTER_USER='rep', #########同步用户
MASTER_PASSWORD='Cw123!@#', #########同步用户的密码
MASTER_LOG_FILE='mysql-bin.000003', #########主库的二进制文件
MASTER_LOG_POS=5420; #########主库的pos
mysql> start slave; #########启动从库
#############查看主从同步状态
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.18.6
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 27098
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 21998
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: 27098
Relay_Log_Space: 22205
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: 1
Master_UUID: 32e9eb51-714c-11ed-b5b4-fa163e620961
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)
双yes代表配置成功,这时需要验证,验证很简单,修改主库的一个表,然后查看从库是否变化。
也可以选择观察主库pos是否变化。