在一台物理机中须要多个测试环境,那么就须要用到了搭建数据库的多个实例,多个实例的意思就是运行多份程序,实例与实例之间没有影响。要注意监听的端口须要不一样。也就是在一台主机上跑好多个mysql实例数据库,每一个数据库本身管理本身独立的数据库文件。mysql
准备环境:centos7.4,关闭防火墙,关闭SElinux,yum安装实现多实例 yum install mariadb-serversql
1:建立运行的目录环境数据库
[root@centos7 ~]# mkdir /mysqldb/{3306,3307,3308}/{etc,socket,pid,log,data} -pv [root@centos7 ~]# chown -R mysql.mysql /mysqldb/ 指定文件夹的全部者,所属组 ,这样mysql对全部的文件夹起到控制的做用 [root@centos7 ~]# tree /mysqldb/
2:(建立数据库)生成三个不用的数据库文件放到各自的data文件夹中
vim
[root@centos7 ~]# mysql_install_db --datadir=/mysqldb/3306/data/ --user=mysql [root@centos7 ~]# mysql_install_db --datadir=/mysqldb/3307/data/ --user=mysql [root@centos7 ~]# mysql_install_db --datadir=/mysqldb/3308/data/ --user=mysql
三、复制模板配置文件,并进行修改配置文件centos
[root@centos7 ~]# cp /etc/my.cnf /mysqldb/3306/etc/ [root@centos7 ~]# cp /etc/my.cnf /mysqldb/3307/etc/ [root@centos7 ~]# cp /etc/my.cnf /mysqldb/3308/etc/ [root@centos7 ~]# vim /mysqldb/3308/etc/my.cnf [mysqld] port=3308 datadir=/mysqldb/3308/data socket=/mysqldb/3308/socket/mysql.sock symbolic-links=0 [mysqld_safe] log-error=/mysqldb/3308/log/mariadb.log pid-file=/mysqldb/3308/pid/mariadb.pid [root@centos7 ~]# cp /mysqldb/3308/etc/my.cnf /mysqldb/3306/etc/my.cnf [root@centos7 ~]# cp /mysqldb/3308/etc/my.cnf /mysqldb/3307/etc/my.cnf :%s /3308/3307/g 脚本搜索全局替换
4:准备启动服务脚本 安全
[root@centos7 ~]# vim mysql #!bin/bash/
port=3306 #须要修改成当前实例的端口号 mysql_user="root" mysql_pwd="" cmd_path="/usr/bin" #安装目录下的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 [root@centos7 ~]# cp mysql /mysqldb/3306/ [root@centos7 ~]# cp mysql /mysqldb/3307/ [root@centos7 ~]# cp mysql /mysqldb/3308/
5:修改脚本权限,安全保障,防止密码被人看到 bash
[root@centos7 ~]# chmod 700 /mysqldb/3308/mysql [root@centos7 ~]# chmod 700 /mysqldb/3307/mysql [root@centos7 ~]# chmod 700 /mysqldb/3306/mysql
6:启动服务socket
[root@centos7 /mysqldb]# service mysql stop #保证本身原来的服务中止,释放3306端口
[root@centos7 /mysqldb]#sh /mysqldb/3306/mysqld start [root@centos7 /mysqldb]#sh /mysqldb/3307/mysqld start [root@centos7 /mysqldb]#sh /mysqldb/3308/mysqld start
#若是看到三个实例监听的端口都打开后说明服务启动正常
7:链接测试测试
[root@centos7 /mysqldb]# mysql -S /mysqldb/3308/socket/mysql.sock加密
8:多实例搭建成功!如下命令可用来中止实例
[root@centos7 /mysqldb]# sh /mysqldb/3308/mysqld stop
9:给root加密码
[root@centos7 /mysqldb]# mysql -S /mysqldb/3307/socket/mysql.sock Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 8 Server version: 10.2.16-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 statem ent. MariaDB [(none)]> grant all on *.* to root@localhost IDENTIFIED BY '111111'; MariaDB [(none)]> use mysql; Database changed MariaDB [mysql]> select user,host from user; MariaDB [mysql]> select user,host,password from user; +------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *FD571203974BA9AFE270FE62151AE967ECA5E0AA |
| root | centos7 | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | centos7 | |
+------+-----------+-------------------------------------------+
[root@centos7 /mysqldb]# mysql -uroot -S /mysqldb/3307/socket/mysql.sock -p111111#指定密码,再次登陆