标签:img ima sock usr 命令 原来 class install family
在一台物理机中需要多个测试环境,那么就需要用到了搭建数据库的多个实例,多个实例的意思就是运行多份程序,实例与实例之间没有影响。要注意监听的端口需要不同。也就是在一台主机上跑好多个mysql实例数据库,每个数据库自己管理自己独立的数据库文件。
准备环境:centos7.4,关闭防火墙,关闭SElinux,yum安装实现多实例 yum install mariadb-server
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文件夹中
[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
3、复制模板配置文件,并进行修改配置文件
[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:修改脚本权限,安全保障,防止密码被人看到
[root@centos7 ~]# chmod 700 /mysqldb/3308/mysql
[root@centos7 ~]# chmod 700 /mysqldb/3307/mysql
[root@centos7 ~]# chmod 700 /mysqldb/3306/mysql
6:启动服务
[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
#指定密码,再次登录
标签:img ima sock usr 命令 原来 class install family
原文地址:https://www.cnblogs.com/wang-jin/p/9377033.html