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

Mariadb-多实例安装

Mariadb多实例安装

1.数据库多实例一般在测试环境中使用,生成环境不建议使用多实例

为何要做多实例呢
说白了就是缺机器呗
基于Mariadb源码编译来进行多实例安装
规划多实例目录文件,这里目录用端口号来代替了,容易区分

[root@node6 ~]#mkdir /MysqL/{3307,3308,3309} -p
[root@node6 ~]#ls /MysqL/330
3307/ 3308/ 3309/
[root@node6 /MysqL]#mkdir {3307,3308,3309}/{data,etc,socket,log,pid} -pv
mkdir: created directory ‘3307/data’
mkdir: created directory ‘3307/etc’
mkdir: created directory ‘3307/socket’
mkdir: created directory ‘3307/log’
mkdir: created directory ‘3307/pid’
mkdir: created directory ‘3308/data’
mkdir: created directory ‘3308/etc’
mkdir: created directory ‘3308/socket’
mkdir: created directory ‘3308/log’
mkdir: created directory ‘3308/pid’
mkdir: created directory ‘3309/data’
mkdir: created directory ‘3309/etc’
mkdir: created directory ‘3309/socket’
mkdir: created directory ‘3309/log’
mkdir: created directory ‘3309/pid’
# 查看目录结构
[root@node6 /MysqL]#tree .
.
├── 3307
│   ├── data
│   ├── etc
│   ├── log
│   ├── pid
│   └── socket
├── 3308
│   ├── data
│   ├── etc
│   ├── log
│   ├── pid
│   └── socket
└── 3309
    ├── data
    ├── etc
    ├── log
    ├── pid
    └── socket

2.修改目录的所有者为MysqL用户

[root@node6 /MysqL]#ll 
total 0
drwxr-xr-x 6 root root 53 May  3 09:49 3307
drwxr-xr-x 6 root root 53 May  3 09:49 3308
drwxr-xr-x 6 root root 53 May  3 09:49 3309
[root@node6 /MysqL]#chown -R MysqL.MysqL *
[root@node6 /MysqL]#ll 
total 0
drwxr-xr-x 6 MysqL MysqL 53 May  3 09:49 3307
drwxr-xr-x 6 MysqL MysqL 53 May  3 09:49 3308
drwxr-xr-x 6 MysqL MysqL 53 May  3 09:49 3309

3.生成各自的数据库文件

# 先进入到源码或者二进制安装的程序目录下
[root@node6 /MysqL]#cd /data/MysqL/
[root@node6 /data/MysqL]#./scripts/MysqL_install_db --user=MysqL --datadir=/MysqL/3307/data
[root@node6 /data/MysqL]#./scripts/MysqL_install_db --user=MysqL --datadir=/MysqL/3308/data
[root@node6 /data/MysqL]#./scripts/MysqL_install_db --user=MysqL --datadir=/MysqL/3309/data

# 查看生成好的数据库文件目录,这里就查看其中一个3307
[root@node6 /data/MysqLll /MysqL/3307/data
total 110660
-rw-rw---- 1 MysqL MysqL    16384 May  3 09:56 aria_log.00000001
-rw-rw---- 1 MysqL MysqL       52 May  3 09:56 aria_log_control
-rw-rw---- 1 MysqL MysqL      938 May  3 09:56 ib_buffer_pool
-rw-rw---- 1 MysqL MysqL 12582912 May  3 09:56 ibdata1
-rw-rw---- 1 MysqL MysqL 50331648 May  3 09:56 ib_logfile0
-rw-rw---- 1 MysqL MysqL 50331648 May  3 09:56 ib_logfile1
drwx------ 2 MysqL root      4096 May  3 09:56 MysqL
-rw-rw---- 1 MysqL MysqL    28999 May  3 09:56 MysqL-bin.000001
-rw-rw---- 1 MysqL MysqL       19 May  3 09:56 MysqL-bin.index
-rw-rw---- 1 MysqL MysqL        7 May  3 09:56 MysqL-bin.state
drwx------ 2 MysqL MysqL       20 May  3 09:56 performance_schema
drwx------ 2 MysqL root         6 May  3 09:56 test

4.创建各个数据库配置文件,以及修改各个配置文件的端口号以及目录

[root@node6 /data/MysqL]#cp /etc/my.cnf /MysqL/3307/etc/
[root@node6 /MysqL/3307/etc]#vim my.cnf
[MysqLd]
port=3307
datadir=/MysqL/3307/
socket=/MysqL/3307/socket/MysqL.sock
# disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run MysqLd under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[MysqLd_safe]
log-error=/MysqL/3307/log/mariadb.log
pid-file=/MysqL/3307/pid/mariadb.pid

#
# include all files from the config directory
#
#!includedir /etc/my.cnf.d

# 把3307上的配置文件复制到后面两个实例上去
[root@node6 /MysqL/3307/etc]#cp my.cnf /MysqL/3308/etc/
[root@node6 /MysqL/3307/etc]#cp my.cnf /MysqL/3309/etc/

# 修改其它两个实例的配置文件改成相对应的端口和目录
[root@node6 /MysqL/3307/etc]#sed -i 's/3307/3308/' /MysqL/3308/etc/my.cnf 
[root@node6 /MysqL/3307/etc]#sed -i 's/3307/3309/' /MysqL/3309/etc/my.cnf 

5.创建服务的启动脚本

[root@node6 /MysqL/3307]#vim MysqLd
#!/bin/bash
#chkconfig: 345 80 2
port=3307
MysqL_user="root"
MysqL_pwd=""
cmd_path="/data/MysqL/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

# 把3307上的启动脚本复制到3308、3309上去,并把启动端口修改即可,其它不用修改
[root@node6 /MysqL/3307]#cp MysqLd ../3308/
[root@node6 /MysqL/3307]#cp MysqLd ../3309/
[root@node6 /MysqL/3307]#sed -i 's/3307/3308/' ../3308/MysqLd
[root@node6 /MysqL/3307]#sed -i 's/3307/3309/' ../3309/MysqLd

6.启动多实例,并查看相对应的端口是否处于监听状态

# 添加执行权限给MysqLd
[root@node6 /MysqL/3307]#chmod +x MysqLd 
[root@node6 /MysqL/3307]#chmod +x ../3308/MysqLd 
[root@node6 /MysqL/3307]#chmod +x ../3309/MysqLd 
[root@node6 /MysqL/3307]#./MysqLd start
Starting MysqL...
[root@node6 /MysqL/3307]#../3308/MysqLd start 
Starting MysqL...
[root@node6 /MysqL/3307]#../3309/MysqLd start 
Starting MysqL...
[root@node6 /MysqL/3307]#ss -tnl 
State      Recv-Q Send-Q                         Local Address:Port                                        Peer Address:Port              
LISTEN     0      128                                        *:111                                                    *:*                  
LISTEN     0      128                                        *:22                                                     *:*                  
LISTEN     0      100                                127.0.0.1:25                                                     *:*                  
LISTEN     0      80                                        :::3306                                                  :::*                  
LISTEN     0      80                                        :::3307                                                  :::*                  
LISTEN     0      80                                        :::3308                                                  :::*                  
LISTEN     0      80                                        :::3309                                                  :::*                  
LISTEN     0      128                                       :::111                                                   :::*                  
LISTEN     0      128                                       :::22                                                    :::*                  
LISTEN     0      100                                      ::1:25                                                    :::*             

7.连接到多实例上去

[root@node6 /MysqL/3307]#MysqL -uroot -p -S /MysqL/3307/socket/MysqL.sock 
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.23-MariaDB Source distribution

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)]> select @@port;
+--------+
| @@port |
+--------+
|   3307 |
+--------+
1 row in set (0.00 sec)

# 修改3307实例密码和加固安全,修改之后的密码需要在启动脚本中添加进去
[root@node6 /MysqL/3307]#MysqL_secure_installation -S /MysqL/3307/socket/MysqL.sock
[root@node6 /MysqL/3307]#vim MysqLd
#!/bin/bash
#chkconfig: 345 80 2
port=3307
MysqL_user="root"
MysqL_pwd="123456"

8.把启动脚本拷贝到/etc/init.d/改名为MysqLd3307以示区分是哪个实例的启动脚本,并添加到开机启动项里

[root@node6 /MysqL/3307]#cp MysqLd /etc/init.d/MysqLd3307
[root@node6 /MysqL/3307]#chkconfig --add MysqLd3307
[root@node6 /MysqL/3307]#chkconfig --list
MysqLd          0:off   1:off   2:on    3:on    4:on    5:on    6:off
MysqLd3307      0:off   1:off   2:off   3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off

后续就可以登录数据库里进行操作了

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

相关推荐