zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

【死磕数据库专栏启动】在CentOS7中安装 MySQL5.7版本实战

2023-04-18 16:09:17 时间

前言

学习MySQL是一件比较枯燥的事情,学习开始之前要先安装MySQL数据库,之后才能进行增删改查和架构的搭建。

MySQL包含的内容比较多,比如SQL语句的增删改查,读写锁,事务,数据备份,分库分表,存储引擎和,高可用,优化等等。

专栏 【死磕数据库】是云计算架构专栏中的一个分专栏,让我们一起学习数据库,死磕数据库。

在这里插入图片描述

实验环境

本次实验是单台虚拟机上安装MySQL,以下是简单的配置,虚拟机使用的是vmware ,刚开始学习的小伙伴建议与我的环境一致,这样才好一起探讨学习呀。

  • 操作系统: centos7.6
  • IP地址: 192.168.1.41
  • 内存: 2G
  • CPU: 2C

提前关闭selinux和防火墙:

[root@mufenggrow ~]# setenforce 0
[root@mufenggrow ~]# systemctl stop firewalld
[root@mufenggrow ~]# 

一. 安装MySQL

1.1 配置yum源

这里我们先备份 Centos-Base.repo,然后从阿里云下载centos7-repo:

  • 备份本地的yum源
[root@mufenggrow ~]# mv /etc/yum.repos.d/CentOS-Base.repo  /etc/yum.repos.d/CentOS-Base.repo.bak
  • 下载阿里云的yum源
[root@mufenggrow ~]#  wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2023-02-24 17:13:29--  http://mirrors.aliyun.com/repo/Centos-7.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 120.192.91.238, 120.220.92.236, 120.220.92.240, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|120.192.91.238|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2523 (2.5K) [application/octet-stream]
正在保存至: “/etc/yum.repos.d/CentOS-Base.repo”

100%[============================================================================>] 2,523       --.-K/s 用时 0.007s  

2023-02-24 17:13:30 (338 KB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” [2523/2523])

1.2 安装之前的环境检查

在centos7的系统上默认是mariadb, 如果要安装MySQL需要先把系统中存在的mysql或者mariadb删除掉。

  • 查看是否已经安装MySQL/mariadb ,如果已经安装先删除
[root@mufenggrow ~]# rpm -qa |grep mariadb
mariadb-server-5.5.60-1.el7_5.x86_64
mariadb-libs-5.5.60-1.el7_5.x86_64
mariadb-5.5.60-1.el7_5.x86_64
[root@mufenggrow ~]# yum remove mariadb -y

  • 再次查看是否还有残留,如果有就删除
[root@mufenggrow ~]# rpm -qa |grep mysql
qt-mysql-4.8.7-2.el7.x86_64
[root@mufenggrow ~]# rpm -e qt-mysql-4.8.7-2.el7.x86_64
  • MySQL和mariadb都要查看
[root@mufenggrow ~]# rpm -qa |grep maraidb
  • 最后查看老版本中MySQL相关的安装目录
[root@mufenggrow ~]# find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/usr/lib64/mysql
/usr/lib64/perl5/vendor_perl/auto/DBD/mysql
/usr/lib64/perl5/vendor_perl/DBD/mysql
/usr/share/mysql
[root@mufenggrow ~]# rm -rf /etc/selinux/targeted/active/modules/100/mysql
[root@mufenggrow ~]# rm -rf /usr/lib64/mysql
[root@mufenggrow ~]# rm -rf /usr/lib64/perl5/vendor_perl/auto/DBD/mysql
[root@mufenggrow ~]# rm -rf /usr/lib64/perl5/vendor_perl/DBD/mysql
[root@mufenggrow ~]# rm -rf /usr/share/mysql

1.3 下载MySQL的包

这里直接使用yum包,所以下载rpm包生成yum包

在没下载安装之前,我的系统中是没有MySQL相关的yum源的:
在这里插入图片描述

  • 下载MySQL57-community的rpm包
[root@mufenggrow yum.repos.d]# yum install wget -y && wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

这里我担心没有wget,先安装wget,再使用wget下载

  • 使用rpm -ivh 安装刚刚下载的rpm包
[root@mufenggrow yum.repos.d]# rpm -ivh mysql57-community-release-el7-9.noarch.rpm 
警告:mysql57-community-release-el7-9.noarch.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql57-community-release-el7-9  ################################# [100%]

这时候会看到生成了几个与MySQL相关的repo的包:

在这里插入图片描述

1.4 开始使用yum安装

直接使用yum安装即可:

[root@mufenggrow yum.repos.d]#  yum install mysql-community-server

此处如果出现如下报错:

mysql-community-libs-compat-5.7.41-1.el7.x86_64.rpm 的公钥尚未安装

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

只需要执行这条命令即可:

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

执行完成后重新进行安装:

[root@mufenggrow yum.repos.d]#  yum install mysql-community-server

出现如图的内容表示安装成功:

在这里插入图片描述

1.5 启动并测试

先启动,然后查看运行状态:

[root@mufenggrow yum.repos.d]# systemctl start mysqld
[root@mufenggrow yum.repos.d]# systemctl status mysqld |grep active
   Active: active (running) since 五 2023-02-24 18:17:05 CST; 22s ago
[root@mufenggrow yum.repos.d]# 

查看版本:

[root@mufenggrow yum.repos.d]# mysql -V
mysql  Ver 14.14 Distrib 5.7.41, for Linux (x86_64) using  EditLine wrapper

二. 设置新密码并重新启动

2.1 设置新密码

MySQL的密码并不是默认为空,而是在启动的时候,默认在log日志中生成密码,可以通过命令查看到密码:

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

在这里插入图片描述
随机密码超级难记,我们可以设置自己的密码

  • 先登录数据库
[root@mufenggrow yum.repos.d]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.41

Copyright (c) 2000, 2023, 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> 

  • 修改密码
mysql> alter user 'root'@'localhost' identified by 'Mufenggrow123!';
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

注意: 这里的密码如果设置的过于简单也会报错,要设置的复杂一点,密码长度大于8位数

2.2 重新登录测试

[root@mufenggrow ~]# mysql -uroot -pMufenggrow123!

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.41 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, 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> create database mufeng;

Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mufeng             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

可以看到,数据库已经能够正常使用了,接下来,就开启MySQL学习之旅吧。

总结

  • 💕 好啦,这就是今天要分享给大家的全部内容了,我们下期再见!
  • 💕 博客主页:mufeng.blog.csdn.net
  • 💕 本文由沐风晓月原创,首发于CSDN博客
  • 💕 每一个你想要学习的念头,都是未来的你像现在的你求救,不辜负未来,全力奔赴