微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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...\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

#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)]>

[root@centos7mini 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"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐