康乐_SH

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

1.实验目的

Centos7 yum安装mariadb并实现三个实例

2.环境要求

一台系统Centos7.x主机

3.前提准备

关闭SELINUX
vim /etc/selinux/config
关闭防火墙
systemctl stop firewalld
时间同步
crontab -e
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null
*/5 * * * * /usr/sbin/ntpdate ntp3.aliyun.com &>/dev/null

4.安装mariadb

yum -y install mariadb-server

5.准备三个实例的目录

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

6.生成数据库文件

mysql_install_db --user=mysql --datadir=/mysql/3306/data
mysql_install_db --user=mysql --datadir=/mysql/3307/data
mysql_install_db --user=mysql --datadir=/mysql/3308/data

7.准备配制文件

vim /mysql/3306/etc/my.cnf
[mysqld]
port=3306
datadir=/mysql/3306/data
socket=/mysql/3306/socket/mysql.sock
log-error=/mysql/3306/log/mysql.log
pid-file=/mysql/3306/pid/mysql.pid

#重复上面步骤设置3307、3308
sed 's/3306/3307/' /mysql/3306/etc/my.cnf > /mysql/3307/etc/my.cnf
sed 's/3306/3308/' /mysql/3306/etc/my.cnf > /mysql/3308/etc/my.cnf

 

8.准备启动脚本

vim /mysql/3306/bin/mysqld
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

chmod +x /mysql/3306/bin/mysqld
#重复上述过程,分别建立3307、3308的启动脚本

9.启动服务

/mysql/3306/bin/mysqld start
/mysql/3307/bin/mysqld start
/mysql/3308/bin/mysqld start
ss -ntl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      100    127.0.0.1:25                        *:*                  
LISTEN     0      50            *:3306                      *:*                  
LISTEN     0      50            *:3307                      *:*                  
LISTEN     0      50            *:3308                      *:*                  

10.登录实例

mysql -uroot -S /mysql/3306/socket/mysql.sock   #初始不需要输入密码登录

11.修改root密码

mysqladmin -uroot -S /mysql/3306/socket/mysql.sock password '123456'
mysqladmin -uroot -S /mysql/3307/socket/mysql.sock password '123456'
mysqladmin -uroot -S /mysql/3308/socket/mysql.sock password '123456'

12.测试连接

mysql -uroot -p123456 -S /mysql/3308/socket/mysql.sock

13.开机启动

vim /etc/rc.d/rc.local
for i in {3306..3308};do /mysql/$i/bin/mysqld start;done
chmod +x /etc/rc.d/rc.local

 

posted on 2022-03-01 22:35  康乐_SH  阅读(72)  评论(0编辑  收藏  举报