mariadb多实例安装

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mariadb多实例安装相关的知识,希望对你有一定的参考价值。

基于YUM安装的mariadb多实例
.=================================================================

1. yum 安装mariadb-server包
#yum install mariadb-server

2. 创建多实例对应的目录结构
#mkdir /mysql/{3306,3307,3308}/{data,etc,socket,log,bin,pid} -pv
#tree /mysql/
/mysql/
├── 3306
│ ├── bin
│ ├── data
│ ├── etc
│ ├── log
│ ├── pid
│ └── socket
├── 3307
│ ├── bin
│ ├── data
│ ├── etc
│ ├── log
│ ├── pid
│ └── socket
└── 3308
├── bin
├── data
├── etc
├── log
├── pid
└── socket

#chown -R mysql.mysql /mysql

3. 创建多实例的数据库文件
#mysql_install_db --datadir=/mysql/3306/data/ --user=mysql
#mysql_install_db --datadir=/mysql/3307/data/ --user=mysql
#mysql_install_db --datadir=/mysql/3308/data/ --user=mysql

4. 创建对应配置文件
#cp /etc/my.cnf /mysql/3306/etc
#vim /mysql/3306/etc/my.cnf
[mysqld]
port=3306 加一行
datadir=/mysql/3306/data
socket=/mysql/3306/socket/mysql.sock
[mysqld_safe]
log-error=/mysql/3306/log/mariadb.log
pid-file=/mysql/3306/pid/mariadb.pid

#cp /mysql/3306/etc/my.cnf /mysql/3307/etc/ 修改
#cp /mysql/3306/etc/my.cnf /mysql/3308/etc/ 修改

#vim /mysql/3307/etc/my.cnf
:%s/3306/3307/

#vim /mysql/3308/etc/my.cnf
:%s/3306/3308/

或者用sed命令改也可以
#sed -i ‘s/3306/3307/‘ /mysql/3307/etc/my.cnf
#sed -i ‘s/3306/3308/‘ /mysql/3308/etc/my.cnf

5. 准备各实例的启动脚本
#mysqladmin shutdown //因为正好要开mariadb多实例, 可以先把yum 安装的mariadb数据库先停了

思路:先创建3306实例的启动脚本,然后分别copy到3307和3308 bin目录下
#cd /mysql/3306/bin/
#vim mysqld
#!/bin/bash
port=3306
mysql_user="root"
mysql_pwd=""
cmd_path="/usr/bin"
mysql_basedir="/mysql"
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"

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

function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped... "
exit
else
printf "Stoping MySQL... "
${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
fi
}

function_restart_mysql()
{
printf "Restarting MySQL... "
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} "
esac

#cp mysqld ../../3307/bin/
#cp mysqld ../../3308/bin/

#vim ../../3308/bin/mysqld
port=3308
#vim ../../3307/bin/mysqld
port=3307

#chmod +x mysqld
#chmod +x ../../3307/bin/mysqld
#chmod +x ../../3308/bin/mysqld

6. 启动和关闭实例
#/mysql/3306/bin/mysqld start
#/mysql/3307/bin/mysqld start
#/mysql/3308/bin/mysqld start

#/mysql/3306/bin/mysqld stop
#/mysql/3307/bin/mysqld stop
#/mysql/3308/bin/mysqld stop

7. 测试连接
#mysql -S /mysql/3306/socket/mysql.sock
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 1
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.

MariaDB [(none)]>

[[email protected] bin]#mysql -S /mysql/3307/socket/mysql.sock
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 1
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘h‘ for help. Type ‘c‘ to clear the current input statement.

MariaDB [(none)]> show variables like ‘port‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3307 |
+---------------+-------+
1 row in set (0.00 sec)

#mysql -S /mysql/3308/socket/mysql.sock

8. 安全加固
#mysqladmin -S /mysql/3306/socket/mysql.sock password ‘root‘ //加登陆数据库口令
#mysql -S /mysql/3306/socket/mysql.sock -uroot -proot //用口令登陆

#mysqladmin -S /mysql/3307/socket/mysql.sock password ‘root‘
#mysql -S /mysql/3307/socket/mysql.sock -uroot -proot

#mysqladmin -S /mysql/3308/socket/mysql.sock password ‘root‘
#mysql -S /mysql/3308/socket/mysql.sock -uroot -proot

9. 配置免密登陆
编辑3306,3307,3308启动脚本,加上对应口令:root 这样启动关闭数据库就可以不用输入密码了
#vim /mysql/3306/bin/mysqld
mysql_pwd="root"

#vim /mysql/3307/bin/mysqld
mysql_pwd="root"

#vim /mysql/3308/bin/mysqld
mysql_pwd="root"

以上是关于mariadb多实例安装的主要内容,如果未能解决你的问题,请参考以下文章

初识MariaDB之2——MariaDB多实例安装

MariaDB多实例

Linux之Ansible安装多实例mariadb

基于二进制安装的mariadb实现多实例

基于源码编译安装Mariadb实现多实例

MariaDB三种方法安装及多实例实现