MariaDB 多实例:
环境和思路:
centos7.4
yum 安装实现.
一个版本 mysql 通过多个端口实现
规划 3 个实例的端口: 3306/3307/3308.
将各自的配置文件, 日志文件, pid,socket 单独存放.
安装 MariaDB:
[root@tang ~]#yum install mariadb-server
三个数据库的数据都放在独自的实例中:
[root@tang ~]#mkdir /mysqldb/{3306,3307,3308}/{etc,socket,pid,log,data} -pv
查看目录结构:
- [root@tang ~]#tree /mysqldb/
- /mysqldb/
- 3306
- data
- etc
- log
- pid
- socket
- 3307
- data
- etc
- log
- pid
- socket
- 3308
- data
- etc
- log
- pid
- socket
由于是 yum 安装 mysql 账号已经被创建好了
- [root@tang ~]#getent passwd mysql
- mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
修改数据库目录的所有者和所有组:
[root@tang ~]#chown -R mysql.mysql /mysqldb
生成三个数据库各自的数据库文件:
- [root@tang ~]#mysql_install_db --datadir=/mysqldb/3306/data --user=mysql --basedir=/usr
- [root@tang ~]#mysql_install_db --datadir=/mysqldb/3307/data --user=mysql --basedir=/usr
- [root@tang ~]#mysql_install_db --datadir=/mysqldb/3308/data --user=mysql --basedir=/usr
准备配置文件:
根据 3 个不同的实例准备 3 个配置文件:
思路: 用 / etc/my.cnf 当模板:
将配置文件考到各自的文件下:
- [root@tang ~]#cp /etc/my.cnf /mysqldb/3306/etc/
- [root@tang ~]#cp /etc/my.cnf /mysqldb/3307/etc/
- [root@tang ~]#cp /etc/my.cnf /mysqldb/3308/etc/
修改各自的配置文件 3306/3307/3308:
- [root@tang ~]#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 的配置文件也要手动修改 ==
准备启动服务脚本:
参考格式:
[root@tang ~]#cat /usr/lib/systemd/system/mariadb.service
启动的关键命令:
ExecStart=/usr/bin/mysqld_safe --basedir=/usr
停止 yum 安装的 mysql 服务防止冲突:
[root@tang ~]#systemctl stop mariadb
运行创建启动脚本:
将脚本考入对应的目录里:
==> 将脚本分别存个在各自的路径下 3306/3307/3308, 将 prot 改为各自的端口号运行.==
- [root@tang ~]#rz /mysqldb/3306/mysqld
- [root@tang ~]#rz /mysqldb/3307/mysqld
- [root@tang ~]#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...\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
脚本考到各自的运行路径下:
- [root@tang ~]#cp /mysqldb/3306/mysqld /mysqldb/3307/(配置文件端口改为 port=3307)
- [root@tang ~]#cp /mysqldb/3306/mysqld /mysqldb/3308/(配置文件端口改为 port=3308)
设置权限:
密码会放在 mysqld 目录里所有权限设置的安全一些
- [root@tang ~]#chmod 700 /mysqldb/3306/mysqld
- [root@tang ~]#chmod 700 /mysqldb/3307/mysqld
- [root@tang ~]#chmod 700 /mysqldb/3308/mysqld
启动服务:
- [root@tang ~]#/mysqldb/3306/mysqld start
- [root@tang ~]#/mysqldb/3307/mysqld start
- [root@tang ~]#/mysqldb/3308/mysqld start
查看服务启动:
- [root@tang ~]#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(指定数据库路径连接)
- [root@tang ~]#mysql -S /mysqldb/3306/socket/mysql.sock
- [root@tang ~]#mysql -S /mysqldb/3307/socket/mysql.sock
- [root@tang ~]#mysql -S /mysqldb/3308/socket/mysql.sock
通过查看端口判断进入是哪个实例:
- MariaDB [(none)]> show variables like 'port';
- +---------------+-------+
- | Variable_name | Value |
- +---------------+-------+
- | port | 3306 |
- +---------------+-------+
设置口令:
[root@tang ~]#mysqladmin -uroot -S /mysqldb/3306/socket/mysql.sock password 'centos'
关闭 mysql 时需要输入口令:
- [root@tang ~]#/mysqldb/3306/mysqld stop
- Stoping MySQL...
- Enter password:
把口令添加到脚本里:
- [root@tang ~]#vim /mysqldb/3306/mysqld
- mysql_pwd="centos"
这时关闭 mysql 不需要输入口令
进入 mysql 需要口令了:
[root@tang ~]#mysql -pcentos -S /mysqldb/3306/socket/mysql.sock
来源: http://www.bubuko.com/infodetail-2731038.html