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

这样看起来跟系统服务一样!


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