Multi instance implementation of mysql database under Linux

Keywords: MySQL Database MariaDB socket

Preparation environment:

centos7 install Yum install MariaDB server

Plan and implement the directory structure of multiple instances

Port: 33063307, 3308

The folder where each instance stores the database / data / MySQL {330633073308}

/data/mysql/3306/{etc,soket,log,data,pid,bin}

Create the data folder required by mysql under the data / folder

[root@centos7-5 data]# mkdir /data/mysql/{3306,3307,3308}/{data,etc,bin,pid,log,socket} -pv

Refer to the properties of the home directory owner and the group after mysql is installed in the system

[root@centos7-5 data]# ll /var/lib/mysql/ -d
drwxr-xr-x 5 mysql mysql 177 Sep 30 10:41 /var/lib/mysql/

[root@centos7-5 data]# ll /var/lib/mysql/
drwx------ 2 mysql mysql 4096 Sep 30 10:41 mysql
srwxrwxrwx 1 mysql mysql 0 Sep 30 10:41 mysql.sock
drwx------ 2 mysql mysql 4096 Sep 30 10:41 performance_schema
drwx------ 2 mysql mysql 6 Sep 30 10:41 test

Modify the owner and group permissions of the instance database

[root@centos7-5 data]# chown mysql:mysql mysql           ##Modify the owner and group permissions of the instance database

Script path of default build database file:

[root@centos7-5 data]#rpm -ql mariadb-server

/usr/bin/mysql_install_db
[root@centos7-5 data]# /usr/bin/mysql_install_db --help ##View script usage
[root@centos7-5 data]# /usr/bin/mysql_install_db --datadir=/data/mysql/3306/data ###Generate database file
[root@centos7-5 data]# /usr/bin/mysql_install_db --datadir=/data/mysql/3307/data ###Generate database file
[root@centos7-5 data]# /usr/bin/mysql_install_db --datadir=/data/mysql/3308/data ###Generate database file

Prepare the profile and modify the profile

[root@centos7-5 mysql]# cp my.cnf /data/mysql/3306/etc/ ###Copy configuration file and modify
[root@centos7-5 mysql]# cp my.cnf /data/mysql/3307/etc/ ###Copy configuration file and modify
[root@centos7-5 mysql]# cp my.cnf /data/mysql/3308/etc/ ###Copy configuration file and modify
[mysqld]

port=3306

datadir=/data/mysql/3306/data

socket=/data/mysql/3306/socket/mysql.sock

[mysqld_safe]

log-error=/data/mysql/3306/log/mariadb.log

pid-file=/data/mysql/3306/pid/mariadb.pid                    ###The configuration file needs to be modified. Other 3307 and 3308 instances need to be modified in the same way

Prepare startup program file

#!/bin/bash
port=3306
mysql_user="root"
mysql_pwd=""
cmd_path="/usr/bin"
mysql_basedir="/data/mysql"
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"

function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
${cmd_path}/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"
${cmd_path}/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

The startup file is in the installation package when binary compilation is installed; in the support files folder

[root@centos7-5 3306]# /bin/mysqld start                   ##Start program

LISTEN 0 50 *:3306
[root@centos7-5 3306]# ../3307/bin/mysqld start       ##Start program

LISTEN 0 50 *:3307
[root@centos7-5 3306]# ../3308/bin/mysqld start          ##Start program

LISTEN 0 50 *:3308
MariaDB [(none)]> show variables like "port";                 ##View the current database connection port

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port | 3306 |

+---------------+-------+

Set the startup auto start script

[root@centos7-5 ~]# cp /data/mysql/3306/bin/mysqld /etc/init.d/mysql3306
[root@centos7-5 ~]#vim /etc/init.d/mysql3306

chkconfig:345 20 800

description: mysql 3306 ##Add these two lines
[root@centos7-5 ~]# chkconfig --add mysql3306 ##Add to power on execution list

Or add the boot script path / etc/rc.local to the boot path

/data/mysql/3306/bin/mysqld

 

Add: [root @ centos7-5 ~] (RPM - QL MariaDB server) binary path

/usr/libexec/mysqld

 

Posted by coco777 on Wed, 18 Dec 2019 08:55:32 -0800