> 文档中心 > Centos 7.6通过rpm安装MySQL 5.7.38

Centos 7.6通过rpm安装MySQL 5.7.38


1、通过wget下载MySQL的rpm

wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

 输出内容如下:

[root@admin ~]# wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm--2022-05-10 13:58:29--  https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpmResolving dev.mysql.com (dev.mysql.com)... 137.254.60.11Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.HTTP request sent, awaiting response... 302 FoundLocation: https://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpm [following]--2022-05-10 13:58:30--  https://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpmResolving repo.mysql.com (repo.mysql.com)... 92.122.253.200Connecting to repo.mysql.com (repo.mysql.com)|92.122.253.200|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 9224 (9.0K) [application/x-redhat-package-manager]Saving to: ‘mysql57-community-release-el7-9.noarch.rpm’100%[============================================================================>] 9,224       --.-K/s   in 0s      2022-05-10 13:58:31 (103 MB/s) - ‘mysql57-community-release-el7-9.noarch.rpm’ saved [9224/9224]

2、检查是否存在MySql的repo

[root@admin ~]# cd /etc/yum.repos.d/[root@admin yum.repos.d]# lsrhel.repo

可以看到没有MySql的repo

3、安装MySQL的repo,并查看是否安装成功

[root@admin ~]# rpm -ivh mysql57-community-release-el7-9.noarch.rpm warning: mysql57-community-release-el7-9.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEYPreparing...                          ################################# [100%]Updating / installing...   1:mysql57-community-release-el7-9  ################################# [100%][root@admin ~]# cd -/etc/yum.repos.d[root@admin yum.repos.d]# lsmysql-community.repo  mysql-community-source.repo  rhel.repo

4、查看系统原来是否存在MySQL包

[root@admin yum.repos.d]# rpm -qa | grep mysqlmysql57-community-release-el7-9.noarch

可以看到只有我们刚才安装的包,没有其他的mysql包。

5、安装MySQL

[root@admin yum.repos.d]# yum install -y mysql-serverLoaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfilemysql-connectors-community                                                                     | 2.6 kB  00:00:00     mysql-tools-community                                                                          | 2.6 kB  00:00:00     mysql57-community                                                                              | 2.6 kB  00:00:00     (1/3): mysql-tools-community/x86_64/primary_db                                                 |  86 kB  00:00:01     (2/3): mysql-connectors-community/x86_64/primary_db                                            |  87 kB  00:00:01     (3/3): mysql57-community/x86_64/primary_db                                                     | 306 kB  00:00:01     Resolving Dependencies--> Running transaction check---> Package mysql-community-server.x86_64 0:5.7.38-1.el7 will be installed--> Processing Dependency: mysql-community-common(x86-64) = 5.7.38-1.el7 for package: mysql-community-server-5.7.38-1.el7.x86_64--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.38-1.el7.x86_64--> Running transaction check---> Package mysql-community-client.x86_64 0:5.7.38-1.el7 will be installed--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.38-1.el7.x86_64---> Package mysql-community-common.x86_64 0:5.7.38-1.el7 will be installed--> Running transaction check---> Package mysql-community-libs.x86_64 0:5.7.38-1.el7 will be installed--> Finished Dependency ResolutionDependencies Resolved====================================================================================================================== Package                            Arch               Version                    Repository                     Size======================================================================================================================Installing: mysql-community-server             x86_64             5.7.38-1.el7               mysql57-community             178 MInstalling for dependencies: mysql-community-client             x86_64             5.7.38-1.el7               mysql57-community              28 M mysql-community-common             x86_64             5.7.38-1.el7               mysql57-community             311 k mysql-community-libs               x86_64             5.7.38-1.el7               mysql57-community             2.6 MTransaction Summary======================================================================================================================Install  1 Package (+3 Dependent packages)Total download size: 208 MInstalled size: 890 MDownloading packages:warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.38-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEYPublic key for mysql-community-common-5.7.38-1.el7.x86_64.rpm is not installed(1/4): mysql-community-common-5.7.38-1.el7.x86_64.rpm                                          | 311 kB  00:00:01     (2/4): mysql-community-libs-5.7.38-1.el7.x86_64.rpm                                            | 2.6 MB  00:00:04     (3/4): mysql-community-client-5.7.38-1.el7.x86_64.rpm                                          |  28 MB  00:00:37     (4/4): mysql-community-server-5.7.38-1.el7.x86_64.rpm                                          | 178 MB  00:02:13     ----------------------------------------------------------------------------------------------------------------------Total                                                                                 1.5 MB/s | 208 MB  00:02:18     Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysqlImporting GPG key 0x5072E1F5: Userid     : "MySQL Release Engineering " Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5 Package    : mysql57-community-release-el7-9.noarch (installed) From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysqlPublic key for mysql-community-server-5.7.38-1.el7.x86_64.rpm is not installed Failing package is: mysql-community-server-5.7.38-1.el7.x86_64 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

执行命令后,正常下载即可,但是下载完后会有个告警
失败的软件包是:mysql-community-server-5.7.38-1.el7.x86_64
GPG密钥配置为:file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
 
处理办法:修改 /etc/yum.repos.d/mysql-community.repo,找到你安装的MySql版本。修改其中的gpgcheck=0即可,默认值为1

[root@VM-4-16-centos yum.repos.d]# vim /etc/yum.repos.d/mysql-community.repo

6、启动MySQL服务并查看启动状态

启动Mysql错误:[root@admin ~]# systemctl start mysqld.serviceFailed to start mysqld.service: Unit not found.解决办法:首先需要安装mariadb-server安装mariadb-server[root@admin ~]# yum install -y mariadb mariadb-server mariadb.service# 启动服务[root@admin ~]# systemctl start mariadb.service# 添加到开机启动[root@admin ~]# systemctl enable mariadb.service再次启动Mysql[root@admin ~]# systemctl start mysqld.service

7、查看启动状态 

[root@admin ~]# systemctl status mysqld● mysqld.service - MySQL Server   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)   Active: active (running) since Tue 2022-05-10 14:45:17 CST; 1min 59s ago     Docs: man:mysqld(8)           http://dev.mysql.com/doc/refman/en/using-systemd.html  Process: 4564 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)  Process: 3788 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 4567 (mysqld)    Tasks: 27   CGroup: /system.slice/mysqld.service           └─4567 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pidMay 10 14:44:36 admin systemd[1]: Starting MySQL Server...May 10 14:45:17 admin systemd[1]: Started MySQL Server.

runing表示已经启动

8、查看MySql的版本号

[root@admin ~]# mysql -Vmysql  Ver 14.14 Distrib 5.7.38, for Linux (x86_64) using  EditLine wrapper

9、查看MySql初始密码

通过my.cnf文件找到log-error=/var/log/mysqld.log文件,在改文件中根据password关键字查询初始密码

[root@VM-4-16-centos ~]# vim /etc/my.cnf

找到初始密码之后,就可以开始登录了

[root@VM-4-16-centos ~]# grep "password" /var/log/mysqld.log

10、登录MySQL

[root@admin ~]# mysql -uroot -pEnter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.7.38Copyright (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> alter user 'root'@'localhost' identified by "Root@123";Query OK, 0 rows affected (0.00 sec)---开放连接权限mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Root@123' WITH GRANT OPTION;Query OK, 0 rows affected, 1 warning (0.00 sec)

安装完成!