1. 程式人生 > >Mariadb-多實例安裝

Mariadb-多實例安裝

dex stop rec ket password mod systemd ddr list

Mariadb多實例安裝

1.數據庫多實例一般在測試環境中使用,生成環境不建議使用多實例

為何要做多實例呢
說白了就是缺機器唄
基於Mariadb源碼編譯來進行多實例安裝
規劃多實例目錄文件,這裏目錄用端口號來代替了,容易區分

[[email protected] ~]#mkdir /mysql/{3307,3308,3309} -p
[[email protected] ~]#ls /mysql/330
3307/ 3308/ 3309/
[[email protected] /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’
# 查看目錄結構
[[email protected] /mysql]#tree .
.
├── 3307
│?? ├── data
│?? ├── etc
│?? ├── log
│?? ├── pid
│?? └── socket
├── 3308
│?? ├── data
│?? ├── etc
│?? ├── log
│?? ├── pid
│?? └── socket
└── 3309
  ? ├── data
    ├── etc
    ├── log
    ├── pid
    └── socket

2.修改目錄的所有者為mysql用戶

[[email protected] /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
[[email protected] /mysql]#chown -R mysql.mysql *
[[email protected] /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.生成各自的數據庫文件

# 先進入到源碼或者二進制安裝的程序目錄下
[[email protected] /mysql]#cd /data/mysql/
[[email protected] /data/mysql]#./scripts/mysql_install_db --user=mysql --datadir=/mysql/3307/data
[[email protected] /data/mysql]#./scripts/mysql_install_db --user=mysql --datadir=/mysql/3308/data
[[email protected] /data/mysql]#./scripts/mysql_install_db --user=mysql --datadir=/mysql/3309/data

# 查看生成好的數據庫文件目錄,這裏就查看其中一個3307
[[email protected] /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.創建各個數據庫的配置文件,以及修改各個配置文件的端口號以及目錄

[[email protected] /data/mysql]#cp /etc/my.cnf /mysql/3307/etc/
[[email protected] /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上的配置文件復制到後面兩個實例上去
[[email protected] /mysql/3307/etc]#cp my.cnf /mysql/3308/etc/
[[email protected] /mysql/3307/etc]#cp my.cnf /mysql/3309/etc/

# 修改其它兩個實例的配置文件改成相對應的端口和目錄
[[email protected] /mysql/3307/etc]#sed -i ‘s/3307/3308/‘ /mysql/3308/etc/my.cnf 
[[email protected] /mysql/3307/etc]#sed -i ‘s/3307/3309/‘ /mysql/3309/etc/my.cnf 

5.創建服務的啟動腳本

[[email protected] /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上去,並把啟動端口修改即可,其它不用修改
[[email protected] /mysql/3307]#cp mysqld ../3308/
[[email protected] /mysql/3307]#cp mysqld ../3309/
[[email protected] /mysql/3307]#sed -i ‘s/3307/3308/‘ ../3308/mysqld
[[email protected] /mysql/3307]#sed -i ‘s/3307/3309/‘ ../3309/mysqld

6.啟動多實例,並查看相對應的端口是否處於監聽狀態

# 添加執行權限給mysqld
[[email protected] /mysql/3307]#chmod +x mysqld 
[[email protected] /mysql/3307]#chmod +x ../3308/mysqld 
[[email protected] /mysql/3307]#chmod +x ../3309/mysqld 
[[email protected] /mysql/3307]#./mysqld start
Starting MySQL...
[[email protected] /mysql/3307]#../3308/mysqld start 
Starting MySQL...
[[email protected] /mysql/3307]#../3309/mysqld start 
Starting MySQL...
[[email protected] /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.連接到多實例上去

[[email protected] /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實例密碼和加固安全,修改之後的密碼需要在啟動腳本中添加進去
[[email protected] /mysql/3307]#mysql_secure_installation -S /mysql/3307/socket/mysql.sock
[[email protected] /mysql/3307]#vim mysqld
#!/bin/bash
#chkconfig: 345 80 2
port=3307
mysql_user="root"
mysql_pwd="123456"

8.把啟動腳本拷貝到/etc/init.d/改名為mysqld3307以示區分是哪個實例的啟動腳本,並添加到開機啟動項裏

[[email protected] /mysql/3307]#cp mysqld /etc/init.d/mysqld3307
[[email protected] /mysql/3307]#chkconfig --add mysqld3307
[[email protected] /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

後續就可以登錄到數據庫裏進行操作了

Mariadb-多實例安裝