MariaDB編譯安裝並實現多實例

一、環境準備

centos7.6最小化安裝

https://downloads.mariadb.org   官網下載地址

https://downloads.mariadb.org/interstitial/mariadb-10.2.25/bintar-linux-x86_64/mariadb-10.2.25-linux-x86_64.tar.gz/  二進制包


二、安裝

1、準備安裝包

[root@localhost ~]#ls
anaconda-ks.cfg  mariadb-10.2.25-linux-x86_64.tar.gz  original-ks.cfg
[root@localhost ~]#tar xf mariadb-10.2.25-linux-x86_64.tar.gz -C /usr/local/   
                   #官網提供的二進制包編譯的路徑是/usr/local,所以此處不可修改

2、準備用戶、數據目錄,可以考慮使用邏輯卷

[root@localhost ~]#useradd -r -s /sbin/nologin -d /data/mysql mysql
[root@localhost ~]#mkdir /data/mysql
[root@localhost ~]#chown -R mysql.mysql /data/mysql
[root@localhost ~]#cd /usr/local/
[root@localhost local]#ln -s mariadb-10.2.25-linux-x86_64/ mysql  
[root@localhost local]#chown -R mysql.mysql mysql/

4、準備配置文件

[root@localhost local]#mkdir /etc/mysql
[root@localhost local]#cp mariadb-10.2.25-linux-x86_64/support-files/my-huge.cnf   /etc/mysql/my.cnf
[root@localhost local]#vi /etc/mysql/my.cnf    #添加數據目錄的路徑

image.png

5、初始化

[root@localhost mariadb-10.2.25-linux-x86_64]#./scripts/mysql_install_db  --datadir=/data/mysql --user=mysql
[root@localhost mariadb-10.2.25-linux-x86_64]#yum install libaio   #安裝時有個報錯安裝該包即可


image.png


image.png

5、準備服務腳本,並啓動服務,安全初始化

[root@localhost mariadb-10.2.25]#cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld

[root@localhost mariadb-10.2.25-linux-x86_64]#chkconfig --add mysqld

[root@localhost mariadb-10.2.25-linux-x86_64]#chkconfig --list mysqld


image.png

/usr/local/mysql/bin/mysql_secure_installation
分別設置:數據庫管理員root口令
          禁止root遠程登錄
          刪除anonymous用戶帳號
          刪除test數據庫

6、配置環境變量

[root@localhost mariadb-10.2.25-linux-x86_64]#echo "PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mariadb10.2.sh

[root@localhost mariadb-10.2.25-linux-x86_64]#. /etc/profile.d/mariadb10.2.sh

[root@localhost mariadb-10.2.25-linux-x86_64]#echo $PATH

/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

三、實現多實例

1、建立實例目錄結構

mkdir -pv /www/mysql/{3306,3307,3308}/{data,etc,socket,bin,log,pid}
chown -R mysql.mysql /www/mysql/

image.png

2、分別爲3個MariaDB實例創建初始化數據

cd /usr/local/mariadb-10.2.25-linux-x86_64/
./scripts/mysql_install_db  --datadir=/www/mysql/3306/data/ --user=mysql
./scripts/mysql_install_db  --datadir=/www/mysql/3307/data/ --user=mysql
./scripts/mysql_install_db  --datadir=/www/mysql/3308/data/ --user=mysql

3、準備配置文件

cp  /etc/my.cnf      /www/mysql/3306/etc/my.cnf
vi    /www/mysql/3306/etc/my.cnf

image.png

cp /www/mysql/3306/etc/my.cnf  /www/mysql/3307/etc/my.cnf
sed -i 's/3306/3307/' /www/mysql/3307/etc/my.cnf
cp /www/mysql/3306/etc/my.cnf /www/mysql/3308/etc/my.cnf
sed -i 's/3306/3308/' /www/mysql/3308/etc/my.cnf

到此已經可以使用

/usr/local/mysql/bin/mysqld_safe --defaults-file=/www/mysql/3307/etc/my.cnf  &> /dev/null &   #啓動服務
mysql -S /www/mysql/3307/socket/mysql.sock  #啓動服務後連接
/usr/local/mysql/bin/mysqladmin -uroot -p -S /www/mysql/3307/socket/mysql.sock  shutdown     #關閉服務

image.png

只是每次啓動停止服務都要輸入很長的命令,可以編寫一個腳本來實現管理

vi  /www/mysql/3306/bin/mysqld

#!/bin/bash
port=3306
mysql_user="root"
#mysql_pwd=""                                                                           
cmd_path="/usr/local/mysql/bin"
mysql_basedir="/www/mysql"
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"
. /etc/init.d/functions
function_start_mysql()
{
    if [ ! -e "$mysql_sock" ];then
      printf "Starting MySQL...`action`\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 /www/mysql/3306/bin/mysqld /www/mysql/3307/bin/

sed -i 's/3306/3307/' /www/mysql/3307/bin/mysqld  


cp /www/mysql/3306/bin/mysqld /www/mysql/3308/bin/

sed -i 's/3306/3308/' /www/mysql/3308/bin/mysqld

啓動服務成功監聽


image.png

也可以將腳本放在/etc/init.d/用service命令來管理

cp /www/mysql/3307/bin/mysqld /etc/init.d/mysqld3307

image.png

這樣看起來跟系統服務一樣!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章