首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

【MySQL】Ubuntu 20.04 下安装 MySQL 5.7

  • 25-03-08 02:01
  • 2518
  • 13703
blog.csdn.net

文章目录

  • 安装环境说明
  • 安装过程
    • 1. 检查并卸载已有 MySQL
    • 2. 添加 MySQL 源
    • 3. 安装 MySQL
    • 4. 简单配置 MySQL
  • 参考

安装环境说明

  • 服务器:阿里云云服务器。
  • 操作系统版本: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"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 远程连接工具: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
  • 1
  • 2
  • 3
  • 4
  • 5

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

sudo systemctl stop mysql
sudo systemctl stop maridb
  • 1
  • 2

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

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

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

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

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

2. 添加 MySQL 源

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

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

添加如下内容

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
  • 1
  • 2
  • 3
  • 4

更新本地的 apt 包索引

sudo apt update
  • 1

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

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.
  • 1
  • 2
  • 3
  • 4

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

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

再次更新 apt 包索引

sudo apt update
  • 1

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

先后安装 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
  • 1
  • 2

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

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

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

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

首先是客户端

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

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

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

然后是服务端

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

添加如下内容

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

重启 MySQL 服务端

systemctl restart mysql.service 
  • 1

以上就是所有的安装过程

参考

  1. 亲测-ubuntu20.04 安装 MySQL5.7
  2. 在 Ubuntu 上安装 MySQL 5.7 20.04
注:本文转载自blog.csdn.net的ljh1257的文章"https://blog.csdn.net/ljh1257/article/details/139722256"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2491) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top