1. 基础环境
- 系统版本:
############## NeoKylin Linux Version#################Release:NeoKylin Linux Advanced Server release V7Update6 (Chromium)Kernel:3.10.0-957.el7.x86_64Build:NeoKylin Linux Advanced Serverrelease V7Update6/(Chromium)-x86_64b4.lic/20190820#################################################
- MySQL版本
[root@localhost ~]# rpm -qa | grep mysqlmysql-community-server-5.7.40-1.el7.x86_64mysql-community-client-5.7.40-1.el7.x86_64mysql80-community-release-el7-7.noarchmysql-community-common-5.7.40-1.el7.x86_64mysql-community-libs-5.7.40-1.el7.x86_64
2. 移除系统自带MariaDB
[root@localhost ~]# yum remove mariadb*
3. 安装MySQL 5.7
因为中标麒麟 V7.6 系统源中并没有MySQL,所以我们需要手动添加MySQL的仓库源。好在MySQL官方为我们提供了MySQL的仓库源,所以我们直接使用就好了。
- 从MySQL官网下载仓库源安装包
wget https://repo.mysql.com//mysql80-community-release-el7-7.noarch.rpm
- 安装MySQL仓库源安装包
rpm -ivh mysql80-community-release-el7-7.noarch.rpm
- 查看MySQL仓库源启用情况
[root@localhost ~]# yum repolist all | grep mysqlmysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Comm 禁用mysql-cluster-7.5-community-source MySQL Cluster 7.5 Comm 禁用mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Comm 禁用mysql-cluster-7.6-community-source MySQL Cluster 7.6 Comm 禁用mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Comm 禁用mysql-cluster-8.0-community-debuginfo/x86_64 MySQL Cluster 8.0 Comm 禁用mysql-cluster-8.0-community-source MySQL Cluster 8.0 Comm 禁用mysql-connectors-community/x86_64 MySQL Connectors Commu 启用: 206mysql-connectors-community-debuginfo/x86_64 MySQL Connectors Commu 禁用mysql-connectors-community-source MySQL Connectors Commu 禁用mysql-tools-community/x86_64 MySQL Tools Community 启用: 94mysql-tools-community-debuginfo/x86_64 MySQL Tools Community 禁用mysql-tools-community-source MySQL Tools Community 禁用mysql-tools-preview/x86_64 MySQL Tools Preview 禁用mysql-tools-preview-source MySQL Tools Preview - 禁用mysql57-community/x86_64 MySQL 5.7 Community Se 禁用mysql57-community-source MySQL 5.7 Community Se 禁用mysql80-community/x86_64 MySQL 8.0 Community Se 启用: 367mysql80-community-debuginfo/x86_64 MySQL 8.0 Community Se 禁用mysql80-community-source MySQL 8.0 Community Se 禁用
这里可以看到默认启用的是mysql 8.0的仓库源。因为我们要安装mysql 5.7,所以我们要禁用mysql 8.0的仓库源,启用mysql 5.7的仓库源。
- 启用MySQL 5.7的仓库源
[root@localhost ~]# yum-config-manager --disable mysql80-community[root@localhost ~]# yum-config-manager --enable mysql57-community[root@localhost ~]#[root@localhost ~]#[root@localhost ~]#[root@localhost ~]# yum repolist all | grep mysqlmysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Comm 禁用mysql-cluster-7.5-community-source MySQL Cluster 7.5 Comm 禁用mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Comm 禁用mysql-cluster-7.6-community-source MySQL Cluster 7.6 Comm 禁用mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Comm 禁用mysql-cluster-8.0-community-debuginfo/x86_64 MySQL Cluster 8.0 Comm 禁用mysql-cluster-8.0-community-source MySQL Cluster 8.0 Comm 禁用mysql-connectors-community/x86_64 MySQL Connectors Commu 启用: 206mysql-connectors-community-debuginfo/x86_64 MySQL Connectors Commu 禁用mysql-connectors-community-source MySQL Connectors Commu 禁用mysql-tools-community/x86_64 MySQL Tools Community 启用: 94mysql-tools-community-debuginfo/x86_64 MySQL Tools Community 禁用mysql-tools-community-source MySQL Tools Community 禁用mysql-tools-preview/x86_64 MySQL Tools Preview 禁用mysql-tools-preview-source MySQL Tools Preview - 禁用mysql57-community/x86_64 MySQL 5.7 Community Se 启用: 624mysql57-community-source MySQL 5.7 Community Se 禁用mysql80-community/x86_64 MySQL 8.0 Community Se 禁用mysql80-community-debuginfo/x86_64 MySQL 8.0 Community Se 禁用mysql80-community-source MySQL 8.0 Community Se 禁用
再次查看,我们发现mysql 5.7的仓库源已经启用了。这里我们也可以选择手动编辑 /etc/yum.repos.d/mysql-community.repo ,来启用 mysql 5.7的仓库源。方法就是将文件中 mysql80-community enabled选项设置为0,将mysql57-community enabled选项设置为1。
- 安装mysql 5.7
[root@localhost ~]# yum install mysql-community-server#安装完成之后,查看安装情况[root@localhost ~]# rpm -qa | grep mysqlmysql-community-server-5.7.40-1.el7.x86_64mysql-community-client-5.7.40-1.el7.x86_64mysql80-community-release-el7-7.noarchmysql-community-common-5.7.40-1.el7.x86_64mysql-community-libs-5.7.40-1.el7.x86_64
4. 初始化MySQL
- 启动mysql
[root@localhost ~]# systemctl start mysqld
- 设置mysql开机自启动
[root@localhost ~]# systemctl enable mysqld
- 查看mysql服务状态
[root@localhost ~]# systemctl status mysqld● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since 四 2022-11-10 14:21:09 CST; 838ms ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 16055 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 15960 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 16058 (mysqld) Tasks: 27 CGroup: /system.slice/mysqld.service └─16058 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid11月 10 14:21:04 localhost.localdomain systemd[1]: Starting MySQL Server...11月 10 14:21:09 localhost.localdomain systemd[1]: Started MySQL Server.[root@localhost ~]#[root@localhost ~]#
- 修改mysql root用户密码
mysql安装成功之后会为root账户生成一个临时随机密码,这个临时随机密码我们可以在/var/log/mysqld.log文件中找到。
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log2022-11-10T06:21:06.369797Z 1 [Note] A temporary password is generated for root@localhost: 9eh*
知道临时随机密码后,我们就可以登录mysql,将root账户的密码设置成任何我们想要的密码了。
[root@localhost ~]# mysql -uroot -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.7.40Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
因为mysql的密码规则需要很复杂,这里我们想把密码设置得简单一些,所以我们需要修改一下mysql的密码规则。这一步大家可以根据现场情况自行决定是否修改。
mysql> set global validate_password_policy=0;mysql> set global validate_password_length=1;
接下来将root账户的密码设置成我们想要的。
ALTER USER 'root'@'localhost' IDENTIFIED BY 'yourpassword';
至此,mysql安装完毕,你可以根据自己的需求使用mysql数据库了。
5. 参考资料
A Quick Guide to Using the MySQL Yum Repository
centos7 安装 Mysql 5.7.28,详细完整教程
来源地址:https://blog.csdn.net/zhajianting/article/details/127790839