#查看内核版本
[root@localhost ~]# uname -r
3.10.0-327.el7.x86_64
[root@localhost ~]# cat /etc/centos-release
CentOS Linux release 7.2.1511 (Core)
#安装包开启服务
yum install mariadb-server -y
systemctl start mariadb
#准备三个实例的目录
mkdir -pv /mysql/{3306,3307,3308}/{data,etc,socket,log,bin,pid}
chown -R mysql.mysql /mysql
#生成数据库文件
mysql_install_db --datadir=/mysql/3306/data --user=mysql
mysql_install_db --datadir=/mysql/3307/data --user=mysql
mysql_install_db --datadir=/mysql/3308/data --user=mysql
#准备配置文件. 3307 3308 同样的方法
[root@localhost ~]# cat /mysql/3306/etc/my.cnf
[client-server]
[mysqld]
port=3306
datadir=/mysql/3306/data/
socket=/mysql/3306/socket/mysql.sock
[mysqld_safe]
log-error=/mysql/3306/log/mariadb.log
pid-file=/mysql/3306/pid/mariadb.pid
!includedir /etc/my.cnf.d
#准备脚本文件
[root@localhost ~]# cat /mysql/3306/bin/mysqld
#!/bin/bash
port=3306
mysql_user="root"
mysql_pwd="magedu"
cmd_path="/usr/bin"
mysql_basedir="/mysql"
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
#重复上述过程,分别建立3307,3308的启动脚本
[root@localhost ~]# cp /mysql/3306/bin/mysqld /mysql/3307/bin/mysqld
[root@localhost ~]# sed -rin 's#3306#3307#p' /mysql/3307/bin/mysqld
[root@localhost ~]# cp /mysql/3306/bin/mysqld /mysql/3308/bin/mysqld
[root@localhost ~]# sed -rin 's#3306#3308#p' /mysql/3308/bin/mysqld
#启动关闭服务
[root@localhost ~]# systemctl stop mariadb #停止之前的mariadb
[root@localhost ~]# /mysql/3306/bin/mysqld start #开启实例3306
Starting MySQL...
[root@localhost ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@localhost ~]# /mysql/3306/bin/mysqld stop #关闭实例3306
Stoping MySQL...
[root@localhost ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
#同理:开启3307 3308 的实例
/mysql/3306/bin/mysqld start
/mysql/3307/bin/mysqld start
/mysql/3308/bin/mysqld start
#登录实例
/mysql/3306/bin/mysqld start
mysql -uroot -S /mysql/3306/socket/mysql.sock
mariadb>show variables like 'port'; #确认连接的端口
MariaDB [(none)]> create database 3307_db; #创建数据库
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> system ls /mysql/3307/data/3307_db -d ; #在该文件下就生成了目录
/mysql/3307/data/3307_db
#修改root密码
[root@localhost ~]# /mysql/3306/bin/mysqld start
Starting MySQL...
[root@localhost ~]# mysqladmin -uroot -S /mysql/3306/socket/mysql.sock password '123456' #加上新口令
#重复步骤,分别修改别外两个实例3307,3308对应root口令
#测试连接
[root@localhost ~]# /mysql/3306/bin/mysqld start
Starting MySQL...
[root@localhost ~]# /mysql/3307/bin/mysqld start
Starting MySQL...
[root@localhost ~]# /mysql/3308/bin/mysqld start
Starting MySQL...
[root@localhost ~]# mysql -uroot -S /mysql/3306/socket/mysql.sock -p123456 #该方法只适合本机连接
Welcome to the MariaDB monitor. Commands end with ; or /g.
Your MariaDB connection id is 8
Server version: 10.4.21-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 statement.
MariaDB [(none)]>
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/115656.html