学习笔记-MySQL 5.7 部署及初始化设置 - Ubuntu

学习笔记-MySQL 5.7 部署及初始化设置 - Ubuntu

编码文章call10242025-01-20 9:55:3413A+A-

前言摘要

#    之前曾经写过 MySQL 部署及初始化设置 文档,但是那是基于 MySQL 8.x 版本编写的,原本考虑MySQL 5.7.X 已经停止支持了,可以不用了。但最近又有需求安装 MySQL 5.7 所以又单独开了一篇
#    本来考虑 CentOS 和 Ubuntu 下的部署文档合成一篇,但是Ubuntu步骤差异还是挺大的,再说最近办公网访问Ubuntu那边的资源一直有问题,所以会单独水一篇
#    最后,如果没有特殊需求,建议不要再使用这种厂家已经停止支持的数据库软件,风险太大。多用用国产吧,虽然风险也挺大的
#    以上内容是完全照抄之前写CentOS 环境部署 MySQL 5.7.X 的内容
#    实际上写这个是因为群里有人问在Ubuntu上部署MySQL 5.7 无法进行,甚至还给了我参考文档,里面介绍了三种方法
#    自己测试下来,发现确实有问题,无法正常安装,所以总结了最简单的可以安装的方法
#    另外,我知道,这篇只要发出去,肯定有人马上就回来一句,都什么年代了,还…………

基本环境

底层: Windows 11 x64
虚拟化层:VMWare Workstation 17.0.2
虚拟化兼容性:ESXi 6.7
虚拟硬件:
    CPU: 4 Core
    RAM: 16 GB
    HDD: 128 GB SCSI
    NetWork: NAT
操作系统版本: Ubuntu 20.04.6
                内核 6.6.0
                系统 更新 到最新
                配置 NTP 时间同步
                安装 Open VM Tools
                美化 CLI

信息汇总

#    安装包 官方网站 下载链接
https://downloads.mysql.com/archives/community/
#    MySQL 5.7.42 Ubuntu 版 压缩包 下载链接 (最后版本,5.7.44 没有 Ubuntu 专用版本)
https://downloads.mysql.com/archives/get/p/23/file/mysql-server_5.7.42-1ubuntu18.04_amd64.deb-bundle.tar

#    备用链接
#    #    MySQL 5.7.42 Ubuntu 版 下载链接 
https://cdn.mysql.com/archives/mysql-5.7/libmysqlclient20_5.7.42-1ubuntu18.04_amd64.deb
https://cdn.mysql.com/archives/mysql-5.7/libmysqlclient-dev_5.7.42-1ubuntu18.04_amd64.deb
https://cdn.mysql.com/archives/mysql-5.7/libmysqld-dev_5.7.42-1ubuntu18.04_amd64.deb
https://cdn.mysql.com/archives/mysql-5.7/mysql-client_5.7.42-1ubuntu18.04_amd64.deb
https://cdn.mysql.com/archives/mysql-5.7/mysql-common_5.7.42-1ubuntu18.04_amd64.deb
https://cdn.mysql.com/archives/mysql-5.7/mysql-community-client_5.7.42-1ubuntu18.04_amd64.deb
https://cdn.mysql.com/archives/mysql-5.7/mysql-community-server_5.7.42-1ubuntu18.04_amd64.deb
https://cdn.mysql.com/archives/mysql-5.7/mysql-community-source_5.7.42-1ubuntu18.04_amd64.deb
https://cdn.mysql.com/archives/mysql-5.7/mysql-community-test_5.7.42-1ubuntu18.04_amd64.deb
https://cdn.mysql.com/archives/mysql-5.7/mysql-server_5.7.42-1ubuntu18.04_amd64.deb
https://cdn.mysql.com/archives/mysql-5.7/mysql-server_5.7.42-1ubuntu18.04_amd64.deb-bundle.tar
https://cdn.mysql.com/archives/mysql-5.7/mysql-testsuite_5.7.42-1ubuntu18.04_amd64.deb
#    Ubuntu\Debian 系统 源文件 下载链接 (最新版已经没有 5.7.X 信息)
https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb

安装 MySQL 5.7.X

前期准备

#    创建 安装用临时文件夹
#    我不想频繁切目录,所以所有操作均带路径
mkdir -p ~/temp/install/
#    安装必要插件
apt install -y libaio1 libmecab2
#    下载 MySQL 5.7.42 x64 Ubuntu 专用版本
curl -o mysql-server_5.7.42-1ubuntu18.04_amd64.deb-bundle.tar https://cdn.mysql.com/archives/mysql-5.7/mysql-server_5.7.42-1ubuntu18.04_amd64.deb-bundle.tar
#    解压缩 MySQL 5.7 压缩包
tar -vxf ~/temp/install/mysql-server_5.7.42-1ubuntu18.04_amd64.deb-bundle.tar --directory ~/temp/install/

安装 MySQL 5.7.42

#    安装 MySQL 5.7.42 软件包
dpkg -i ~/temp/install/mysql-{common,community-client,community-source,client,community-server,server}_*.deb
#    提示 “输入密码”
Enter root password: 
#    提示 “再次输入密码” 
Re-enter root password:

使用初始化向导配置 MySQL 5.7.X

#    运行 数据库初始化向导
mysql_secure_installation

#    由于配置向导内容太多,看起来比较乱,因此详细内容单独展示,此处仅做基础提示

#    输入用户root的密码 (直接输入上面的密码)
Enter password for user root: 
#    设置VALIDATE PASSWORD插件 (选择 “Y”)
Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: 
#    设置密码安全级别 (建议选择 “2”)
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 
#    是否更改root用户的密码?    (选择 “N”,因为刚刚自己设置的密码)
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
#    是否删除匿名用户?    (选择“ Y ” 删除)
Remove anonymous users? (Press y|Y for Yes, any other key for No) :
#    是否禁止root远程登录?    (选择“ N ” 拒绝禁止)
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
#    是否删除测试数据库并访问它?    (选择“ Y ” 删除)
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
#    是否立即重新加载特权表?    (选择“ Y ” 立即重新加载权限列表)
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : 

配置向导详解

#    由于配置向导内容太多,看起来比较乱,因此详细内容单独展示

#    运行 数据库初始化向导
mysql_secure_installation

#    确保MySQL服务器部署的安全
Securing the MySQL server deployment.
#    输入用户root的密码
Enter password for user root: 

#    可用于测试密码并提高安全性。它检查密码的强度,并允许用户只设置那些足够安全的密码。你想设置VALIDATE PASSWORD插件吗?
VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin?
#    按y|Y表示“是”,按任意其他键表示“否”:
Press y|Y for Yes, any other key for No: 

#    密码验证策略有三个级别
There are three levels of password validation policy:
#    LOW长度>=8
LOW    Length >= 8
#    MEDIUM长度>=8,数字、大小写混合和特殊字符
MEDIUM Length >= 8, numeric, mixed case, and special characters
#    STRONG长度>=8,数字,大小写混合,特殊字符和字典文件
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
#    请输入0=LOW、1=MEDIUM和2=STRONG:
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 

#    密码的估计强度:100
Estimated strength of the password: 100 
#    是否更改root用户的密码?(按y|y表示“是”,按任意其他键表示“否”):
Change the password for root ? ((Press y|Y for Yes, any other key for No) :

#    默认情况下,MySQL安装有一个匿名用户,允许任何人登录MySQL,而无需为他们创建用户帐户。这只是为了测试,并使安装更加顺利
By default, a MySQL installation has an anonymous user,allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother.
#    您应该在移动到生产环境之前将其删除。
You should remove them before moving into a production environment.
#    是否删除匿名用户?(按y|y表示“是”,按任意其他键表示“否”):
Remove anonymous users? (Press y|Y for Yes, any other key for No) : 

#    通常,只应允许root从“localhost”进行连接。这样可以确保有人无法从网络中猜测根密码
Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
#    是否禁止root远程登录?(按y|y表示“是”,按任意其他键表示“否”):
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : 

#    默认情况下,MySQL附带一个名为“test”的数据库,任何人都可以访问。这也仅用于测试,在进入生产环境之前应将其删除。
By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
#    是否删除测试数据库并访问它?(按y|y表示“是”,按任意其他键表示“否”):
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : 

#    重新加载特权表将确保到目前为止所做的所有更改都将立即生效。
Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
#    是否立即重新加载特权表?(按y|y表示“是”,按任意其他键表示“否”):
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : 

管理 MySQL 服务

#    启动 MySQL
systemctl start mysql.service
#    检查运行状态
systemctl status mysql.service
#    设置MySQL为开机自启动
systemctl enable mysql.service
#    查看端口运行情况
netstat -nlpt
#    停止MySQL
systemctl stop mysqld
#    重启MySQL
systemctl restart mysqld

测试MySQL

#    进入MySQL Shell
sudo mysql -uroot -pP@ssw0rd
#    测试MySQL,刷新权限
flush privileges;
#    测试MySQL,列出数据库
show databases;
#    测试MySQL,查看用户
select user,host from mysql.user;
#    退出MySQL Shell
exit

防火墙放行 MySQL 服务

创建 MySQL 服务 端口配置文件

#    创建 MySQL 服务 端口配置文件
sudo nano /usr/lib/firewalld/services/mysql.xml
#    编辑 MySQL 服务 端口配置文件
#    {
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>MySQL</short>
  <description>MySQL for Database </description>
  <port protocol="tcp" port="3306"/>
</service>
#    }

#    方法2: 命令行直接创建 MySQL 服务 端口配置文件
sudo cat <<EOF > /usr/lib/firewalld/services/mysql.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>MySQL</short>
  <description>MySQL for Database </description>
  <port protocol="tcp" port="3306"/>
</service>
EOF

防火墙 放行 MySQL 服务

#    刷新 防火墙策略
firewall-cmd --reload
#    放行 MySQL 服务
firewall-cmd --permanent --zone=public --add-service=mysql
#    生效 防火墙策略
firewall-cmd --reloadfirewall-cmd --reload

备用

错误安装方法 示例

#    以下示例仅仅是个尝试,以前他是正确安装方法,现在因为MySQL官方停止支持,无法进行了而已

下载并运行 MySQL APT源编辑工具

#    创建 安装用临时文件夹
mkdir -p ~/temp/install/
#    下载 MySQL APT源编辑工具
curl -o ~/temp/install/mysql-apt-config_0.8.12-1_all.deb https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.de
#    运行 MySQL APT源编辑工具
dpkg -i ~/temp/install/mysql-apt-config_0.8.12-1_all.deb
#    在 菜单 中,选择 “ubuntu bionic”
#    在 菜单 中,选择 “MysQL Server & cluster (Currently selected: mysql-8.)”
#    在 菜单 中,选择 “mysgl-5.7”
#    回到上一级 菜单 中,选择 “OK” 退出 配置工具

添加 公钥 并 更新源

#    添加公钥
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 467B942D3A79BD29
#    更新 源
apt-get update
#    搜素 源 中的 MySQL
apt-cache policy mysql-server
#    失败,现在已经搜索不到 MySQL 5.7.X ,更换 其他第三方源也一样
点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4