MySQL的編譯安裝及多實例配置總結
阿新 • • 發佈:2019-04-29
pro table 安裝包 opp 錯誤 參考 配置服務 service server 編譯安裝MySQL+多實例
編譯安裝MySQL
編譯環境 | MySQL版本 |
---|---|
CentOS 7.6 | mariadb-10.2.23 |
一、安裝編譯所需要的包
yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boostdevel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssldevel libevent-devel libaio-devel -y
二、添加MySQL用戶及其家目錄
1.添加MySQL用戶
[[email protected] ~]# useradd -r -s /sbin/nologin -d /data/mysql mysql
2.為MySQL用戶添加家目錄
[[email protected] ~]# chmod 700 /data/mysql
[[email protected] ~]# chown mysql.mysql /data/mysql
三、編譯安裝MySQL
1.解壓MySQL安裝包
[[email protected] ~]# tar -xf mariadb-10.2.23.tar.gz
2.編譯安裝MySQL
此處需要註意如果編譯時中間如果出現缺包的錯誤,需要將此目錄下的CMakeCache.txt刪除後重新進行編譯。
[[email protected] ~]# cmake . -DCMAKE_INSTALL_PREFIX=/app/mysql -DMYSQL_DATADIR=/data/mysql/ -DSYSCONFDIR=/etc/mysql -DMYSQL_USER=mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITHOUT_MROONGA_STORAGE_ENGINE=1 -DWITH_DEBUG=0 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DENABLED_LOCAL_INFILE=1 -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci [[email protected] mariadb-10.2.23]# make && make install
3.創建數據庫
[[email protected] mariadb-10.2.23]# cd /app/mysql/
[[email protected] mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mysql
Installing MariaDB/MySQL system tables in ‘/data/mysql‘ ...
OK
4.放置配置文件並修改
[[email protected] mysql]# cp support-files/my-huge.cnf /etc/mysql/my.cnf
[[email protected] mysql]# sed -i ‘/\[mysqld\]/adatadir=/data/mysql‘ /etc/mysql/my.cnf
5.配置服務啟動腳本
[[email protected] mysql]# chkconfig --add mysqld
6.啟動服務
[[email protected] mysql]# service mysqld start
Starting mysqld (via systemctl): [ OK ]
7.為MySQL添加環境變量
[[email protected] mysql]# echo ‘PATH=/app/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[[email protected] mysql]# . /etc/profile.d/mysql.sh
MySQL的多實例環境搭建
在測試環境中通常會需要在一臺主機上搭建多個版本的MySQL,此處演示如何搭建多實例的MySQL。
一、為每個實例創建各自的目錄並賦予權限(此處以創建2個實例為例)
[[email protected] /]# mkdir -pv /mysql/{3306,3307}/{data,etc,socket,bin,log,pid}
[[email protected] ~]# chown -R mysql.mysql /mysql
[[email protected] ~]# tree /mysql #查看下目錄結構
/mysql
├── 3306
│?? ├── bin
│?? ├── data
│?? ├── etc
│?? ├── log
│?? ├── pid
│?? └── socket
└── 3307
├── bin
├── data
├── etc
├── log
├── pid
└── socket
二、為每個實例創建數據庫文件
[[email protected] /]# /app/mysql/scripts/mysql_install_db --user=mysql --datadir=/mysql/3306/data
[[email protected] /]# /app/mysql/scripts/mysql_install_db --user=mysql --datadir=/mysql/3307/data
三、為每個實例創建配置文件
配置文件可以參考/etc/my.cnf進行修改
[[email protected] /]# cp /etc/my.cnf /mysql/3306/etc/my.cnf
[[email protected] /]# vim /mysql/3306/etc/my.cnf #修改my.cnf,寫入配置
[mysqld] #註意需要將#!includedir /etc/my.cnf.d註釋
port=3306
datadir=/mysql/3306/data
socket=/mysql/3306/socket/mysql.sock
symbolic-links=0
[mysqld_safe]
log-error=/mysql/3306/log/mariadb.log
pid-file=/mysql/3306/pid/mariadb.pid
[[email protected] /]# cp /mysql/3306/etc/my.cnf /mysql/3307/etc/
[[email protected] /]# sed -i ‘s/3306/3307/‘ /mysql/3307/etc/my.cnf
四、為實例添加服務腳本
1.此為事先準備好的服務腳本
#!/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
2.將服務啟動腳本存放至各實例的bin目錄下,並加以修改
[[email protected] /]# vim /mysql/3306/bin/mysqld #對端口和cmd_path進行修改
port=3306
cmd_path="/app/mysql/bin"
[[email protected] /]# vim /mysql/3307/bin/mysqld #對端口和cmd_path進行修改
port=3307
cmd_path="/app/mysql/bin"
[[email protected] /]# chmod +x /mysql/3306/bin/mysqld #為服務腳本添加執行權限
[[email protected] /]# chmod +x /mysql/3307/bin/mysqld
3.關閉編譯安裝的MySQL服務
[[email protected] /]# ss -tnl |grep 3306
LISTEN 0 80 :::3306 :::*
[[email protected] /]# service mysqld stop
Stopping mysqld (via systemctl): [ OK ]
4.啟動實例1,2
[[email protected] /]# /mysql/3306/bin/mysqld start
Starting MySQL...
[[email protected] /]# /mysql/3307/bin/mysqld start
Starting MySQL...
[[email protected] /]# ss -tnl | grep 3306
LISTEN 0 80 :::3306 :::*
[[email protected] /]# ss -tnl | grep 3307
LISTEN 0 80 :::3307 :::*
5.連接數據庫
客戶端在連接多實例的MySQL時,需要指定端口號和socket文件路徑否則報錯。
[[email protected] /]# mysql -uroot -p -P3306 -S /mysql/3306/socket/mysql.sock
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.23-MariaDB Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
6.關閉多實例MySQL服務
多實例的MySQL服務在關閉時需要輸入MySQL管理員的密碼,也可以將管理員密碼存放在配置文件的變量mysql_pwd=""中
[[email protected] /]# /mysql/3306/bin/mysqld stop
Stoping MySQL...
Enter password:
MySQL的編譯安裝及多實例配置總結