CentOS安装MySQL 5.7

刘哲贤 6月前 ⋅ 206 阅读

在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。

1.下载并安装MySQL官方的 Yum Repository

[root@localhost ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

使用上面的命令就直接下载了安装用的Yum Repository,大概25KB的样子,然后就可以直接yum安装了。

[root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm

之后就开始安装MySQL服务器

[root@localhost ~]# yum -y install mysql-community-server

这步可能会花些时间,安装完成后就会覆盖掉之前的mariadb。

[root@VM-0-12-centos ~]# yum -y install mysql-community-server
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
mysql-connectors-community                                                                             | 2.5 kB  00:00:00     
mysql-tools-community                                                                                  | 2.5 kB  00:00:00     
mysql57-community                                                                                      | 2.5 kB  00:00:00     
(1/3): mysql-connectors-community/x86_64/primary_db                                                    |  62 kB  00:00:00     
(2/3): mysql-tools-community/x86_64/primary_db                                                         |  76 kB  00:00:00     
(3/3): mysql57-community/x86_64/primary_db                                                             | 224 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.31-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.31-1.el7 for package: mysql-community-server-5.7.31-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.31-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.31-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.31-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.7.31-1.el7 will be installed
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.64-1.el7 will be obsoleted
--> Processing Dependency: libmysqlclient.so.18()(64bit) for package: 2:postfix-2.10.1-7.el7.x86_64
--> Processing Dependency: libmysqlclient.so.18(libmysqlclient_18)(64bit) for package: 2:postfix-2.10.1-7.el7.x86_64
---> Package mysql-community-libs.x86_64 0:5.7.31-1.el7 will be obsoleting
--> Running transaction check
---> Package mysql-community-libs-compat.x86_64 0:5.7.31-1.el7 will be obsoleting
---> Package postfix.x86_64 2:2.10.1-7.el7 will be updated
---> Package postfix.x86_64 2:2.10.1-9.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================
 Package                                 Arch               Version                       Repository                     Size
==============================================================================================================================
Installing:
 mysql-community-libs                    x86_64             5.7.31-1.el7                  mysql57-community             2.3 M
     replacing  mariadb-libs.x86_64 1:5.5.64-1.el7
 mysql-community-libs-compat             x86_64             5.7.31-1.el7                  mysql57-community             1.2 M
     replacing  mariadb-libs.x86_64 1:5.5.64-1.el7
 mysql-community-server                  x86_64             5.7.31-1.el7                  mysql57-community             173 M
Installing for dependencies:
 mysql-community-client                  x86_64             5.7.31-1.el7                  mysql57-community              25 M
 mysql-community-common                  x86_64             5.7.31-1.el7                  mysql57-community             311 k
Updating for dependencies:
 postfix                                 x86_64             2:2.10.1-9.el7                os                            2.4 M

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

Total download size: 205 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.31-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Public key for mysql-community-common-5.7.31-1.el7.x86_64.rpm is not installed
(1/6): mysql-community-common-5.7.31-1.el7.x86_64.rpm                                                  | 311 kB  00:00:01     
(2/6): mysql-community-libs-5.7.31-1.el7.x86_64.rpm                                                    | 2.3 MB  00:00:01     
(3/6): mysql-community-libs-compat-5.7.31-1.el7.x86_64.rpm                                             | 1.2 MB  00:00:00     
(4/6): postfix-2.10.1-9.el7.x86_64.rpm                                                                 | 2.4 MB  00:00:01     
(5/6): mysql-community-client-5.7.31-1.el7.x86_64.rpm                                                  |  25 MB  00:00:05     
(6/6): mysql-community-server-5.7.31-1.el7.x86_64.rpm                                                  | 173 MB  00:00:53     
------------------------------------------------------------------------------------------------------------------------------
Total                                                                                         3.6 MB/s | 205 MB  00:00:56     
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 (@/mysql57-community-release-el7-10.noarch)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql-community-common-5.7.31-1.el7.x86_64                                                                 1/8 
  Installing : mysql-community-libs-5.7.31-1.el7.x86_64                                                                   2/8 
  Installing : mysql-community-libs-compat-5.7.31-1.el7.x86_64                                                            3/8 
  Installing : mysql-community-client-5.7.31-1.el7.x86_64                                                                 4/8 
  Installing : mysql-community-server-5.7.31-1.el7.x86_64                                                                 5/8 
  Updating   : 2:postfix-2.10.1-9.el7.x86_64                                                                              6/8 
  Cleanup    : 2:postfix-2.10.1-7.el7.x86_64                                                                              7/8 
  Erasing    : 1:mariadb-libs-5.5.64-1.el7.x86_64                                                                         8/8 
  Verifying  : mysql-community-libs-compat-5.7.31-1.el7.x86_64                                                            1/8 
  Verifying  : mysql-community-common-5.7.31-1.el7.x86_64                                                                 2/8 
  Verifying  : 2:postfix-2.10.1-9.el7.x86_64                                                                              3/8 
  Verifying  : mysql-community-server-5.7.31-1.el7.x86_64                                                                 4/8 
  Verifying  : mysql-community-client-5.7.31-1.el7.x86_64                                                                 5/8 
  Verifying  : mysql-community-libs-5.7.31-1.el7.x86_64                                                                   6/8 
  Verifying  : 1:mariadb-libs-5.5.64-1.el7.x86_64                                                                         7/8 
  Verifying  : 2:postfix-2.10.1-7.el7.x86_64                                                                              8/8 

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

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

Dependency Updated:
  postfix.x86_64 2:2.10.1-9.el7                                                                                               

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

Complete!

至此MySQL就安装完成了,然后是对MySQL的一些设置。

2.MySQL数据库设置

首先启动MySQL

[root@localhost ~]# systemctl start  mysqld.service

查看MySQL运行状态,运行状态如图:

● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-08-28 11:08:02 CST; 13s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 24023 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 23965 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 24026 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─24026 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Aug 28 11:07:56 VM-0-12-centos systemd[1]: Starting MySQL Server...
Aug 28 11:08:02 VM-0-12-centos systemd[1]: Started MySQL Server.

此时MySQL已经开始正常运行,不过要想进入MySQL还得先找出此时root用户的密码,通过如下命令可以在日志文件中找出密码:

[root@localhost ~]# grep "password" /var/log/mysqld.log
2020-08-28T03:07:58.019528Z 1 [Note] A temporary password is generated for root@localhost: rejTmAqHh0%K

如下命令进入数据库:

[root@VM-0-12-centos ~]# mysql -uroot -p
Enter password: rejTmAqHh0%K

输入初始密码,此时不能做任何事情,因为MySQL默认必须修改密码之后才能操作数据库:(密码太简单会报错)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';

这样配置完之后本地可能连不上数据库,被拒绝访问,所以得进入数据库执行以下命令:

[root@VM-0-12-centos ~]# mysql -uroot -p   ##连接数据库
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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.
 ###执行这条命令
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY 'Lzx_123456' WITH GRANT OPTION;   
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges
mysql> exit
Bye

[1]+  Stopped                 mysql -uroot -p
[root@VM-0-12-centos ~]# 

参考文章:CentOS7安装MySQL


全部评论: 0

    我有话说: