MariaDB多实例

Posted

tags:

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

MariaDB多实例:

环境和思路:

centos7.4

yum安装实现。

一个版本mysql通过多个端口实现

规划3个实例的端口:3306/3307/3308。

将各自的配置文件、日志文件、pid、socket单独存放。

安装MariaDB:

[[email protected] ~]#yum install mariadb-server
三个数据库的数据都放在独自的实例中:
[[email protected] ~]#mkdir /mysqldb/{3306,3307,3308}/{etc,socket,pid,log,data} -pv
查看目录结构:
[[email protected] ~]#tree /mysqldb/
/mysqldb/
├── 3306
│   ├── data
│   ├── etc
│   ├── log
│   ├── pid
│   └── socket
├── 3307
│   ├── data
│   ├── etc
│   ├── log
│   ├── pid
│   └── socket 
└── 3308
    ├── data
    ├── etc
    ├── log
    ├── pid
    └── socket

由于是yum安装mysql账号已经被创建好了

[[email protected] ~]#getent passwd mysql
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
修改数据库目录的所有者和所有组:
[[email protected] ~]#chown -R mysql.mysql /mysqldb
生成三个数据库各自的数据库文件:
[[email protected] ~]#mysql_install_db  --datadir=/mysqldb/3306/data --user=mysql --basedir=/usr
[[email protected] ~]#mysql_install_db  --datadir=/mysqldb/3307/data --user=mysql --basedir=/usr
[[email protected] ~]#mysql_install_db  --datadir=/mysqldb/3308/data --user=mysql --basedir=/usr
准备配置文件:

根据3个不同的实例准备3个配置文件:

思路:用/etc/my.cnf 当模板:

将配置文件考到各自的文件下:

[[email protected] ~]#cp /etc/my.cnf /mysqldb/3306/etc/
[[email protected] ~]#cp /etc/my.cnf /mysqldb/3307/etc/
[[email protected] ~]#cp /etc/my.cnf /mysqldb/3308/etc/
修改各自的配置文件3306/3307/3308:
[[email protected] ~]#vim /mysqldb/3306/etc/my.cnf
[mysqld]
port=3306       (手动必须添加)
datadir=/mysqldb/3306/data
socket=/mysqldb/3306/socket/mysql.sock
[mysqld_safe]
log-error=/mysqldb/3306/log/mariadb.log
pid-file=/mysqldb/3306/pid/mariadb.pid
#!includedir /etc/my.cnf.d(注释掉)

==> 端口为3307/3308的配置文件也要手动修改==

准备启动服务脚本:

参考格式:

[[email protected] ~]#cat /usr/lib/systemd/system/mariadb.service

启动的关键命令:

ExecStart=/usr/bin/mysqld_safe --basedir=/usr

停止yum安装的mysql服务防止冲突:
[[email protected] ~]#systemctl stop mariadb
运行创建启动脚本:

将脚本考入对应的目录里:

==> 将脚本分别存个在各自的路径下3306/3307/3308,将prot改为各自的端口号运行。==

[[email protected] ~]#rz /mysqldb/3306/mysqld
[[email protected] ~]#rz /mysqldb/3307/mysqld
[[email protected] ~]#rz /mysqldb/3308/mysqld

==> 将脚本里对应的端口和数据库路径编辑正确==

#!/bin/bash

port=3306
mysql_user="root"
mysql_pwd=""
cmd_path="/usr/bin"
mysql_basedir="/mysqldb"
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
脚本考到各自的运行路径下:
[[email protected] ~]#cp /mysqldb/3306/mysqld  /mysqldb/3307/(配置文件端口改为port=3307)
[[email protected] ~]#cp /mysqldb/3306/mysqld  /mysqldb/3308/(配置文件端口改为port=3308)
设置权限:

密码会放在mysqld目录里所有权限设置的安全一些

[[email protected] ~]#chmod 700 /mysqldb/3306/mysqld
[[email protected] ~]#chmod 700 /mysqldb/3307/mysqld
[[email protected] ~]#chmod 700 /mysqldb/3308/mysqld
启动服务:
[[email protected] ~]#/mysqldb/3306/mysqld start
[[email protected] ~]#/mysqldb/3307/mysqld start
[[email protected] ~]#/mysqldb/3308/mysqld start
查看服务启动:
[[email protected] ~]#ss -ntl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      50               *:3307                         *:*                  
LISTEN     0      50               *:3308                         *:*                  
LISTEN     0      128              *:111                          *:*                  
LISTEN     0      5      192.168.122.1:53                           *:*                  
LISTEN     0      128              *:22                           *:*                  
LISTEN     0      128      127.0.0.1:631                          *:*                  
LISTEN     0      100      127.0.0.1:25                           *:*                  
LISTEN     0      50               *:3306                         *:*                  
LISTEN     0      128             :::111                         :::*                  
测试连接:

需要用sock文件连接mysql(指定数据库路径连接)

[[email protected] ~]#mysql -S /mysqldb/3306/socket/mysql.sock
[[email protected] ~]#mysql -S /mysqldb/3307/socket/mysql.sock
[[email protected] ~]#mysql -S /mysqldb/3308/socket/mysql.sock
通过查看端口判断进入是哪个实例:
MariaDB [(none)]>  show variables like ‘port‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+   

设置口令:

[[email protected] ~]#mysqladmin -uroot -S /mysqldb/3306/socket/mysql.sock password ‘centos‘
关闭mysql时需要输入口令:
[[email protected] ~]#/mysqldb/3306/mysqld stop
Stoping MySQL...
Enter password: 
把口令添加到脚本里:
[[email protected] ~]#vim /mysqldb/3306/mysqld
mysql_pwd="centos"

这时关闭mysql不需要输入口令

进入mysql需要口令了:
[[email protected] ~]#mysql -pcentos -S /mysqldb/3306/socket/mysql.sock

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

mariadb多实例安装

MariaDB多实例

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

mariadb10.2 多实例

Linux之Ansible安装多实例mariadb

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