如何在 Ubuntu(20.04)的 XAMPP 中将 MariaDB 更改为 MySQL?

如何在 Ubuntu(20.04)的 XAMPP 中将 MariaDB 更改为 MySQL?

有很多关于如何在 xampp 中将 mariadb 更改为 mysql 的“操作方法” (这里这里这里这里等),但它们都是适用于 Windows 操作系统

有人知道如何通过 bash 命令行在 Ubuntu 20.04 上执行此操作吗?(最好是 mysql 5.7)

wget https://www.apachefriends.org/xampp-files/7.4.12/xampp-linux-x64-7.4.12-0-installer.run
sudo chmod +x xampp-linux-x64-7.4.12-0-installer.run
sudo ./xampp-linux-x64-7.4.12-0-installer.run --mode unattended --launchapps 0
sudo /opt/lampp/manager-linux-x64.run

wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
tar -xzf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
sudo mv -f mysql-5.7.35-linux-glibc2.12-x86_64/* /opt/lampp/mysql

# and after that?

在此处输入图片描述

降级的解决方法:

没有带有 MySQL 5.7 的 Xampp 版本。根据阿帕奇之友,自 XAMPP 5.5.30 和 5.6.14 起,XAMPP 搭载的是 MariaDB 而不是 MySQL(根据发布说明,最后一个搭载 MySQL 的 Xampp 是 v5.6.12-0 -搭载 MySQL v5.6.25-;然后跳转到 Xampp v5.6.14 -搭载 MariaDB v10.0.17-)

RELEASE NOTES

[2015-10-19] XAMPP for Linux 64bit 5.6.14
This version of XAMPP includes:
    - Updated Apache to 2.4.17
    - Updated PHP to 5.6.14
    - Added MariaDB 10.0.17
    - Updated phpMyAdmin 4.5.0.2

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

[2015-08-27] XAMPP for Linux 64bit 5.6.12-0
This version of XAMPP includes:
    - Updated Apache to 2.4.12
    - Updated PHP to 5.6.11
    - Updated MySQL to 5.6.25
    - Updated phpMyAdmin 4.4.12
    - Update OpenSSL to 1.0.1p
    - Phanebook issue with SQLite
    - Fix charset admin pages
    - Added OS X HOW-TO guides
    - Enabled SSL support for PostgreSQL PHP extension
    - Added PHP XMLRPC extension
    - Added PHP MSSQL extension
    - Included new dashboard

所以我找到了一个名为比特纳米灯,并且在以前的版本有一个mysql 5.7并可以降级

在此处输入图片描述

但我仍然想知道我的问题的答案,是否可以在 Linux 中完成这样的事情

答案1

长话短说,我已成功从 MariaDB 10.4.11 迁移到 MySQL 5.7.38。这样做的原因是为了检查特定版本的 InnoDB 引擎,即 5.7.38。

实际上,使用 bitnami lampstack 相当容易。此外,使用 bitnami,您可以轻松配置运行 MariaDB 和 MySQL [1](同时在不同端口上运行或一次在同一个默认端口上运行一个),并通过管理器(或通过 systemd 服务)控制它们。

我首先尝试在 bitnami lampstack 包中找到最接近的 MySQL 版本变更日志并下载此软件包,但网络上缺少该软件包(高于 7.1.33 的版本(MySQL 5.7.28)和低于 7.2.24 的版本(切换到 MySQL 8))。

我的下一个最佳选择是抓住任何可用的 bitnami 包(7.2.26)并只交换已安装的 MySQL 包。(抱歉,这里没有 wget,因为链接已被计时器显示。如果您能找到它们的档案,请更新)

首先运行安装程序以获取完整的选项列表(选择要安装的模块):

./bitnami-lampstack-7.2.26-0-linux-x64-installer.run --help

并使用覆盖的设置进行安装(我更喜欢以用户身份安装,因此这里不需要 sudo):

./bitnami-lampstack-7.2.26-0-linux-x64-installer.run --prefix /opt/bitnami/lampstack-7.2.26.0 --disable-components varnish,zendframework,symfony,codeigniter,cakephp,smarty,laravel --base_password testpass --apache_server_port 9955 --apache_server_ssl_port 9943  --mysql_port 3309 --mode unattended

此时,您将获得一个可以在选定端口上运行服务的可运行的 LAMP 堆栈。遗憾的是,bitnami 没有禁用应用程序启动的标志,因此我们需要手动执行此操作:

$ cd /opt/bitnami/lampstack-7.2.26.0 && ./ctlscript.sh status  && ./ctlscript.sh stop
apache already running
mysql already running
Syntax OK
/opt/bitnami/lampstack-7.2.26.0/apache2/scripts/ctl.sh : httpd stopped
/opt/bitnami/lampstack-7.2.26.0/mysql/scripts/ctl.sh : mysql stopped

安装所选的 MySQL 版本

正如您所注意到的,lampstack 模块通过其“scripts”文件夹中的 ctl 脚本进行控制。因此,最重要的部分是将这些脚本放在 MySQL 的自定义安装中。

$ mv mysql mysql_original
$ mkdir mysql && mkdir mysql/{tmp,run,data}
$ cp -r mysql_original/scripts mysql/
$ cp mysql_original/my.cnf mysql ##it should be adapted to your previous configuration, but keep defined directory variables
$ wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
$ tar -xzf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz  -C mysql --strip-components 1 ##untar archive contents into mysql skipping root directory
$ ln -s /opt/bitnami/lampstack-7.2.26.0/mysql/bin/mysqld /opt/bitnami/lampstack-7.2.26.0/mysql/bin/mysqld.bin ##or fix mysql ctl.sh to use mysqld instead of mysqld.bin

现在您有两个选择:从旧的 MariaDB 安装复制“数据”或初始化新数据库然后导入转储。第一个不可靠,所以我更喜欢第二个。

选项 1 迁移 MariaDB“数据”

注意!备份“数据”文件夹和/或创建数据库转储(这样做会跳过选项 1 并跳转到选项 2)。

$ cp -R old_data/ /opt/bitnami/lampstack-7.2.26.0/mysql/
$ ./opt/bitnami/lampstack-7.2.26.0/ctlscript.sh start mysql

如果您遵循此选项并降级,则肯定会收到此错误。

 InnoDB: Unsupported redo log format. The redo log was created with MariaDB 10.2.44. Please follow the instructions at http://dev.mysql.com/doc/refman/5.7/en/upgrading-downgrading.html

注意!潜在的有害行为 一种解决方法是删除 ib_logfile* 文件,然后重新启动服务。

然后你将会遇到一堆错误,但数据库将启动并运行。

[ERROR] InnoDB: Your database may be corrupt or you may have copied the InnoDB tablespace but not the InnoDB log files. Please refer to http://dev.mysql.com/doc/refman/5.7/en/forcing-innodb-recovery.html for information about forcing recovery

[ERROR] InnoDB: Page [page id: space=0, page number=734] log sequence number 3261161573 is in the future! Current system log sequence number 3221898782.
[ERROR] InnoDB: Your database may be corrupt or you may have copied the InnoDB tablespace but not the InnoDB log files. Please refer to http://dev.mysql.com/doc/refman/5.7/en/forcing-innodb-recovery.html for information about forcing recovery.

之后,您需要通过调用 mysql_upgrade 来升级数据库。让我们为此创建 ctl_upgrade.sh 脚本:

$ vi mysql/scripts/ctl_upgrade.sh

#!/bin/sh
MSQLPATH=/opt/bitnami/lampstack-7.2.26.0/mysql
MYSQL_PIDFILE=$MSQLPATH/run/mysqld.pid

MYSQL_START="$MSQLPATH/bin/mysql_upgrade --defaults-file=$MSQLPATH/my.cnf --socket=$MSQLPATH/tmp/mysql.sock --verbose"

$MYSQL_START >$MSQLPATH/data/mysqld-update.log 2>&1

$ ./mysql/scripts/ctl_upgrade.sh
$ ./mysql/scripts/ctl.sh stop && ./mysql/scripts/ctl.sh start

选项 2 转储并导入数据库

我不会在这里深入讲解,因为这个过程相当简单。转储:

$ vi /opt/bitnami/mysqldump.sh

#!/bin/sh
## Dump DB with no DB name in two rows:
## 1. dump structure with routines and no data
## 2. dump only data in ansi mode
## I avoid dumping with auto creating database to avoid collisions with existing databases on import.
MYSQL_BIN=mysqldump ##note, here i use old mysql binary available globally
PASSWORD=
HOST=$1
USER=root
DATABASE=$2

IGNORED_TABLES_STRING='' #if you want to skip some tables from dumping

now="$(date +'%d_%m_%Y_%H_%M')"
filename="${DATABASE}_db_backup_$now.sql"
backupfolder="./mysqldump"
logfile="$backupfolder/"backup_log_"$(date +'%Y_%m')".txt

echo "Dump structure started at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
    $MYSQL_BIN --host=${HOST} --user=${USER} --password=${PASSWORD} --no-create-db ${DATABASE} --single-transaction --no-data --routines --log-error $logfile > $backupfolder/${DB_FILE} 
echo "Dump structure finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"

echo "Dumping contents"
##note dumped db will be in ansi mode, for better [compatibility][5] 
$MYSQL_BIN --compatible=ansi --host=${HOST} --user=${USER} --password=${PASSWORD} --no-create-db ${DATABASE} --no-create-info --skip-triggers ${IGNORED_TABLES_STRING} --log-error $logfile >> $backupfolder/${DB_FILE} 

$ cd /opt/bitnami && mkdir mysqldump && sh mysqldump.sh localhost db_to_dump
## stop old lampp services
$ ./opt/lampp/ctlscript.sh stop

导入到新数据库:

$ vi /opt/bitnami/mysqlrestore.sh

#!/bin/sh
## Note here we use lampstack MySQL binary (not added to PATH)
MYSQL_BIN="/opt/bitnami/lampstack-7.2.26.0/mysql/bin/mysql --socket=/opt/bitnami/lampstack-7.2.26.0/mysql/tmp/mysql.sock"

PASSWORD=
filename="${1}"
USER=root
DATABASE=$2

now="$(date +'%d_%m_%Y_%H_%M')"

echo "Creating DB $DATABASE"
$MYSQL_BIN --user=$USER --password=$PASSWORD -e "CREATE DATABASE $DATABASE;"

MYSQL_BIN -u $USER --password=$PASSWORD $DATABASE < $filename

$ cd /opt/bitnami/lampstack-7.2.26.0/mysql/scripts && cp ctl.sh ctl_init.sh && vi ctl_init.sh
change MYSQL_START to 
MYSQL_START="/opt/bitnami/lampstack-7.2.26.0/mysql/bin/mysqld --socket=/opt/bitnami/lampstack-7.2.26.0/mysql/tmp/mysql.sock  --datadir=/opt/bitnami/lampstack-7.2.26.0/mysql/data --log-error=/opt/bitnami/lampstack-7.2.26.0/mysql/tmp/mysqld_init.log  --pid-file=$MYSQL_PIDFILE --lower-case-table-names=1 --user=mysql --initialize"
 NB! if you need root without password use --initialize-insecure

$ ./ctl_init.sh start
$ ./ctl.sh start
$ cd /opt/bitnami && ./mysqlrestore.sh mysqldump/dumped_file.sql db_new_name

检查“数据”目录中的日志。迁移已完成。

可选:将 MariaDB 安装到 lampstack 管理器中

我选择了与 InnoDB 版本相同的 MariaDB。

 $ cd /opt && mkdir mariadb && mkdir mariadb/10.2.44-systemd 
 $ wget https://archive.mariadb.org/mariadb-10.2.44/bintar-linux-systemd-x86_64/mariadb-10.2.44-linux-systemd-x86_64.tar.gz
 $ tar -xzf mariadb-10.2.44-linux-systemd-x86_64.tar.gz -C 10.2.44-systemd --strip-components 1 
 $ cd 10.2.44-systemd && mkdir {tmp,run,data}
 $ cp /opt/bitnami/lampstack-7.2.26.0/mysql/scripts scripts
 $ cp /opt/bitnami/lampstack-7.2.26.0/mysql/my.cnf my.cnf
 $ ln -s /opt/mariadb/10.2.44-systemd /opt/bitnami/lampstack-7.2.26.0/mariadb

 At this point bitnami is aware that mariab module is installed, yet we need to update paths in copied ctl scripts to point to mariadb.

 $ cd scripts
 $ grep -rlZ "/opt/bitnami/lampstack-7.2.26.0/mysql" . ../my.cnf | xargs -0 sed -i 's/\/opt\/bitnami\/lampstack-7.2.26.0\/mysql/\/opt\/mariadb\/10.2.44-systemd/g'
 
 However, PhpMyAdmin will fail to connect, because in conf it points to mysql socket. Workaround is to create mariadb socket into mysql dir.

 $ vi ctl.sh
  In start_mysql() function in success clause (ERR -eq 0) add 
  ln -s/opt/mariadb/10.2.44-systemd/tmp/mysql.sock /opt/bitnami/lampstack-7.2.26.0/mysql/tmp/mysql.sock

  And in stop_mysql() in (RUNNING -eq 0) clause put
  rm /opt/bitnami/lampstack-7.2.26.0/mysql/tmp/mysql.sock

 Now you can run both MariaDB and MySQL and easily control them via manager.

可选:lampstack 的 Systemd 服务

$ sudo vi /etc/systemd/system/xampp.target
[Unit]
Description=Lampp target 
Requires=multi-user.target
After=multi-user.target
AllowIsolate=yes

$ sudo vi /etc/systemd/system/[email protected]
[Unit]
Description=Start Xampp module: %I
PartOf=xampp.target

[Service]
Type=simple
User=your_username
ExecStart=/opt/bin/xampp_ctl.sh start %i
ExecStop=/opt/bin/xampp_ctl.sh stop %i 
Restart=on-failure
RemainAfterExit=yes

[Install]
WantedBy=xampp.target

$ daemon-reload
$ systemctl enable xampp@apache  ## for service name after @ use ctlscript tags (check by calling ctlscript.sh help)
$ systemctl enable xampp@mariadb ## to start mariadb by default
$ systemctl enable xampp.target
$ systemctl disable xampp.service ## old xampp service
$ ln -s /opt/bitnami/lampstack-7.2.26.0/ctlscript.sh /opt/bin/xampp_ctl.sh
$ systemctl start xampp.target

[1]:我相信使用 xampp linux 包是可以实现的,但乍一看我发现它的配置很混乱,因为所有模块都是有点连接在一起的。

相关内容