Homestead -- Ubuntu20.04卸载mysql8后安装mysql5.7

Ubuntu 20.04 LTS 默认源是mysql8, 项目需要,要降级为Mysql5.7.记录一下

PS: 以下记录是给我自己看的,如果你刚好有这样的问题,自己看下思路即可, 如果你对linux完全不熟悉的话,不要安全照搬的去复制命令,那样是走不通的

第一步: 换源

1. 下载mysql-apt-config_0.8.15-1_all.deb

wget http://repo.mysql.com/mysql-apt-config_0.8.15-1_all.deb

2. 使用dpkg -i 安装mysql-apt-config_0.8.15-1_all.deb, 会弹出一个交互式的ncurses窗口

dpkg -i mysql-apt-config_0.8.15-1_all.deb

要先选择 Ubuntu bionic版本

选择第一个MySQL Server & Cluste, 切换为mysql5.7


按上面的操作,基本要按两次OK

3. 使用cat /etc/apt/sources.list.d/mysql.list查看生成的配置源, 如下

vagrant@homestead:~$ cat /etc/apt/sources.list.d/mysql.list

### THIS FILE IS AUTOMATICALLY CONFIGURED ###
# You may comment out entries below, but any other modifications may be lost.
# Use command 'dpkg-reconfigure mysql-apt-config' as root for modifications.
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 http://repo.mysql.com/apt/ubuntu/ bionic mysql-tools-preview
deb-src http://repo.mysql.com/apt/ubuntu/ bionic mysql-5.7

上面的源已经换成了bionic版本的, 因为Ubuntu focal默认没有mysql5.7的源,所以要换源,另外这两个版本的mysql包都是兼容的.所以Ubuntu20.04能安装上ubuntu18上的mysql5.7

4. 使用apt-get update 更新

sudo apt-get update

第二步: 安装Mysql5.7

1. 尝试apt-get install mysql-server安装, 这时候还是指向mysql8的,这是对的,因为还没配置

vagrant@homestead:~$ sudo apt-get install mysql-server

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libhtml-template-perl libmecab2 mecab-ipadic mecab-ipadic-utf8 mecab-utils **mysql-client-8.0 mysql-client-core-8.0
  mysql-server-8.0 mysql-server-core-8.0**
Suggested packages:
  libipc-sharedcache-perl mailx tinyca
The following NEW packages will be installed:
  libhtml-template-perl libmecab2 mecab-ipadic mecab-ipadic-utf8 mecab-utils **mysql-client-8.0 mysql-client-core-8.0
  mysql-server mysql-server-8.0 mysql-server-core-8.0**

2. 配置apt的指向

我们需要让apt知道自己要指向mysql5.7. 编辑/etc/apt/preferences.d/mysql内容如下:

vagrant@homestead:~$  cat /etc/apt/preferences.d/mysql
Package: *
Pin: origin repo.mysql.com
Pin-Priority: 900

Package: *
Pin: origin cn.archive.ubuntu.com
Pin-Priority: 700

Package: *
Pin: origin security.ubuntu.com
Pin-Priority: 800

按上面这样的配置, 最高的源优先级来自己于repo.mysql.com, 第二优先级Ubuntu Security repo,最后才是Ubuntu local的源

3. 需要再次sudo apt-get update , 然后执行sudo apt-get install mysql-server

vagrant@homestead:~$ sudo apt-get install mysql-server

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libmecab2 libtinfo5 **mysql-client mysql-community-client mysql-community-server**
The following NEW packages will be installed:
  libmecab2 libtinfo5 **mysql-client mysql-community-client mysql-community-server mysql-server**
0 upgraded, 6 newly installed, 0 to remove and 23 not upgraded.
Need to get 51.2 MB of archives.
After this operation, 315 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y

第三步: 解决dpkg returned an error code (1)错误

按上面的安装后,会报如下错误,

 Do you want to continue? [Y/n] Y
Get:1 http://ch.archive.ubuntu.com/ubuntu focal/universe amd64 libtinfo5 amd64 6.2-0ubuntu2 [83.0 kB]
[...]
Setting up mysql-community-server (5.7.31-1ubuntu18.04) ...
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service -> /lib/systemd/system/mysql.service.
**Job for mysql.service failed because the control process exited with error code.**
See "systemctl status mysql.service" and "journalctl -xe" for details.
invoke-rc.d: initscript mysql, action "start" failed.
 mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: activating (auto-restart) (Result: exit-code) since Mon 2020-08-31 13:09:33 CEST; 4ms ago
    Process: 24415 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
    Process: 24505 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid (code=exited, status=1/FAILURE)
dpkg: error processing package mysql-community-server (--configure):
 installed mysql-community-server package post-installation script subprocess returned error exit status 1
dpkg: dependency problems prevent configuration of mysql-server:
 mysql-server depends on mysql-community-server (= 5.7.31-1ubuntu18.04); however:
  Package mysql-community-server is not configured yet.

**dpkg: error processing package mysql-server (--configure):
 dependency problems - leaving unconfigured**
Processing triggers for systemd (245.4-4ubuntu3.2) ...
No apport report written because the error message indicates its a followup error from a previous failure.
Processing triggers for libc-bin (2.31-0ubuntu9) ...
**Errors were encountered while processing:
 mysql-community-server
 mysql-server
E: Sub-process /usr/bin/dpkg returned an error code (1)**

上面这样的错误很难定位,我在这里折腾了很久. 这时先通过dpkg -l|grep mysql看下已安装的mysql包

dpkg -l|grep mysql

**ii  libmysqlclient21:amd64               8.0.21-0ubuntu0.20.04.4           amd64        MySQL database client library**
ii  mysql-apt-config                     0.8.15-1                          all          Auto configuration for MySQL APT Repo.
ii  mysql-client                         5.7.31-1ubuntu18.04               amd64        MySQL Client meta package depending on latest version
**ii  mysql-common                         5.8+1.0.5ubuntu2                  all          MySQL database common files, e.g. /etc/mysql/my.cnf**
ii  mysql-community-client               5.7.31-1ubuntu18.04               amd64        MySQL Client
iF  mysql-community-server               5.7.31-1ubuntu18.04               amd64        MySQL Server
iU  mysql-server

这里可以看出libmysqlclientmysql-common两个包还是nysql8的包, 先通过apt-cache search libmysqlclient看下libmysqlclient

apt-cache search libmysqlclient

default-libmysqlclient-dev - MySQL database development files (metapackage)
libmysqlclient21 - MySQL database client library
libcrypt-mysql-perl - Perl module to emulate the MySQL PASSWORD() function
libglpk40 - linear programming kit with integer (MIP) support
librust-mysqlclient-sys-dev - Auto-generated rust bindings for libmysqlclient - Rust source code
libmysqlclient-dev - MySQL development headers
libmysqlclient20 - MySQL shared client libraries
libmysqlclient20-dbgsym - debug symbols for libmysqlclient20

这里有两个不同的libmysqlclient包, libmysqlclient21 和 libmysqlclient20 看下这两个的具体版本

apt-cache show libmysqlclient20 | grep Version
Version: 5.7.31-1ubuntu18.04

apt-cache show libmysqlclient21 | grep Version
Version: 8.0.21-0ubuntu0.20.04.4
Version: 8.0.19-0ubuntu5

我们应该要安装libmysqlclient20的, 但是默认安装了libmysqlclient21,所以还需要再次修改下apt的源指向

第四步: 再次修改apt源指向/etc/apt/preferences.d/mysql

我们之前已经将Mysql的官方源设置为最高优先级了,但是mysql-common 和 libmysqlclient包还是从Ubuntu的源上安装了, 那我们暴力点,直接告诉apt不要安装Ubuntu上的任何mysql包. 最后修改的/etc/apt/preferences.d/mysql如下:

cat /etc/apt/preferences.d/mysql

**Package: *
Pin: origin repo.mysql.com
Pin-Priority: 900

Package: libmysqlclient* mysql*
Pin: origin ch.archive.ubuntu.com
Pin-Priority: -1

Package: libmysqlclient* mysql*
Pin: origin security.ubuntu.com
Pin-Priority: -1**

以上的配置可以参考: manpages.debian.org/buster/apt/apt...

2. 再次安装前,移除下之前安装的mysql包

apt-get remove mysql-client mysql-common mysql-community-client mysql-community-server mysql-server

apt-get purge mysql-client mysql-common mysql-community-client mysql-community-server mysql-server

3. 用新的apt配置再来安装一遍

apt-get update
apt-get install mysql-server

现在已经成功安装了mysql5.7了

vagrant@homestead:~$ systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-06-28 11:54:25 CDT; 8h ago
   Main PID: 1044 (mysqld)
      Tasks: 27 (limit: 2265)
     Memory: 204.2M
     CGroup: /system.slice/mysql.service
             └─1044 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Jun 28 11:54:20 homestead systemd[1]: Starting MySQL Community Server...
Jun 28 11:54:25 homestead systemd[1]: Started MySQL Community Server.

总结

  1. 在Ubuntu20.04上安装mysql5.7. 要避免Ubuntu自己的mysql源对mysql5.7 安装的影响
  2. 以上安装完还没具体使用过,因为没有安装libmysqlclient21, 但是安装以后安装别的包可能会依赖这个包,比如说安装libsnmp35,这时候应该会安装不成功的, 那可以自行修改/etc/apt/preferences.d/mysql, 比如把里面配置的libmysqlclient* 去掉试试
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!