腾讯云centos7安装mysql5.7

Posted 敲代码的xiaolang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了腾讯云centos7安装mysql5.7相关的知识,希望对你有一定的参考价值。

昨天服务器上的数据库被勒索了,重装系统之后不得不再装一次数据库,踩了很多坑,在此记录安装过程。

  • 首先把centos7自带的数据库mariadb卸载掉,把mysql的相关文件夹都删掉。

查看组件服务

rpm -qa | grep -i mariadb
rpm -qa | grep -i mysql

查看相关文件夹

find / -name mysql
find / -name mariadb

组件服务删除

rpm -ev --nodeps 组键服务名

文件删除

rm -rf 文件名

演示如下

[root@VM-16-13-centos ~]# rpm -qa | grep -i mariadb
mariadb-libs-5.5.68-1.el7.x86_64


[root@VM-16-13-centos ~]# rpm -qa | grep -i mysql
​ ‌‍‍‏​‌‏

[root@VM-16-13-centos ~]# find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/usr/share/mysql
/usr/lib64/mysql
find: ‘/proc/7206’: No such file or directory


[root@VM-16-13-centos ~]# find / -name mariadb
​ ‌‍‍‏​‌‍

[root@VM-16-13-centos ~]# rpm -ev --nodeps  ‌‍‍‏mariadb-libs-5.5.68-1.el7.x86_6
error: package ‌‍‍‏mariadb-libs-5.5.68-1.el7.x86_6 is not installed


[root@VM-16-13-centos ~]# rm -rf  ‌‍‍‏/etc/selinux/targeted/active/modules/100/mysql
​ ‌‍‍‏​‌‍

[root@VM-16-13-centos ~]# rm -rf /usr/share/mysql
​ ‌‍‍‏​‌‍

[root@VM-16-13-centos ~]# rm -rf /usr/lib64/mysql
  • 在home文件夹下新建一个data文件夹

演示如下

[root@VM-16-13-centos ~]# cd /home/
​ ‌‍‍‏​‌‍

[root@VM-16-13-centos home]# mkdir data
​ ‌‍‍‏​‌‍

[root@VM-16-13-centos home]# ls
data  lighthouse


[root@VM-16-13-centos home]# cd data
  • 下载 MySQL的yum包,这里我先下载的wget,如果你存在的话就不用安装它,直接使用下面的语句即可:
wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm

如果不存在,请先安装wget:

yum -y install wget

演示如下

[root@VM-16-13-centos data]# yum -y install wget
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
epel                                                                                                                                   | 4.7 kB  00:00:00     
extras                                                                                                                                 | 2.9 kB  00:00:00     
os                                                                                                                                     | 3.6 kB  00:00:00     
updates                                                                                                                                | 2.9 kB  00:00:00     
Package wget-1.14-18.el7_6.1.x86_64 already installed and latest version
Nothing to do


[root@VM-16-13-centos data]# wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
--2022-10-31 23:05:38--  http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 184.30.152.230
Connecting to repo.mysql.com (repo.mysql.com)|184.30.152.230|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 25548 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-10.noarch.rpm’

100%[====================================================================================================================>] 25,548       135KB/s   in 0.2s   

2022-10-31 23:05:39 (135 KB/s) - ‘mysql57-community-release-el7-10.noarch.rpm’ saved [25548/25548]
  • 然后安装MySQL源
rpm -Uvh mysql57-community-release-el7-10.noarch.rpm

演示如下

[root@VM-16-13-centos data]# rpm -Uvh mysql57-community-release-el7-10.noarch.rpm
warning: mysql57-community-release-el7-10.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql57-community-release-el7-10 ################################# [100%]
  • 安装mysql的服务
yum install -y mysql-community-server

演示如下

[root@VM-16-13-centos data]# yum install -y mysql-community-server
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
mysql-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-connectors-community/x86_64/primary_db                                                                                    |  93 kB  00:00:00     
(2/3): mysql-tools-community/x86_64/primary_db                                                                                         |  89 kB  00:00:01     
(3/3): mysql57-community/x86_64/primary_db                                                                                             | 325 kB  00:00:01     
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.40-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.40-1.el7 for package: mysql-community-server-5.7.40-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.40-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.40-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.40-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.7.40-1.el7 will be installed
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.68-1.el7 will be obsoleted
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64
---> Package mysql-community-libs.x86_64 0:5.7.40-1.el7 will be obsoleting
--> Running transaction check
---> Package mysql-community-libs-compat.x86_64 0:5.7.40-1.el7 will be obsoleting
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================
 Package                                          Arch                        Version                            Repository                              Size
==============================================================================================================================================================
Installing:
 mysql-community-libs                             x86_64                      5.7.40-1.el7                       mysql57-community                      2.6 M
     replacing  mariadb-libs.x86_64 1:5.5.68-1.el7
 mysql-community-libs-compat                      x86_64                      5.7.40-1.el7                       mysql57-community                      1.2 M
     replacing  mariadb-libs.x86_64 1:5.5.68-1.el7
 mysql-community-server                           x86_64                      5.7.40-1.el7                       mysql57-community                      178 M
Installing for dependencies:
 mysql-community-client                           x86_64                      5.7.40-1.el7                       mysql57-community                       28 M
 mysql-community-common                           x86_64                      5.7.40-1.el7                       mysql57-community                      311 k

Transaction Summary
==============================================================================================================================================================
Install  3 Packages (+2 Dependent packages)

Total download size: 211 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.40-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
Public key for mysql-community-common-5.7.40-1.el7.x86_64.rpm is not installed
(1/5): mysql-community-common-5.7.40-1.el7.x86_64.rpm                                                                                  | 311 kB  00:00:01     
(2/5): mysql-community-libs-5.7.40-1.el7.x86_64.rpm                                                                                    | 2.6 MB  00:00:02     
(3/5): mysql-community-libs-compat-5.7.40-1.el7.x86_64.rpm                                                                             | 1.2 MB  00:00:00     
(4/5): mysql-community-client-5.7.40-1.el7.x86_64.rpm                                                                                  |  28 MB  00:00:12     
(5/5): mysql-community-server-5.7.40-1.el7.x86_64.rpm                                                                                  | 178 MB  00:00:55     
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                         3.5 MB/s | 211 MB  00:01:00     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
 Userid     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 Package    : mysql57-community-release-el7-10.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql


Public key for mysql-community-client-5.7.40-1.el7.x86_64.rpm is not installed


 Failing package is: mysql-community-client-5.7.40-1.el7.x86_64
 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

然后我们发现有一个bug,就是:

Public key for mysql-community-client-5.7.40-1.el7.x86_64.rpm is not installed

解决方法:

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

然后继续安装mysql的服务:

yum install mysql-server

演示过程

[root@VM-16-13-centos data]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
​ ‌‍‍‏​‌‍

[root@VM-16-13-centos data]# yum install mysql-server
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.40-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.40-1.el7 for package: mysql-community-server-5.7.40-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.40-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.40-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.40-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.7.40-1.el7 will be installed
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.68-1.el7 will be obsoleted
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-9.el7.x86_64
---> Package mysql-community-libs.x86_64 0:5.7.40-1.el7 will be obsoleting
--> Running transaction check
---> Package mysql-community-libs-compat.x86_64 0:5.7.40-1.el7 will be obsoleting
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================
 Package                                          Arch                        Version                            Repository                              Size
==============================================================================================================================================================
Installing:
 mysql-community-libs                             x86_64                      5.7.40-1.el7                       mysql57-community                      2.6 M
     replacing  mariadb-libs.x86_64 1:5.5.68-1.el7
 mysql-community-libs-compat                      x86_64                      5.7.40-1.el7                       mysql57-community                      1.2 M
     replacing  mariadb-libs.x86_64 1:5.5.68-1.el7
 mysql-community-server                           x86_64                      5.7.40-1.el7                       mysql57-community                      178 M
Installing for dependencies:
 mysql-community-client                           x86_64                      5.7.40-1.el7                       mysql57-community                       28 M
 mysql-community-common                           x86_64                      5.7.40-1.el7                       mysql57-community                      311 k

Transaction Summary
==============================================================================================================================================================
Install  3 Packages (+2 Dependent packages)

Total size: 211 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : mysql-community-common-5.7.40-1.el7.x86_64                                                                                                 1/6 
  Installing : mysql-community-libs-5.7.40-1.el7.x86_64                                                                                                   2/6 
  Installing : mysql-community-client-5.7.40-1.el7.x86_64                                                                                                 3/6 
  Installing : mysql-community-server-5.7.40-1.el7.x86_64                                                                                                 4/6 
  Installing : mysql-community-libs-compat-5.7.40-1.el7.x86_64                                                                                            5/6 
  Erasing    : 1:mariadb-libs-5.5.68-1.el7.x86_64                                                                                                         6/6 
warning: file /usr/lib64/mysql/plugin/mysql_clear_password.so: remove failed: No such file or directory
warning: file /usr/lib64/mysql/plugin/dialog.so: remove failed: No such file or directory
warning: file /usr/lib64/mysql/libmysqlclient.so.18.0.0: remove failed: No such file or directory
  Verifying  : mysql-community-client-5.7.40-1.el7.x86_64                                                                                                 1/6 
  Verifying  : mysql-community-server-5.7.40-1.el7.x86_64                                                                                                 2/6 
  Verifying  : mysql-community-common-5.7.40-1.el7.x86_64                                                                                                 3/6 
  Verifying  : mysql-community-libs-5.7.40-1.el7.x86_64                                                                                                   4/6 
  Verifying  : mysql-community-libs-compat-5.7.40-1.el7.x86_64                                                                                            5/6 
  Verifying  : 1:mariadb-libs-5.5.68-1.el7.x86_64                                                                                                         6/6 

Installed:
  mysql-community-libs.x86_64 0:5.7.40-1.el7       mysql-community-libs-compat.x86_64 0:5.7.40-1.el7       mysql-community-server.x86_64 0:5.7.40-1.el7      

Dependency Installed:
  mysql-community-client.x86_64 0:5.7.40-1.el7                                  mysql-community-common.x86_64 0:5.7.40-1.el7                                 

Replaced:
  mariadb-libs.x86_64 1:5.5.68-1.el7                                                                                                                          

Complete!
  • 启动mysql服务
systemctl start mysqld.service
  • 查看mysql服务是否已经启动
systemctl status mysqld.service

演示过程

[root@VM-16-13-centos data]# systemctl start mysqld.service
​ ‌‍‍‏​‌‍

[root@VM-16-13-centos data]# systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2022-10-31 23:09:22 CST; 8s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 320 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 32489 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 323 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─323 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Oct 31 23:09:17 VM-16-13-centos systemd[1]: Starting MySQL Server...
Oct 31 23:09:22 VM-16-13-centos systemd[1]: Started MySQL Server.
  • 查看生成的临时密码
grep 'temporary password' /var/log/mysqld.log 

演示过程
如下,&tbS5gCo5;Ka就是临时密码

[root@VM-16-13-centos data]# grep 'temporary password' /var/log/mysqld.log 
2022-10-31T15:09:18.499659Z 1 [Note] A temporary password is generated for root@localhost: &tbS5gCo5;Ka
  • 登录MySQL
mysql -uroot -p (临时密码)

然后这里出现了一个错误,也就是虽然输入了正确的密码,但是还是无法进入,提示如下:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

解决方法:
初期考虑是服务器的端口有问题,修改了端口后,还是不行,于是进行配置文件的修改:

vim /etc/my.cnf

编辑 /etc/my.cnf ,在[mysqld] 部分最后添加一行

skip-grant-tables  

保存后,重启mysql

service mysqld restart  

然后重新登陆mysql,此时就不需要密码就可以登录了

演示过程

[root@VM-16-13-centos data]# vim /etc/my.cnf
​ ‌‍‍‏​‌‍

[root@VM-16-13-centos data]# service mysqld restart  
Redirecting to /bin/systemctl restart mysqld.service


[root@VM-16-13-centos data]# mysql -uroot 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 2
Server version: 5.7.40 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.
  • 然后设置新的密码
set password for root@localhost = password('你的新密码');

然后此时出现了报错:

ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

解决方法

flush privileges;
set password for root@localhost = password('你的新密码');
flush privileges;
vim /etc/my.cnf  #(把最后添加的代码注释掉:skip-grant-tables  )
service mysqld restart  

演示过程

mysql> set password for root@localhost = password('你的新密码');
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> set password for root@localhost = password('你的新密码');
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;  
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye


[root@VM-16-13-centos data]# vim /etc/my.cnf
​ ‌‍‍‏​‌‍

腾讯云centos7安装mysql5.7

CentOS7安装mysql5.7

腾讯云搭建tomcat服务器

阿里云CentOS7安装MySQL5.7报错GPG校验不正确

阿里云CentOS7安装MySQL5.7成功2022年亲测

阿里云 CentOS7.4 环境安装mysql5.7