亲测-ubuntu20.04 安装 MySQL5.7

背景介绍

ubuntu 20.04 版本系统自带的 MySQL 版本是 8.0,普通方法很难安装 5.7 版本的。由于 8.0 版本较 5.7 版本做了不少改动,笔者比较习惯使用 5.7 版本。
网上搜做了一圈,跟着各种教程试了很多遍,最后终于找到了成功的方法。过程记录分享出来,供大家参考。

安装过程

笔者的环境:

root@hz192-168-1.55:/home# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.4 LTS"
  1. 若已经安装了 8.0 版本的 MySQL,请先操作删除。

  2. 添加 MySQL 源

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

更新命令

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 467B942D3A79BD29
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.

执行如下命令即可,467B942D3A79BD29根据实际情况替换。

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 76F1A20FF987672F

接着再重新执行更新,

sudo apt update
  1. 安装 MySQL

查看支持的 MySQL 版本,此处我们已经可以看到有 5.7.40-1ubuntu18.04,接近成功了。

root@hz192-168-1-55:/home# sudo apt-cache policy mysql-server
mysql-server:
  Installed: (none)
  Candidate: 8.0.30-0ubuntu0.20.04.2
  Version table:
     8.0.30-0ubuntu0.20.04.2 500
        500 http://mirrors.aliyun.com/ubuntu focal-updates/main amd64 Packages
        500 http://mirrors.aliyun.com/ubuntu focal-security/main amd64 Packages
     8.0.19-0ubuntu5 500
        500 http://mirrors.aliyun.com/ubuntu focal/main amd64 Packages
     5.7.40-1ubuntu18.04 500
        500 http://repo.mysql.com/apt/ubuntu bionic/mysql-5.7 amd64 Packages

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

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

如无需安装 mysql-client 服务,执行安装 mysql-server 服务,可能会报如下错误。
只需要手动安装 mysql-community-server 即可。sudo apt install mysql-community-server=5.7.40-1ubuntu18.04
The following packages have unmet dependencies:
mysql-server : Depends: mysql-community-server (= 5.7.40-1ubuntu18.04) but it is not going to be installed

  1. 验证
root@hz192-168-1-55:/home# mysql --version
mysql  Ver 14.14 Distrib 5.7.40, for Linux (x86_64) using  EditLine wrapper

ok, 完结。踩坑君再次填好一个坑,接着去踩下一个啦。

注意事项

1)若数据库需要外网访问,需要修改mysql配置文件,注释掉以下:

vi /etc/mysql/mysql.conf.d/mysqld.cnf

# bind-address = 127.0.0.1

2)其他服务器访问 mysql 时,可能会报错:message from server: “Host is not allowed to connect to this MySQL server“ 问题的解决办法

数据库安装完成后,默认是不能远程登陆的,只能在本地用localhost 或者127.0.0.1登录访问,如果需要远程登录,则需要修改mysql设置,具体修改方式:

 

【1】本地登录数据库 mysql
[root@localhost ~]$ mysql -u root -p123456

【2】查看数据库中mysql表中的权限设置:
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> select host, user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
3 rows in set (0.00 sec)

【3】修改root用户权限:
mysql> update user set host = ‘%’ where user = ‘root’;

【4】此时,root用户的权限已经允许非localhost登录:
mysql> select host, user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
+-----------+---------------+
3 rows in set (0.00 sec)

【5】最后,刷新设置,即可远程登录:
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

 

然后在另一台服务器连接远程 mysql 数据库服务器即可!

 

参考链接地址:

https://www.cnblogs.com/immaxfang/p/16804455.html

https://blog.csdn.net/weixin_44543463/article/details/113825319

https://www.cnblogs.com/wangchuanxinshi/p/16588732.html

https://blog.csdn.net/Julia_up/article/details/126776244

 

posted @ 2023-06-29 17:57  菜鸟的奋斗之路  阅读(1603)  评论(0编辑  收藏  举报