mariadb多實例安裝
.=================================================================
1. yum 安裝mariadb-server包
#yum install mariadb-server
2. 創建多實例對應的目錄結構
#mkdir /mysql/{3306,3307,3308}/{data,etc,socket,log,bin,pid} -pv
#tree /mysql/
/mysql/
├── 3306
│ ├── bin
│ ├── data
│ ├── etc
│ ├── log
│ ├── pid
│ └── socket
│ ├── bin
│ ├── data
│ ├── etc
│ ├── log
│ ├── pid
│ └── socket
└── 3308
├── bin
├── data
├── etc
├── log
├── pid
└── socket
#chown -R mysql.mysql /mysql
3. 創建多實例的數據庫文件
#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
4. 創建對應配置文件
#cp /etc/my.cnf /mysql/3306/etc
#vim /mysql/3306/etc/my.cnf
[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
#cp /mysql/3306/etc/my.cnf /mysql/3307/etc/ 修改
#vim /mysql/3307/etc/my.cnf
:%s/3306/3307/
#vim /mysql/3308/etc/my.cnf
:%s/3306/3308/
或者用sed命令改也可以
#sed -i ‘s/3306/3307/‘ /mysql/3307/etc/my.cnf
#sed -i ‘s/3306/3308/‘ /mysql/3308/etc/my.cnf
5. 準備各實例的啟動腳本
#mysqladmin shutdown //因為正好要開mariadb多實例, 可以先把yum 安裝的mariadb數據庫先停了
思路:先創建3306實例的啟動腳本,然後分別copy到3307和3308 bin目錄下
#cd /mysql/3306/bin/
#vim mysqld
#!/bin/bash
port=3306
mysql_user="root"
mysql_pwd=""
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
#cp mysqld ../../3307/bin/
#cp mysqld ../../3308/bin/
#vim ../../3308/bin/mysqld
port=3308
#vim ../../3307/bin/mysqld
port=3307
#chmod +x mysqld
#chmod +x ../../3307/bin/mysqld
#chmod +x ../../3308/bin/mysqld
6. 啟動和關閉實例
#/mysql/3306/bin/mysqld start
#/mysql/3307/bin/mysqld start
#/mysql/3308/bin/mysqld start
#/mysql/3306/bin/mysqld stop
#/mysql/3307/bin/mysqld stop
#/mysql/3308/bin/mysqld stop
7. 測試連接
#mysql -S /mysql/3306/socket/mysql.sock
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.60-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)]>
[root@centos7mini bin]#mysql -S /mysql/3307/socket/mysql.sock
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.60-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)]> show variables like ‘port‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3307 |
+---------------+-------+
1 row in set (0.00 sec)
#mysql -S /mysql/3308/socket/mysql.sock
8. 安全加固
#mysqladmin -S /mysql/3306/socket/mysql.sock password ‘root‘ //加登陸數據庫口令
#mysql -S /mysql/3306/socket/mysql.sock -uroot -proot //用口令登陸
#mysqladmin -S /mysql/3307/socket/mysql.sock password ‘root‘
#mysql -S /mysql/3307/socket/mysql.sock -uroot -proot
#mysqladmin -S /mysql/3308/socket/mysql.sock password ‘root‘
#mysql -S /mysql/3308/socket/mysql.sock -uroot -proot
9. 配置免密登陸
編輯3306,3307,3308啟動腳本,加上對應口令:root 這樣啟動關閉數據庫就可以不用輸入密碼了
#vim /mysql/3306/bin/mysqld
mysql_pwd="root"
#vim /mysql/3307/bin/mysqld
mysql_pwd="root"
#vim /mysql/3308/bin/mysqld
mysql_pwd="root"
mariadb多實例安裝