#!/bin/bash
# 自动安装 MariaDB 二进制版,并配置为与 MySQL 共存
MARIADB_VERSION="11.4.2"
INSTALL_DIR="/opt/mariadb"
DATA_DIR="$INSTALL_DIR/data"
CONF_DIR="/etc/my.cnf.d"
SERVICE_FILE="/etc/systemd/system/mariadb-custom.service"
MARIADB_USER="mariadb"
PKG_NAME="mariadb-${MARIADB_VERSION}-linux-systemd-x86_64"
PKG_FILE="${PKG_NAME}.tar.gz"
set -e
echo "==> 下载并解压 MariaDB..."
cd /opt
if [ ! -f "$PKG_FILE" ]; then
curl -LO "https://archive.mariadb.org/mariadb-${MARIADB_VERSION}/bintar-linux-systemd-x86_64/${PKG_FILE}"
fi
# 检查文件大小(至少 50MB,否则下载失败)
FILE_SIZE=$(stat -c%s "$PKG_FILE")
if [ "$FILE_SIZE" -lt 50000000 ]; then
echo "下载失败:文件过小 ($FILE_SIZE 字节),请检查 URL"
exit 1
fi
tar -xzf "$PKG_FILE"
mv "$PKG_NAME" "$INSTALL_DIR"
echo "==> 创建用户和数据目录..."
groupadd -r $MARIADB_USER || true
useradd -r -g $MARIADB_USER -s /bin/false $MARIADB_USER || true
mkdir -p $DATA_DIR
chown -R $MARIADB_USER:$MARIADB_USER $INSTALL_DIR
echo "==> 写入配置文件..."
mkdir -p $CONF_DIR
cat > $CONF_DIR/mariadb-custom.cnf <<EOF
[mysqld]
basedir=$INSTALL_DIR
datadir=$DATA_DIR
port=3307
socket=$INSTALL_DIR/mariadb.sock
pid-file=$INSTALL_DIR/mariadb.pid
log-error=$INSTALL_DIR/mariadb.err
user=$MARIADB_USER
[client]
port=3307
socket=$INSTALL_DIR/mariadb.sock
EOF
echo "==> 初始化数据库..."
$INSTALL_DIR/scripts/mariadb-install-db \
--user=$MARIADB_USER \
--basedir=$INSTALL_DIR \
--datadir=$DATA_DIR \
--defaults-file=$CONF_DIR/mariadb-custom.cnf
echo "==> 写入 systemd 服务..."
cat > $SERVICE_FILE <<EOF
[Unit]
Description=MariaDB database server (Custom Instance)
After=network.target
[Service]
Type=simple
User=$MARIADB_USER
Group=$MARIADB_USER
ExecStart=$INSTALL_DIR/bin/mariadbd --defaults-file=$CONF_DIR/mariadb-custom.cnf
ExecStop=$INSTALL_DIR/bin/mysqladmin --defaults-file=$CONF_DIR/mariadb-custom.cnf shutdown
Restart=always
RestartSec=5s
LimitNOFILE=5000
[Install]
WantedBy=multi-user.target
EOF
echo "==> 启动 MariaDB 服务..."
systemctl daemon-reexec
systemctl enable mariadb-custom
systemctl start mariadb-custom
echo "==> 安装完成!"
echo "用以下命令连接 MariaDB:"
echo "$INSTALL_DIR/bin/mariadb -u root -S $INSTALL_DIR/mariadb.sock"
MariaDB 与 MySQL 共存安装指南(PDF 文档)
一、概述
本文档记录了在 Linux 系统上安装 MariaDB 二进制版,与已安装的 MySQL 服务共存的完整流程,包括初始化数据库、配置 systemd 服务、创建独立用户和客户端连接设置。
二、环境说明
-
系统:Debian/Ubuntu
-
已安装 MySQL(默认端口 3306)
-
目标 MariaDB 版本:11.4.2
-
MariaDB 安装目录:/opt/mariadb
-
MariaDB 数据目录:/opt/mariadb/data
-
MariaDB 服务名:mariadb-custom
-
MariaDB 端口:3307
三、安装 MariaDB 二进制包
cd /opt
curl -LO "https://archive.mariadb.org/mariadb-11.4.2/bintar-linux-systemd-x86_64/mariadb-11.4.2-linux-systemd-x86_64.tar.gz"
tar -xzf mariadb-11.4.2-linux-systemd-x86_64.tar.gz
mv mariadb-11.4.2-linux-systemd-x86_64 /opt/mariadb
四、创建用户和数据目录
groupadd -r mariadb || true
useradd -r -g mariadb -s /bin/false mariadb || true
mkdir -p /opt/mariadb/data
chown -R mariadb:mariadb /opt/mariadb
chmod -R 750 /opt/mariadb
五、配置 MariaDB
自定义配置文件 /etc/my.cnf.d/mariadb-custom.cnf
[mysqld]
basedir=/opt/mariadb
datadir=/opt/mariadb/data
port=3307
socket=/opt/mariadb/mariadb.sock
pid-file=/opt/mariadb/mariadb.pid
log-error=/opt/mariadb/mariadb.err
user=mariadb
[client]
port=3307
socket=/opt/mariadb/mariadb.sock
六、初始化数据库
sudo -u mariadb /opt/mariadb/scripts/mariadb-install-db \
--user=mariadb \
--basedir=/opt/mariadb \
--datadir=/opt/mariadb/data \
--defaults-file=/etc/my.cnf.d/mariadb-custom.cnf
七、配置 systemd 服务
服务文件 /etc/systemd/system/mariadb-custom.service
[Unit]
Description=MariaDB database server (Custom Instance)
After=network.target
[Service]
Type=simple
User=mariadb
Group=mariadb
ExecStart=/opt/mariadb/bin/mariadbd --defaults-file=/etc/my.cnf.d/mariadb-custom.cnf
ExecStop=/opt/mariadb/bin/mysqladmin --defaults-file=/etc/my.cnf.d/mariadb-custom.cnf shutdown
Restart=always
RestartSec=5s
LimitNOFILE=5000
[Install]
WantedBy=multi-user.target
八、启动与测试
sudo systemctl daemon-reexec
sudo systemctl enable mariadb-custom
sudo systemctl start mariadb-custom
sudo systemctl status mariadb-custom
/opt/mariadb/bin/mariadb -u root -S /opt/mariadb/mariadb.sock
九、创建数据库用户
FLUSH PRIVILEGES;
CREATE USER 'sspanel'@'%' IDENTIFIED BY 'zdeeshim180';
GRANT ALL PRIVILEGES ON *.* TO 'sspanel'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
十、客户端连接
# Socket 连接
/opt/mariadb/bin/mariadb -u root -S /opt/mariadb/mariadb.sock
# TCP/IP 连接
/opt/mariadb/bin/mariadb -u sspanel -p -P 3307 -h 127.0.0.1
十一、停止 MariaDB
sudo systemctl stop mariadb-custom
# 或者
sudo /opt/mariadb/bin/mysqladmin -u root -S /opt/mariadb/mariadb.sock shutdown
十二、注意事项
-
确保
/opt/mariadb/data权限正确(mariadb:mariadb)。 -
如果缺少
libncurses.so.5,安装依赖:sudo apt install libncurses5
-
MySQL 继续使用端口 3306,MariaDB 使用 3307,互不干扰。