Linux下安装Mysql【CentOS7 】

Linux下安装Mysql

一、Linux下安装Mysql-5.7.41【tar包下载安装】

1.1.首先检查是否已经安装过mysql

bash 复制代码
rpm -qa | grep mysql

1.2.下载Linux版本的Mysql-5.7

Mysql官网:https://www.mysql.com/

下载网址:https://downloads.mysql.com/archives/community/

bash 复制代码
cat /etc/redhat-release






1.3.解压缩

首先使用第三方工具上传

bash 复制代码
# 创建一个文件夹,存放mysql
mkdir -p /usr/mysql

cd /usr/mysql

# 在当前目录下(mysql)下创建一个 mysql-5.7.41 文件夹
mkdir mysql-5.7.41

# 解压安装包到该目录下
tar -xvf mysql-5.7.41-1.el7.x86_64.rpm-bundle.tar -C mysql-5.7.41





bash 复制代码
[root@localhost mysql-5.7.41]# ls
mysql-community-client-5.7.41-1.el7.x86_64.rpm    
mysql-community-embedded-compat-5.7.41-1.el7.x86_64.rpm  
mysql-community-server-5.7.41-1.el7.x86_64.rpm
mysql-community-common-5.7.41-1.el7.x86_64.rpm    
mysql-community-embedded-devel-5.7.41-1.el7.x86_64.rpm   
mysql-community-test-5.7.41-1.el7.x86_64.rpm
mysql-community-devel-5.7.41-1.el7.x86_64.rpm     
mysql-community-libs-5.7.41-1.el7.x86_64.rpm
mysql-community-embedded-5.7.41-1.el7.x86_64.rpm  
mysql-community-libs-compat-5.7.41-1.el7.x86_64.rpm

1.4.安装执行 rpm 安装包需要先下载 openssl-devel 插件

解压后的文件都是 rpm 文件,因此需要用到 rpm 包资源管理器相关的指令安装这些 rpm 的安装包

bash 复制代码
yum install openssl-devel

1.5.安装 Mysql5.7 执行 rpm 安装包

安装完该插件之后,依次执行以下命令安装这些 rpm 包

bash 复制代码
yum install openssl-devel

rpm -ivh mysql-community-common-5.7.41-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.41-1.el7.x86_64.rpm

yum remove mysql-libs 

rpm -ivh mysql-community-libs-compat-5.7.41-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-5.7.41-1.el7.x86_64.rpm

# 至此所有依赖已安装完毕,下面安装客户端和服务端

rpm -ivh mysql-community-client-5.7.41-1.el7.x86_64.rpm
yum -y install net-tools
rpm -ivh mysql-community-server-5.7.41-1.el7.x86_64.rpm

# 查询 MySQL 的安装文件
rpm -qa | grep -i mysql

# yum remove openssl-devel
# yum remove net-tools

问题1

bash 复制代码
[root@localhost mysql-5.7.41]# rpm -ivh mysql-community-libs-5.7.41-1.el7.x86_64.rpm
警告:mysql-community-libs-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
        mysql-community-common(x86-64) >= 5.7.9 被 mysql-community-libs-5.7.41-1.el7.x86_64 需要
        mariadb-libs 被 mysql-community-libs-5.7.41-1.el7.x86_64 取代

解决方案1:清除之前安装过的依赖即可,最后重新安装下。

bash 复制代码
[root@localhost mysql-5.7.41]# yum remove mysql-libs 

问题2

bash 复制代码
[root@localhost mysql-5.7.41]# rpm -ivh mysql-community-server-5.7.41-1.el7.x86_64.rpm
警告:mysql-community-server-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
        net-tools 被 mysql-community-server-5.7.41-1.el7.x86_64 需要

解决方案2:

bash 复制代码
yum -y install net-tools


bash 复制代码
[root@localhost mysql-5.7.41]# rpm -ivh mysql-community-common-5.7.41-1.el7.x86_64.rpm
警告:mysql-community-common-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-common-5.7.41-1.e################################# [100%]
[root@localhost mysql-5.7.41]# 
[root@localhost mysql-5.7.41]# rpm -ivh mysql-community-libs-5.7.41-1.el7.x86_64.rpm
警告:mysql-community-libs-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-libs-5.7.41-1.el7################################# [100%]
[root@localhost mysql-5.7.41]# 
[root@localhost mysql-5.7.41]# rpm -ivh mysql-community-libs-compat-5.7.41-1.el7.x86_64.rpm
警告:mysql-community-libs-compat-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-libs-compat-5.7.4################################# [100%]
[root@localhost mysql-5.7.41]# 
[root@localhost mysql-5.7.41]# rpm -ivh mysql-community-devel-5.7.41-1.el7.x86_64.rpm
警告:mysql-community-devel-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-devel-5.7.41-1.el################################# [100%]
[root@localhost mysql-5.7.41]# 
[root@localhost mysql-5.7.41]# rpm -ivh mysql-community-client-5.7.41-1.el7.x86_64.rpm
警告:mysql-community-client-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-client-5.7.41-1.e################################# [100%]
[root@localhost mysql-5.7.41]# 
[root@localhost mysql-5.7.41]# rpm -ivh mysql-community-server-5.7.41-1.el7.x86_64.rpm
警告:mysql-community-server-5.7.41-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-server-5.7.41-1.e################################# [100%]

1.6.Mysql相关操作命令

bash 复制代码
# 启动 MySQL 服务
systemctl start mysqld

# 重启 MySQL 服务
systemctl restart mysqld

# 关闭 MySQL 服务
systemctl stop mysqld

#设置开机自启
systemctl enable mysqld

# 查看状态
systemctl status mysqld


1.7.查看Mysql-5.7 临时密码

rpm 安装 MySQL 会自动生成一个随机密码

bash 复制代码
grep 'temporary password' /var/log/mysqld.log

cat /var/log/mysqld.log

A temporary password is generated for root@localhost: 2yiNb5Pvum<4

账号:root

随机密码:2yiNb5Pvum<4

1.8.修改Mysql-5.7密码

bash 复制代码
# 登录
mysql -u root -p
2yiNb5Pvum<4

# 修改密码
ALTER  USER  'root'@'localhost'  IDENTIFIED BY '123456';

ALTER  USER  'root'@'localhost'  IDENTIFIED BY 'Mysql.123456';

# 设置密码的验证强度等级
#Mysql 5.7 版本
# 将密码复杂度校验调整简单类型
# set global validate_password_policy=MEDIUM;
set global validate_password_policy=LOW;
# 设置密码最少位数限制为 4 位
set global validate_password_length=4;
# 查看 mysql 初始的密码策略
SHOW VARIABLES LIKE 'validate_password%';
bash 复制代码
# 查看 mysql5.7 初始的密码策略
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | OFF    |
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | MEDIUM |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+


1.9.远程连接navicat--遇见问题

1.9.1.问题:1130 - Host '192.168.229.1' is not allowed to connect to this MySQL server

自己安装在本地的mysql默认时不允许进行远程登陆,如果想要远程访问本地的数据库需要修改访问权限,将'root'访问权限修改为'%'

1.9.2.连接服务器--修改权限

bash 复制代码
# 连接服务器
mysql -u root -p

# 看当前所有数据库
show databases;

# 进入mysql数据库
use mysql;

# 查看mysql数据库中所有的表
show tables;

# 执行更新权限语句
# 其中"%"代表所有的地址,意味着所有的地址都可以访问"root"
update user set Host='%' where User='root';

# 查看权限是否修改成功
select host,user from user;

# 刷新服务器配置
FLUSH PRIVILEGES;


1.9.3.连接成功

1.10.卸载Mysql-5.7

bash 复制代码
# 停止Mysql
systemctl stop mysqld

# 查询 MySQL 的安装文件
rpm -qa | grep -i mysql

# 卸载上述查询的所有安装包
rpm -e mysql-community-libs-5.7.41-1.el7.x86_64 --nodeps
rpm -e mysql-community-devel-5.7.41-1.el7.x86_64 --nodeps
rpm -e mysql-community-common-5.7.41-1.el7.x86_64 --nodeps
rpm -e mysql-community-libs-compat-5.7.41-1.el7.x86_64 --nodeps
rpm -e mysql-community-client-5.7.41-1.el7.x86_64 --nodeps
rpm -e mysql-community-server-5.7.41-1.el7.x86_64 --nodeps

# 再次查询 MySQL 的安装文件
rpm -qa | grep -i mysql

# 删除MySQL的数据存放目录
rm -rf /var/lib/mysql

# 找出服务器中分散的mysql文件夹,查询残留的文件和相关依赖,并删除
find / -name mysql 
rm -rf [显示的文件夹路径,每个绝对路径中间用空格隔开]

# MySQL配置文件通常位于/etc/my.cnf或/etc/mysql/my.cnf下,MySQL日志文件通常位于/var/log/mysql下
# 删除mysql日志
rm -rf /var/log/mysqld.log
# 删除MySQL的配置文件备份
rm -rf /etc/my.cnf.rpmsave
# 删除mysql配置文件
rm -rf /etc/mysql/my.cnf
rm -rf /etc/my.cnf

# 清除MySQL用户和组
userdel mysql
groupdel mysql



二、Linux下安装Mysql-8.0.34【tar包下载安装】

1.1.首先检查是否已经安装过mysql

bash 复制代码
rpm -qa | grep mysql

2.2.下载Linux版本的Mysql-8.0.34

Mysql官网:https://www.mysql.com/

下载网址:https://downloads.mysql.com/archives/community/

bash 复制代码
cat /etc/redhat-release





2.3.解压缩

首先使用第三方工具上传

bash 复制代码
# 创建一个文件夹,存放mysql
mkdir -p /usr/mysql

cd /usr/mysql

# 在当前目录下(mysql)下创建一个 mysql-5.7.41 文件夹
mkdir mysql-8.0.34

# 解压安装包到该目录下
tar -xvf mysql-8.0.34-1.el7.x86_64.rpm-bundle.tar -C mysql-8.0.34



bash 复制代码
[root@localhost mysql-8.0.34]# ls
mysql-community-client-8.0.34-1.el7.x86_64.rpm          
mysql-community-debuginfo-8.0.34-1.el7.x86_64.rpm        
mysql-community-icu-data-files-8.0.34-1.el7.x86_64.rpm  
mysql-community-server-8.0.34-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.34-1.el7.x86_64.rpm  
mysql-community-devel-8.0.34-1.el7.x86_64.rpm            
mysql-community-libs-8.0.34-1.el7.x86_64.rpm            
mysql-community-server-debug-8.0.34-1.el7.x86_64.rpm
mysql-community-common-8.0.34-1.el7.x86_64.rpm          
mysql-community-embedded-compat-8.0.34-1.el7.x86_64.rpm  
mysql-community-libs-compat-8.0.34-1.el7.x86_64.rpm     
mysql-community-test-8.0.34-1.el7.x86_64.rpm

2.4.安装执行 rpm 安装包需要先下载 openssl-devel 插件

解压后的文件都是 rpm 文件,因此需要用到 rpm 包资源管理器相关的指令安装这些 rpm 的安装包

bash 复制代码
yum install openssl-devel

2.5.安装 Mysql8.0 执行 rpm 安装包

安装完该插件之后,依次执行以下命令安装这些 rpm 包

bash 复制代码
yum install openssl-devel
# yum install net-tools

rpm -ivh mysql-community-common-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-8.0.34-1.el7.x86_64.rpm

# 至此所有依赖已安装完毕,下面安装客户端和服务端

rpm -ivh mysql-community-client-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.0.34-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.34-1.el7.x86_64.rpm

# 查询 MySQL 的安装文件
rpm -qa | grep -i mysql

# yum remove openssl-devel
# yum remove net-tools

2.6.Mysql相关操作命令

bash 复制代码
# 启动 MySQL 服务
systemctl start mysqld

# 重启 MySQL 服务
systemctl restart mysqld

# 关闭 MySQL 服务
systemctl stop mysqld

#设置开机自启
systemctl enable mysqld

# 查看状态
systemctl status mysqld

2.7.查看Mysql-8.0 临时密码

rpm 安装 MySQL 会自动生成一个随机密码

bash 复制代码
grep 'temporary password' /var/log/mysqld.log

cat /var/log/mysqld.log

A temporary password is generated for root@localhost: ij9uN+hmchw2

账号:root

随机密码:ij9uN+hmchw2

2.8.修改Mysql-8.0 密码

bash 复制代码
# 登录
mysql -u root -p
ij9uN+hmchw2

# 修改密码
ALTER  USER  'root'@'localhost'  IDENTIFIED BY '123456';

ALTER  USER  'root'@'localhost'  IDENTIFIED BY 'Mysql.123456';

# 设置密码的验证强度等级
#Mysql 8.0 版本
# 将密码复杂度校验调整简单类型
# set global validate_password.policy=MEDIUM;
set global validate_password.policy=LOW;
# 设置密码最少位数限制为 4 位
set global validate_password.length=4;
# 查看 mysql 初始的密码策略
SHOW VARIABLES LIKE 'validate_password%';
bash 复制代码
mysql> SHOW VARIABLES LIKE 'validate_password%';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

提示ERROR 1820,让我们先修改密码后才能查看规则。

bash 复制代码
# 查看 mysql8.0 初始的密码策略
mysql> ALTER  USER  'root'@'localhost'  IDENTIFIED BY 'Mysql.123456';
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW VARIABLES LIKE 'validate_password%';
+-------------------------------------------------+--------+
| Variable_name                                   | Value  |
+-------------------------------------------------+--------+
| validate_password.changed_characters_percentage | 0      |
| validate_password.check_user_name               | ON     |
| validate_password.dictionary_file               |        |
| validate_password.length                        | 8      |
| validate_password.mixed_case_count              | 1      |
| validate_password.number_count                  | 1      |
| validate_password.policy                        | MEDIUM |
| validate_password.special_char_count            | 1      |
+-------------------------------------------------+--------+
8 rows in set (0.01 sec)

2.9.远程连接navicat--遇见问题

2.9.1.问题:1130 - Host '192.168.229.1' is not allowed to connect to this MySQL server

自己安装在本地的mysql默认时不允许进行远程登陆,如果想要远程访问本地的数据库需要修改访问权限,将'root'访问权限修改为'%'

2.9.2.连接服务器--修改权限

bash 复制代码
# 连接服务器
mysql -u root -p

# 看当前所有数据库
show databases;

# 进入mysql数据库
use mysql;

# 查看mysql数据库中所有的表
show tables;

# 执行更新权限语句
# 其中"%"代表所有的地址,意味着所有的地址都可以访问"root"
update user set Host='%' where User='root';

# 查看权限是否修改成功
select host,user from user;

# 刷新服务器配置
FLUSH PRIVILEGES;

2.9.3.连接成功

2.10.MySQL 8.0 配置mysql_native_password身份验证插件的password

方法一:

bash 复制代码
# 登录MySQL后输入
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPassword';

FLUSH PRIVILEGES;

方法二:

bash 复制代码
# 编辑my.cnf文件,更改默认的身份认证插件。
vi /etc/my.cnf

# 在[mysqld]中添加下边的代码
default_authentication_plugin=mysql_native_password

# 然后重启MySQL
service mysqld restart

2.11.卸载Mysql-8.0

bash 复制代码
# 停止Mysql
systemctl stop mysqld

# 查询 MySQL 的安装文件
rpm -qa | grep -i mysql

# 卸载上述查询的所有安装包
rpm -e mysql-community-client-plugins-8.0.34-1.el7.x86_64 --nodeps
rpm -e mysql-community-libs-compat-8.0.34-1.el7.x86_64 --nodeps
rpm -e mysql-community-client-8.0.34-1.el7.x86_64 --nodeps
rpm -e mysql-community-server-8.0.34-1.el7.x86_64 --nodeps
rpm -e mysql-community-common-8.0.34-1.el7.x86_64 --nodeps
rpm -e mysql-community-libs-8.0.34-1.el7.x86_64 --nodeps
rpm -e mysql-community-devel-8.0.34-1.el7.x86_64 --nodeps
rpm -e mysql-community-icu-data-files-8.0.34-1.el7.x86_64 --nodeps


# 再次查询 MySQL 的安装文件
rpm -qa | grep -i mysql

# 找出服务器中分散的mysql文件夹,查询残留的文件和相关依赖,并删除
find / -name mysql 
rm -rf [显示的文件夹路径,每个绝对路径中间用空格隔开]

# MySQL配置文件通常位于/etc/my.cnf或/etc/mysql/my.cnf下,MySQL日志文件通常位于/var/log/mysql下
# 删除mysql日志
rm -rf /var/log/mysqld.log
# 删除MySQL的配置文件备份
rm -rf /etc/my.cnf.rpmsave
# 删除mysql配置文件
rm -rf /etc/mysql/my.cnf
rm -rf /etc/my.cnf

# 清除MySQL用户和组
userdel mysql
groupdel mysql


三、Linux下--yum源安装Mysql

3.1.进入Mysql官网获取yum源安装包【无需登录账号】

bash 复制代码
# linux8 yum源
https://dev.mysql.com/get/mysql80-community-release-el8-9.noarch.rpm

# linux7 yum源
https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm

# linux6 yum源
https://dev.mysql.com/get/mysql80-community-release-el6-10.noarch.rpm


3.2.通过yum搜索命令查询mysql相关的包数据

bash 复制代码
yum list | grep mysql*

yum list mysql*

3.3.确保系统中已经安装了wget和rpm工具

bash 复制代码
# 确保系统中已经安装了wget和rpm工具
sudo yum install wget rpm

3.4.安装Mysql的yum源【centos7为例】

bash 复制代码
# linux8 yum源
wget https://dev.mysql.com/get/mysql80-community-release-el8-9.noarch.rpm

# linux7 yum源
wget https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm

# linux6 yum源
wget https://dev.mysql.com/get/mysql80-community-release-el6-10.noarch.rpm

3.5.安装Mysql的yum源

bash 复制代码
cd /etc/yum.repos.d

rpm -ivh mysql80-community-release-el7-11.noarch.rpm

3.6.默认安装Mysql8.0,修改为Mysql5.7

bash 复制代码
vi mysql-community.repo


3.7.安装mysql-server

bash 复制代码
# 安装方式一
yum -y install mysql-community-server

# 安装方式二
# 安装MySQL服务器、MySQL客户端和MySQL依赖库
yum -y install mysql-server mysql mysql-libs

# 安装方式三
# 只想安装MySQL服务器
yum -y install mysql-server



3.8.启动mysql-server

bash 复制代码
# 查看Mysql版本信息
mysql --version

# 启动mysql
systemctl start mysqld

# 查看mysql状态信息
systemctl status mysqld


3.9.获取mysql-server初始登陆的密码

bash 复制代码
grep 'temporary password' /var/log/mysqld.log

cat /var/log/mysqld.log

3.10.其余同上

3.11.卸载mysql

bash 复制代码
# 停止MySQL服务
systemctl stop mysqld

# 查询 MySQL 的安装文件
rpm -qa | grep -i mysql

# 删除已安装mysql
yum -y remove mysql80-community-release-el7-11.noarch
yum -y remove mysql-community-common-5.7.44-1.el7.x86_64
yum -y remove mysql-community-libs-compat-5.7.44-1.el7.x86_64
yum -y remove mysql-community-server-5.7.44-1.el7.x86_64
yum -y remove mysql-community-libs-5.7.44-1.el7.x86_64
yum -y remove mysql-community-client-5.7.44-1.el7.x86_64


# 查询 MySQL 的安装文件
rpm -qa | grep -i mysql

# 查看mysql相关的文件目录,并删除
find / -name mysql 
rm -rf [显示的文件夹路径,每个绝对路径中间用空格隔开]

# MySQL配置文件通常位于/etc/my.cnf或/etc/mysql/my.cnf下,MySQL日志文件通常位于/var/log/mysql下
# 删除mysql日志
rm -rf /var/log/mysqld.log
# 删除MySQL的配置文件备份
rm -rf /etc/my.cnf.rpmsave
# 删除mysql配置文件
rm -rf /etc/mysql/my.cnf
rm -rf /etc/my.cnf

# 清除MySQL用户和组
userdel mysql
groupdel mysql

cd ~
find / -name mysql*

3.12.Mysql的yum源仓库

http://repo.mysql.com/

endl

相关推荐
Zfox_13 分钟前
【Linux】进程信号全攻略(二)
linux·运维·c语言·c++
安於宿命18 分钟前
【Linux】简易版shell
linux·运维·服务器
黄小耶@30 分钟前
linux常见命令
linux·运维·服务器
叫我龙翔31 分钟前
【计网】实现reactor反应堆模型 --- 框架搭建
linux·运维·网络
古驿幽情33 分钟前
CentOS AppStream 8 手动更新 yum源
linux·运维·centos·yum
BillKu34 分钟前
Linux(CentOS)安装 Nginx
linux·运维·nginx·centos
BillKu37 分钟前
Linux(CentOS)yum update -y 事故
linux·运维·centos
a2663789642 分钟前
解决yum命令报错“Could not resolve host: mirrorlist.centos.org
linux·运维·centos
weixin_449310841 小时前
高效集成:聚水潭采购数据同步到MySQL
android·数据库·mysql
CodingBrother1 小时前
MySQL 和 PostgreSQL 的使用案例
mysql·adb·postgresql