【MySQL】Ubuntu 20.04 下安装 MySQL 5.7

安装环境说明

  • 服务器:阿里云云服务器。
  • 操作系统版本:Ubuntu 20.04.6 LTS。
root@iZ7xvhuhaslv4h5dm1d1svZ:~# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.6 LTS"
  • 远程连接工具:Xshell7。

安装过程

1. 检查并卸载已有 MySQL

查看是否有正在运行的 MySQL 服务进程 或者 maridb 服务进程

root@iZ7xvhuhaslv4h5dm1d1svZ:~# ps axj | grep mysql # 有正在运行的
      1    7069    7068    7068 ?             -1 Sl     108   0:01 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
    941    7223    7222     941 pts/0       7222 S+       0   0:00 grep --color=auto mysql
root@iZ7xvhuhaslv4h5dm1d1svZ:~# ps axj | grep maridb # 没有正在运行的
    941    7225    7224     941 pts/0       7224 S+       0   0:00 grep --color=auto maridb

如果有就关闭正在运行的MySQL服务进程,并继续下面的卸载工作,没有则跳到第二步的安装过程

sudo systemctl stop mysql
sudo systemctl stop maridb

然后开始卸载 MySQL
卸载第一步:使用 apt-get 移除 MySQL 软件包

sudo apt-get remove --purge mysql-server mysql-client mysql-common

在跳出来的页面中选择 Yes
在这里插入图片描述

卸载第二步:删除残留的配置文件

sudo rm -rf /etc/mysql # MySQL 的配置文件所在目录
sudo rm -rf /var/lib/mysql # MySQL 的数据存储目录

2. 添加 MySQL 源

打开 /etc/apt/sources.list.d/mysql.list

sudo vim /etc/apt/sources.list.d/mysql.list

添加如下内容

deb http://repo.mysql.com/apt/ubuntu/ bionic mysql-apt-config
deb http://repo.mysql.com/apt/ubuntu/ bionic mysql-5.7
deb http://repo.mysql.com/apt/ubuntu/ bionic mysql-tools
deb-src http://repo.mysql.com/apt/ubuntu/ bionic mysql-5.7

更新本地的 apt 包索引

sudo apt update

更新过程中可能会出现如下错误

W: GPG error: http://repo.mysql.com/apt/ubuntu bionic InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY B7B3B788A8D3785C
E: The repository 'http://repo.mysql.com/apt/ubuntu bionic InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

这是由于缺少MySQL APT存储库的公钥导致的,运行以下命令来添加公钥

wget https://repo.mysql.com/RPM-GPG-KEY-mysql-2023
sudo apt-key add RPM-GPG-KEY-mysql-2023

再次更新 apt 包索引

sudo apt update

3. 安装 MySQL

查看系统支持的 MySQL 版本,此处可以看到已经有 5.7.42-1ubuntu18.04

root@iZ7xvhuhaslv4h5dm1d1svZ:~# sudo apt-cache policy mysql-server
mysql-server:
  Installed: (none)
  Candidate: 8.0.37-0ubuntu0.20.04.3
  Version table:
     8.0.37-0ubuntu0.20.04.3 500
        500 http://mirrors.cloud.aliyuncs.com/ubuntu focal-updates/main amd64 Packages
        500 http://mirrors.cloud.aliyuncs.com/ubuntu focal-updates/main i386 Packages
        500 http://mirrors.cloud.aliyuncs.com/ubuntu focal-security/main amd64 Packages
        500 http://mirrors.cloud.aliyuncs.com/ubuntu focal-security/main i386 Packages
     8.0.19-0ubuntu5 500
        500 http://mirrors.cloud.aliyuncs.com/ubuntu focal/main amd64 Packages
        500 http://mirrors.cloud.aliyuncs.com/ubuntu focal/main i386 Packages
     5.7.42-1ubuntu18.04 500
        500 http://repo.mysql.com/apt/ubuntu bionic/mysql-5.7 amd64 Packages

先后安装 MySQL 5.7 版本客户端和服务端,版本信息根据自己的输出自行修改。

sudo apt install -y mysql-client=5.7.42-1ubuntu18.04
sudo apt install -y mysql-server=5.7.42-1ubuntu18.04

server 端安装过程中会自动出现如下界面,可凭喜好是否设定root用户密码
在这里插入图片描述

安装好了之后使用 which 查看说明安装完成

root@iZ7xvhuhaslv4h5dm1d1svZ:~# which mysqld # 查看服务端
/usr/sbin/mysqld
root@iZ7xvhuhaslv4h5dm1d1svZ:~# which mysql # 查看客户端
/usr/bin/mysql

4. 简单配置 MySQL

/etc/mysql 是 MySQL 相关配置文件所在目录,其中 conf.d 是客户端配置文件所在目录,mysql.conf.d 是服务端配置文件所在目录,MySQL 5.7 并没有显式将配置文件分开,而是 Ubuntu 20.04 的安装过程中,将 MySQL 的配置文件分为服务端和客户端部分。

root@iZ7xvhuhaslv4h5dm1d1svZ:~# cd /etc/mysql/
root@iZ7xvhuhaslv4h5dm1d1svZ:/etc/mysql# ll mysql.conf.d/ conf.d/
conf.d/:
total 16
drwxr-xr-x 2 root root 4096 May  8 11:14 ./
drwxr-xr-x 4 root root 4096 Jun 16 20:46 ../
-rw-r--r-- 1 root root    8 Aug  3  2016 mysql.cnf
-rw-r--r-- 1 root root   55 Aug  3  2016 mysqldump.cnf

mysql.conf.d/:
total 12
drwxr-xr-x 2 root root 4096 Jun 16 20:46 ./
drwxr-xr-x 4 root root 4096 Jun 16 20:46 ../
-rw-r--r-- 1 root root 1587 Mar 16  2023 mysqld.cnf

首先是客户端

sudo vim /etc/mysql/conf.d/mysql.cnf

添加如下内容,主要是为了解决汉字兼容问题

# 设置mysql客户端默认字符集
default-character-set=utf8

然后是服务端

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

添加如下内容

#设置3306端口
port=3306
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=innodb

重启 MySQL 服务端

systemctl restart mysql.service 

以上就是所有的安装过程

参考

  1. 亲测-ubuntu20.04 安装 MySQL5.7
  2. 在 Ubuntu 上安装 MySQL 5.7 20.04
  • 25
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值