Mysql------多实例配置
Posted wang-jin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql------多实例配置相关的知识,希望对你有一定的参考价值。
mysql的多实例配置
在一台物理机中需要多个测试环境,那么就需要用到了搭建数据库的多个实例,多个实例的意思就是运行多份程序,实例与实例之间没有影响。要注意监听的端口需要不同。也就是在一台主机上跑好多个mysql实例数据库,每个数据库自己管理自己独立的数据库文件。
准备环境:centos7.4,关闭防火墙,关闭SElinux,yum安装实现多实例 yum install mariadb-server
1:创建运行的目录环境
[[email protected] ~]# mkdir /mysqldb/{3306,3307,3308}/{etc,socket,pid,log,data} -pv [[email protected] ~]# chown -R mysql.mysql /mysqldb/ 指定文件夹的所有者,所属组 ,这样mysql对所有的文件夹起到控制的作用 [[email protected] ~]# tree /mysqldb/
2:(创建数据库)生成三个不用的数据库文件放到各自的data文件夹中
[[email protected] ~]# mysql_install_db --datadir=/mysqldb/3306/data/ --user=mysql
[[email protected] ~]# mysql_install_db --datadir=/mysqldb/3307/data/ --user=mysql
[[email protected] ~]# mysql_install_db --datadir=/mysqldb/3308/data/ --user=mysql
3、复制模板配置文件,并进行修改配置文件
[[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/
[[email protected] ~]# 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
[[email protected] ~]# cp /mysqldb/3308/etc/my.cnf /mysqldb/3306/etc/my.cnf
[[email protected] ~]# cp /mysqldb/3308/etc/my.cnf /mysqldb/3307/etc/my.cnf
:%s /3308/3307/g 脚本搜索全局替换
4:准备启动服务脚本
[[email protected] ~]# 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...
"
${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
[[email protected] ~]# cp mysql /mysqldb/3306/
[[email protected] ~]# cp mysql /mysqldb/3307/
[[email protected] ~]# cp mysql /mysqldb/3308/
5:修改脚本权限,安全保障,防止密码被人看到
[[email protected] ~]# chmod 700 /mysqldb/3308/mysql
[[email protected] ~]# chmod 700 /mysqldb/3307/mysql
[[email protected] ~]# chmod 700 /mysqldb/3306/mysql
6:启动服务
[[email protected] /mysqldb]# service mysql stop #保证自己原来的服务停止,释放3306端口
[[email protected] /mysqldb]#sh /mysqldb/3306/mysqld start [[email protected] /mysqldb]#sh /mysqldb/3307/mysqld start [[email protected] /mysqldb]#sh /mysqldb/3308/mysqld start
#如果看到三个实例监听的端口都打开后说明服务启动正常
7:连接测试
[[email protected] /mysqldb]# mysql -S /mysqldb/3308/socket/mysql.sock
8:?多实例搭建成功!以下命令可用来停止实例
[[email protected] /mysqldb]# sh /mysqldb/3308/mysqld stop
9:给root加密码
[[email protected] /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 [email protected] 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 | |
+------+-----------+-------------------------------------------+
[[email protected] /mysqldb]# mysql -uroot -S /mysqldb/3307/socket/mysql.sock -p111111
#指定密码,再次登录
以上是关于Mysql------多实例配置的主要内容,如果未能解决你的问题,请参考以下文章
如何为 XSLT 代码片段配置 CruiseControl 的 C# 版本?