Linux安装MYSQL5.7(centos7)--详尽版

MySQL是一个关系型数据库管理系统其属于 Oracle 旗下产品。MySQL 是目前最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一。MySQL是一种关系型数据库管理系统,关系数据库将数据保存在不同的表中,增加了速度并提高了灵活性。

MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。MySQL 软件采用了双授权政策,分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型和大型网站的开发都选择 MySQL 作为网站数据库。(参考百度百科

 本文以Centos7系统为例安装MYSQL数据库并介绍MYSQL数据库的基本使用。(Centos7安装Mysql设计库主要参考Linux-centos7安装mysql步骤

本文以安装MYSQL5.7为例介绍安装MYSQL的步骤。

查询机器上安装的MYSQL

rpm -qa|grep mysql
[root@localhost ~]# rpm -qa|grep mysql

如果没有,说明没有安装MYSQL,如果有,可删除然后安装,若需要彻底删除MYSQL可参考文章Linux下彻底删除对MySQL进行删除。

对系统自带的mysql,查询系统使用的mysql 对应的文件夹,并进行删除

 whereis mysql
 find / -name mysql

完成查询后,无问题即可开始MYSQL的安装。 

查询可安装的版本

yum repolist all | grep mysql
[root@localhost ~]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community                 192
mysql-tools-community/x86_64      MySQL Tools Community                       90
mysql57-community/x86_64          MySQL 5.7 Community Server                 584
[root@localhost ~]# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64  MySQL Cluster 7.5 Community     禁用
mysql-cluster-7.5-community-source  MySQL Cluster 7.5 Community - S 禁用
mysql-connectors-community/x86_64   MySQL Connectors Community      启用:    192
mysql-connectors-community-source   MySQL Connectors Community - So 禁用
mysql-tools-community/x86_64        MySQL Tools Community           启用:     90
mysql-tools-community-source        MySQL Tools Community - Source  禁用
mysql-tools-preview/x86_64          MySQL Tools Preview             禁用
mysql-tools-preview-source          MySQL Tools Preview - Source    禁用
mysql55-community/x86_64            MySQL 5.5 Community Server      禁用
mysql55-community-source            MySQL 5.5 Community Server - So 禁用
mysql56-community/x86_64            MySQL 5.6 Community Server      禁用
mysql56-community-source            MySQL 5.6 Community Server - So 禁用
mysql57-community/x86_64            MySQL 5.7 Community Server      启用:    584
mysql57-community-source            MySQL 5.7 Community Server - So 禁用
mysql80-community/x86_64            MySQL 8.0 Community Server      禁用
mysql80-community-source            MySQL 8.0 Community Server - So 禁用

 可用版本如上图所示,本文安装MYSQL5.7。

1、Centos7通过yum安装MYSQL5.7

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

wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
[root@localhost ~]# ll
总用量 36
-rw-------. 1 root root  1736 7月  22 15:17 anaconda-ks.cfg
-rw-r--r--. 1 root root  1784 7月  22 15:26 initial-setup-ks.cfg
-rw-r--r--. 1 root root 25548 4月   7 2017 mysql57-community-release-el7-10.noarch.rpm

下载完成后,文件夹内出现mysql57-community-release-el7-10.noarch.rpm

2、yum安装MySQL5.7

yum -y install mysql57-community-release-el7-10.noarch.rpm
[root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm
已加载插件:fastestmirror, langpacks
正在检查 mysql57-community-release-el7-10.noarch.rpm: mysql57-community-release-el7-10.noarch
mysql57-community-release-el7-10.noarch.rpm 将被安装
正在解决依赖关系
There are unfinished transactions remaining. You might consider running yum-complete-transaction, or "yum-complete-transaction --cleanup-only" and "yum history redo last", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).
--> 正在检查事务
---> 软件包 mysql57-community-release.noarch.0.el7-10 将被 安装
--> 解决依赖关系完成
base/7/x86_64                                                                                          | 3.6 kB  00:00:00
elrepo                                                                                                 | 3.0 kB  00:00:00
extras/7/x86_64                                                                                        | 2.9 kB  00:00:00
updates/7/x86_64                                                                                       | 2.9 kB  00:00:00

依赖关系解决

==============================================================================================================================
 Package                            架构            版本              源                                                 大小
==============================================================================================================================
正在安装:
 mysql57-community-release          noarch          el7-10            /mysql57-community-release-el7-10.noarch           30 k

事务概要
==============================================================================================================================
安装  1 软件包

总计:30 k
安装大小:30 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : mysql57-community-release-el7-10.noarch                                                                   1/1
  验证中      : mysql57-community-release-el7-10.noarch                                                                   1/1

已安装:
  mysql57-community-release.noarch 0:el7-10

完毕!

安装完毕后,开始安装MySQL服务器

3、安装MySQL服务器

yum -y install mysql-community-server
[root@localhost ~]# yum -y install mysql-community-server
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.ustc.edu.cn
 * elrepo: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.ustc.edu.cn
正在解决依赖关系
There are unfinished transactions remaining. You might consider running yum-complete-transaction, or "yum-complete-transaction --cleanup-only" and "yum history redo last", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).
--> 正在检查事务
---> 软件包 mysql-community-server.x86_64.0.5.7.38-1.el7 将被 安装
--> 正在处理依赖关系 mysql-community-common(x86-64) = 5.7.38-1.el7,它被软件包 mysql-community-server-5.7.38-1.el7.x86_64 需要
--> 正在处理依赖关系 mysql-community-client(x86-64) >= 5.7.9,它被软件包 mysql-community-server-5.7.38-1.el7.x86_64 需要
--> 正在检查事务
---> 软件包 mysql-community-client.x86_64.0.5.7.38-1.el7 将被 安装
--> 正在处理依赖关系 mysql-community-libs(x86-64) >= 5.7.9,它被软件包 mysql-community-client-5.7.38-1.el7.x86_64 需要
---> 软件包 mysql-community-common.x86_64.0.5.7.38-1.el7 将被 安装
--> 正在检查事务
---> 软件包 mariadb-libs.x86_64.1.5.5.68-1.el7 将被 取代
--> 正在处理依赖关系 libmysqlclient.so.18()(64bit),它被软件包 2:postfix-2.10.1-9.el7.x86_64 需要
--> 正在处理依赖关系 libmysqlclient.so.18(libmysqlclient_18)(64bit),它被软件包 2:postfix-2.10.1-9.el7.x86_64 需要
---> 软件包 mysql-community-libs.x86_64.0.5.7.38-1.el7 将被 舍弃
--> 正在检查事务
---> 软件包 mysql-community-libs-compat.x86_64.0.5.7.38-1.el7 将被 舍弃
--> 解决依赖关系完成

依赖关系解决

==============================================================================================================================
 Package                                  架构                版本                       源                              大小
==============================================================================================================================
正在安装:
 mysql-community-libs                     x86_64              5.7.38-1.el7               mysql57-community              2.6 M
      替换  mariadb-libs.x86_64 1:5.5.68-1.el7
 mysql-community-libs-compat              x86_64              5.7.38-1.el7               mysql57-community              1.2 M
      替换  mariadb-libs.x86_64 1:5.5.68-1.el7
 mysql-community-server                   x86_64              5.7.38-1.el7               mysql57-community              178 M
为依赖而安装:
 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

事务概要
==============================================================================================================================
安装  3 软件包 (+2 依赖软件包)

总下载量:209 M
Downloading packages:
警告:/var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.38-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
mysql-community-common-5.7.38-1.el7.x86_64.rpm 的公钥尚未安装
(1/5): mysql-community-common-5.7.38-1.el7.x86_64.rpm                                                  | 311 kB  00:00:01
(2/5): mysql-community-libs-5.7.38-1.el7.x86_64.rpm                                                    | 2.6 MB  00:00:00
(3/5): mysql-community-libs-compat-5.7.38-1.el7.x86_64.rpm                                             | 1.2 MB  00:00:00
(4/5): mysql-community-client-5.7.38-1.el7.x86_64.rpm                                                  |  28 MB  00:00:15
(5/5): mysql-community-server-5.7.38-1.el7.x86_64.rpm                                                  | 178 MB  00:00:38
------------------------------------------------------------------------------------------------------------------------------
总计                                                                                          5.0 MB/s | 209 MB  00:00:41
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥
导入 GPG key 0x5072E1F5:
 用户ID     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 指纹       : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 软件包     : mysql57-community-release-el7-10.noarch (@/mysql57-community-release-el7-10.noarch)
 来自       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql


mysql-community-libs-compat-5.7.38-1.el7.x86_64.rpm 的公钥尚未安装
源 "MySQL 5.7 Community Server" 的 GPG 密钥已安装,但是不适用于此软件包。请检查源的公钥 URL 是否配置正确。


 失败的软件包是:mysql-community-libs-compat-5.7.38-1.el7.x86_64
 GPG  密钥配置为:file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

提升公钥未安装,具体解决方法如下。

运行如下命令(通过URL导入mysql的公钥到RPM的配置中

以下命令只适用于mysql5.7,其他版本查看官网相关说明,选择相应版本,查看解决方案

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

然后重新执行如下命令即可成功安装。(参考Centos7安装Mysql5.7 提示GPG密钥不存在/不合适

yum -y install mysql-community-server
[root@localhost ~]# yum -y install mysql-community-server
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * elrepo: mirrors.tuna.tsinghua.edu.cn
 * extras: ftp.sjtu.edu.cn
 * updates: ftp.sjtu.edu.cn
正在解决依赖关系
There are unfinished transactions remaining. You might consider running yum-complete-transaction, or "yum-complete-transaction --cleanup-only" and "yum history redo last", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).
--> 正在检查事务
---> 软件包 mysql-community-server.x86_64.0.5.7.38-1.el7 将被 安装
--> 正在处理依赖关系 mysql-community-common(x86-64) = 5.7.38-1.el7,它被软件包 mysql-community-server-5.7.38-1.el7.x86_64 需要
--> 正在处理依赖关系 mysql-community-client(x86-64) >= 5.7.9,它被软件包 mysql-community-server-5.7.38-1.el7.x86_64 需要
--> 正在检查事务
---> 软件包 mysql-community-client.x86_64.0.5.7.38-1.el7 将被 安装
--> 正在处理依赖关系 mysql-community-libs(x86-64) >= 5.7.9,它被软件包 mysql-community-client-5.7.38-1.el7.x86_64 需要
---> 软件包 mysql-community-common.x86_64.0.5.7.38-1.el7 将被 安装
--> 正在检查事务
---> 软件包 mariadb-libs.x86_64.1.5.5.68-1.el7 将被 取代
--> 正在处理依赖关系 libmysqlclient.so.18()(64bit),它被软件包 2:postfix-2.10.1-9.el7.x86_64 需要
--> 正在处理依赖关系 libmysqlclient.so.18(libmysqlclient_18)(64bit),它被软件包 2:postfix-2.10.1-9.el7.x86_64 需要
---> 软件包 mysql-community-libs.x86_64.0.5.7.38-1.el7 将被 舍弃
--> 正在检查事务
---> 软件包 mysql-community-libs-compat.x86_64.0.5.7.38-1.el7 将被 舍弃
--> 解决依赖关系完成

依赖关系解决

==============================================================================================================================
 Package                                  架构                版本                       源                              大小
==============================================================================================================================
正在安装:
 mysql-community-libs                     x86_64              5.7.38-1.el7               mysql57-community              2.6 M
      替换  mariadb-libs.x86_64 1:5.5.68-1.el7
 mysql-community-libs-compat              x86_64              5.7.38-1.el7               mysql57-community              1.2 M
      替换  mariadb-libs.x86_64 1:5.5.68-1.el7
 mysql-community-server                   x86_64              5.7.38-1.el7               mysql57-community              178 M
为依赖而安装:
 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

事务概要
==============================================================================================================================
安装  3 软件包 (+2 依赖软件包)

总计:209 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : mysql-community-common-5.7.38-1.el7.x86_64                                                                1/6
  正在安装    : mysql-community-libs-5.7.38-1.el7.x86_64                                                                  2/6
  正在安装    : mysql-community-client-5.7.38-1.el7.x86_64                                                                3/6
  正在安装    : mysql-community-server-5.7.38-1.el7.x86_64                                                                4/6
  正在安装    : mysql-community-libs-compat-5.7.38-1.el7.x86_64                                                           5/6
  正在删除    : 1:mariadb-libs-5.5.68-1.el7.x86_64                                                                        6/6
  验证中      : mysql-community-libs-compat-5.7.38-1.el7.x86_64                                                           1/6
  验证中      : mysql-community-client-5.7.38-1.el7.x86_64                                                                2/6
  验证中      : mysql-community-server-5.7.38-1.el7.x86_64                                                                3/6
  验证中      : mysql-community-libs-5.7.38-1.el7.x86_64                                                                  4/6
  验证中      : mysql-community-common-5.7.38-1.el7.x86_64                                                                5/6
  验证中      : 1:mariadb-libs-5.5.68-1.el7.x86_64                                                                        6/6

已安装:
  mysql-community-libs.x86_64 0:5.7.38-1.el7                  mysql-community-libs-compat.x86_64 0:5.7.38-1.el7
  mysql-community-server.x86_64 0:5.7.38-1.el7

作为依赖被安装:
  mysql-community-client.x86_64 0:5.7.38-1.el7                  mysql-community-common.x86_64 0:5.7.38-1.el7

替代:
  mariadb-libs.x86_64 1:5.5.68-1.el7

完毕!

安装完成后,需要进行相应的配置才能正常使用

2、MySQL数据库设置

查看当前启用的MYSQL版本

yum repolist enabled | grep mysql
[root@localhost ~]# yum repolist enabled | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community                 192
mysql-tools-community/x86_64      MySQL Tools Community                       90
mysql57-community/x86_64          MySQL 5.7 Community Server                 584

1、启动MySQL

systemctl start mysqld

[root@localhost ~]# systemctl start mysqld
[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-07-25 17:16:06 CST; 13s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 8918 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 8862 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 8923 (mysqld)
    Tasks: 27
   CGroup: /system.slice/mysqld.service
           └─8923 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

7月 25 17:16:02 localhost.localdomain systemd[1]: Starting MySQL Server...
7月 25 17:16:06 localhost.localdomain systemd[1]: Started MySQL Server.

查询MySQL状态已经正常启动

2、如果要进入MySQL现在需要先找出root用户的密码,通过如下命令可以在日志文件中找出密码

grep "password" /var/log/mysqld.log

[root@localhost ~]# grep "password" /var/log/mysqld.log
2022-07-25T09:16:04.613959Z 1 [Note] A temporary password is generated for root@localhost: 你的MYSQL临时密码

3、进入MYSQL并修改密码

[root@localhost ~]# mysql -uroot -p你的MySQL临时密码
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.38

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.

mysql>

通过如下命令设置新密码

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

 把new password改为你需要设置的密码

但是,设置新密码的时候如果设置的过于简单会报错

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '1111';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

设置1111,提升密码过于简单

解决方案如下

通过如下命令设置密码规则

set global validate_password_policy=0;
set global validate_password_length=1;

上面一条命令为设置密码规则,下面为设置密码最小长度

mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '1111';
Query OK, 0 rows affected (0.00 sec)

执行完成后再修改简单的密码(1111)即可成功

[root@localhost ~]# mysql -uroot -p1111
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.38 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.

mysql>

恭喜你,完成MYSQL5.7的安装

4、因为安装了Yum Repository,以后每次yum操作都会自动更新,需要把这个卸载掉

yum -y remove mysql57-community-release-el7-10.noarch
[root@localhost ~]# yum -y remove mysql57-community-release-el7-10.noarch
已加载插件:fastestmirror, langpacks
正在解决依赖关系
There are unfinished transactions remaining. You might consider running yum-complete-transaction, or "yum-complete-transaction --cleanup-only" and "yum history redo last", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).
--> 正在检查事务
---> 软件包 mysql57-community-release.noarch.0.el7-10 将被 删除
--> 解决依赖关系完成

依赖关系解决

==============================================================================================================================
 Package                            架构            版本             源                                                  大小
==============================================================================================================================
正在删除:
 mysql57-community-release          noarch          el7-10           @/mysql57-community-release-el7-10.noarch           30 k

事务概要
==============================================================================================================================
移除  1 软件包

安装大小:30 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在删除    : mysql57-community-release-el7-10.noarch                                                                   1/1
  验证中      : mysql57-community-release-el7-10.noarch                                                                   1/1

删除:
  mysql57-community-release.noarch 0:el7-10

完毕!

完成!

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QwQ一叶

你的打赏是我制作教程最大的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值