Centos7下使用MariaDB官方yum源安装MariaDB多实例

1.自制yum源

vim /etc/yum.repo.d/mariadb10.2.repo
[mariadb] 
name      =  MariaDB 
baseurl  =  https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/10.2/centos7-amd64/ 
gpgkey    = https://yum.mariadb.org/RPM-GPG-KEY-MariaDB 
gpgcheck = 1

2.安装MariaDB

yum install MariaDB-server -y

3.创建文件夹,将数据库文件分开

mkdir -pv /usr/local/mysql/{3306,3307,3308}/{data,etc,socket,bin,log,pid}

4.分别为3个数据库实例创建初始化数据

mysql_install_db --user=mysql --datadir=/usr/local/mysql/3306/data
mysql_install_db --user=mysql --datadir=/usr/local/mysql/3307/data
mysql_install_db --user=mysql --datadir=/usr/local/mysql/3308/data

5.复制初始化文件,并修改

cp /usr/share/mysql/my-huge.cnf /usr/local/mysql/3306/etc/my.cnf

vim /usr/local/mysql/3306/etc/my.cnf

[client]
port            = 3306
socket          = /usr/local/mysql/3306/mysql.sock

[mysqld]
port            = 3306
datadir         =//usr/local/mysql/3306/data/
socket          = /usr/local/mysql/3306/socket/mysql.sock
log-error       =/usr/local/mysql/3306/log/mariadb.log
pid-file        =/usr/local/mysql/3306/mariadb.pin

6.编写服务启动脚本

#!/bin/bash
#chkconfig: 345 80 2 
port=3306
mysql_user="root" 
mysql_basedir="/usr/local/mysql" 
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"

function_start_mysql()
{
    if [ ! -e "$mysql_sock" ];then
        printf "Starting MySQL...\n"
        mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf &> /dev/null &
        else
            printf "MySQL is running...\n"
            exit
    fi
}

function_stop_mysql()
{
    if [ ! -e "$mysql_sock" ];then
        printf "MySQL is stopped...\n"
        exit
    else
        printf "Stoping MySQL...\n"
        mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
        fi
}
function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 2
    function_start_mysql
}
case $1 in
start)
    function_start_mysql
    ;; 
stop)
    function_stop_mysql
    ;;
restart)
    function_restart_mysql
    ;;
*)
    printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"
esac

7.启动服务

/usr/local/mysql/3306/bin/mysqld start

8.登录数据库

mysql -uroot -S /usr/local/mysql/3306/socket/mysql.sock
最后修改:2024 年 01 月 04 日
如果觉得我的文章对你有用,请随意赞赏