轻文章-使用rpm-bundle包安装mysql及主从配置

mysql安装步骤

1.将mysql-5.7.31-1.el7.x86_64.rpm-bundle.tar包上传至服务器。我在服务器上直接使用了wget命令:

wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.31-1.el7.x86_64.rpm-bundle.tar

2.解压缩

shell> tar xf mysql-5.7.31-1.el7.x86_64.rpm-bundle.tar

3.yum安装mysql,也可rpm。

shell> yum install mysql-community-client-5.7.31-1.el7.x86_64.rpm mysql-community-common-5.7.31-1.el7.x86_64.rpm mysql-community-libs-5.7.31-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.31-1.el7.x86_64.rpm mysql-community-server-5.7.31-1.el7.x86_64.rpm

4.从其他服务器复制my.cnf文件,修改server-id,根据实际需求修改具体参数,然后启动mysql。

shell> systemctl start mysqld
shell> systemctl enable mysqld

5.修改默认root密码

shell> sudo grep 'temporary password' /var/log/mysqld.log

小tips:一般情况下,如果临时密码不在/var/log/mysqld.log,就在/var/lib/mysql/error.log
登录mysql,修改临时密码,配置允许远程访问。

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxx';
mysql> update user set host = '%' where user = 'root';
mysql> select host, user from user; //检查配置结果
mysql> flush privileges;

小tips:yum安装会自动添加mysql用户及组,会自动创建/var/log/mysqld.log等文件,首次启动会自动初始化。

配置主从同步

1.确认主从库的配置文件包含:

[mysqld]
log-bin=mysql-bin
server-id=1 //结合实际情况设置
binlog_format='ROW'

2.配置账号
分别在主库与从库使用如下语句进行账号配置:

mysql> create user 'repluser'@'192.168.2.%' identified by '******';
mysql> grant replication slave on *.* to 'repluser'@'192.168.2.%';
mysql> flush privileges;

3.查询主库,确认同步位置(主库192.168.2.1,从库192.168.2.2)

mysql> show master status \G
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 779
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.01 sec)

4.在从库执行配置命令

mysql> CHANGE MASTER TO MASTER_HOST='192.168.2.1', MASTER_PORT=3306, MASTER_USER='repluser', MASTER_PASSWORD='******', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=779;

5.开启主从同步并查询状态

mysql> start slave;
mysql> show slave status \G

猜你喜欢

转载自blog.csdn.net/zsx0728/article/details/109330870