安装 MySQL5.7版本
MySQL网址:https://dev.mysql.com/downloads/mysql/
mysql-community-client 客户端应用程序
msyql-community-common 共享文件 数据库和客户端
mysq-community-devel 客户端安装应用程序的库文件和头文件
mysql-community-embedded 嵌入式函数库
mysql-community-embedded-devel 嵌入式函数库的库文件
mysql-community-embedd-compat 嵌入式的兼容函数库
mysql-community-server mysql服务
实验环境
- 操纵系统:Centos7.6
- IP地址:192.168.66.61
- 内存:2G
- CPU:4C
- 提前关闭selinux和防火墙
- 配置yum源
- 网络能够通
#关闭SElinux
[root@localhost ~]# setenforce 0 #临时关闭
[root@localhost ~]# vim /etc/selinux/config #永久关闭
修改内容
SELINUX=disabled
[root@localhost ~]# getenforce #获取状态
Disabled
#关闭防火墙
[root@localhost ~]# systemctl status firewalld.service #查看firewall状态
[root@localhost ~]# systemctl stop firewalld.service #关闭
[root@localhost ~]# systemctl disable firewalld.service #设置开机不启动
[root@localhost ~]# iptables -F #清空防火墙规则
一、配置yum源
1.先备份Centos-Base.repo,然后从阿里云下载centos7-repo
[root@localhost ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2023-03-12 17:24:42-- http://mirrors.aliyun.com/repo/Centos-7.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 117.169.106.235, 117.169.106.236, 117.169.106.237, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|117.169.106.235|: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-03-12 17:24:43 (361 KB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” [2523/2523])
#可选择执行或不执行
[root@localhost yum.repos.d]# yum clean all
[root@localhost yum.repos.d]# yum makecache
2.安装之前的环境检查
在Centos7的系统上默认是mariadb,如果要安装MySQL需要先把系统中存在的MySQL或者mariadb删除掉。
[root@localhost ~]# 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@localhost ~]# yum -y remove mariadb*
[root@localhost ~]# rpm -qa |grep mysql
qt-mysql-4.8.7-2.el7.x86_64
[root@localhost ~]# rpm -e qt-mysql-4.8.7-2.el7.x86_64
[root@localhost ~]# rpm -qa |grep maraidb
[root@localhost ~]# 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@localhost ~]# rm -rf /etc/selinux/targeted/active/modules/100/mysql
[root@localhost ~]# rm -rf /usr/lib64/mysql
[root@localhost ~]# rm -rf /usr/lib64/perl5/vendor_perl/auto/DBD/mysql
[root@localhost ~]# rm -rf /usr/lib64/perl5/vendor_perl/DBD/mysql
[root@localhost ~]# rm -rf /usr/share/mysql
3.下载MySQL的包
这里直接使用yum包,所以下载rpm包生成yum包
在没下载安装之前,我的系统中是没有MySQL相关的yum源的
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
back CentOS-fasttrack.repo
CentOS-Base.repo CentOS-Media.repo
CentOS-Base.repo.bak CentOS-Sources.repo
CentOS-CR.repo CentOS-Vault.repo
CentOS-Debuginfo.repo
[root@localhost yum.repos.d]# yum -y install wget
[root@localhost yum.repos.d]# wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
[root@localhost 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的包
[root@localhost yum.repos.d]# ls
back CentOS-Media.repo
CentOS-Base.repo CentOS-Sources.repo
CentOS-Base.repo.bak CentOS-Vault.repo
CentOS-CR.repo mysql-community.repo
CentOS-Debuginfo.repo mysql-community-source.repo
CentOS-fasttrack.repo
4.使用yum安装
[root@localhost yum.repos.d]# yum -y 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
[root@localhost yum.repos.d]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
[root@localhost yum.repos.d]# vim mysql-community.repo
gpgcheck=1 改为0不校验
解释:
enabled=1
#enabled表示当前仓库是否开启,1为开启,0为关闭,此项不写默认为开启
gpgcheck=0
#gpgcheck表示安装rpm包时,是否基于公私钥对匹配包的安全信息,1表示开启,0表示关闭,此项不写默认为验证
gpgkey #指定密钥的位置,比如阿里源密钥的位置如下
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
出现如图的内容表示安装成功:
5.启动并测试
先启动,然后查看运行状态:
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# systemctl status mysqld |grep active
Active: active (running) since 日 2023-03-12 18:20:14 CST; 3min 17s ago
[root@localhost ~]# systemctl status mysqld
[root@localhost ~]# ps -ef |grep mysqld |grep -v grep
mysql 22635 1 0 18:20 ? 00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
[root@localhost ~]# netstat -antp |grep 3306
tcp6 0 0 :::3306 :::* LISTEN 22635/mysqld
[root@localhost ~]# netstat -antp |grep mysqld
tcp6 0 0 :::3306 :::* LISTEN 22635/mysqld
查看版本:
[root@localhost ~]# mysqld -V
mysqld Ver 5.7.41 for Linux on x86_64 (MySQL Community Server (GPL))
二. 设置新密码并重新启动
1.设置新密码
MySQL的密码并不是默认为空,而是在启动的时候,默认在log日志中生成密码,可以通过命令查看到密码:
[root@localhost ~]# grep "temporary password" /var/log/mysqld.log
2023-03-12T10:20:12.831687Z 1 [Note] A temporary passwor is generated for root@localhost: e#/ar*7->+/H
随机密码难记,可以设置自己的密码
登录数据库
[root@localhost ~]# 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 'Lilinbo123!';
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
注意: 这里的密码如果设置的过于简单也会报错,要设置的复杂一点,密码长度大于8位数
2.重新登录测试
[root@localhost ~]# mysql -uroot -pLilinbo123!
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 lilinbo;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lilinbo |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
可以看到,数据库已经能够正常使用了,接下来,就开启MySQL学习之旅吧。