mysql多实例配置启动过程
-
安装mysql(mariadb)相关程序
-
在线安装
yum -y install mariadb mariadb-server -
离线安装
下载地址(阿里巴巴提供的开源下载地址):阿里巴巴开源镜像站-OPSX镜像站-阿里云开发者社区阿里巴巴开源镜像站,免费提供Linux镜像下载服务,拥有Ubuntu、CentOS、Deepin、MongoDB、Apache、Maven、Composer等多种开源软件镜像源,此外还提供域名解析DNS、网络授时NTP等服务,致力于为互联网用户提供全面,高效和稳定的基础服务。http://developer.aliyun.com/mirror/搜索mariadb下载 'mariadb-client-10.5' dpkg -i *.deb
-
-
配置mysql(mariadb)启动配置文件my.conf
[client]
port = #端口号
socket = #socket所在路径
[mysqld]
user = #使用哪个用户
port = #端口号
socket = #socket所在路径
basedir = #表示MySQL的安装路径
datadir = #数据库所在路径
log-bin =
server-id = 6 #表示是本机的序号为1,一般来讲就是master的意思
log-error= #错误日志
[mysqld_safe]
log-error= #错误日志
pid-file= #进程ID文件 -
初始化数据库
-
mysql_install_db --basedir=/usr/local/mysql/ --datadir=/opt/mysqld/data1 --user=mysql
-
mysqld --defaults-file=*/my.cnf --initialize --basedir=/usr/local/mysql/ --datadir=/home/mysql/data3308/data
-
-
启动mysql数据库
- mysqld_safe --defaults-file=*/my.cnf &
- mysqld --defaults-file=*/my.cnf &
-
停止mysql
systemctl stop mariadb -
测试连接数据
mysql -S */*.socket -uroot -p
linux实现脚本
#!/bin/sh
set -e
datapath=$3
port=$2
if [ -z $2 ];then
printf "请输入数据库端口号 \n"
printf "Usage: /data/${port}/mysql {start|stop|restart} {port} \n"
exit
fi
if [ -z ${datapath} ];then
datapath=/data/${port}
else
datapath=${datapath}\${port}
fi
printf "数据库data路径: \n"
echo ${datapath}
mysql_user="root"
cmdPath="/usr/bin"
mysql_sock="${datapath}/mysql.sock"
mysqld_pid_file_path=${datapath}/${port}.pid
myconf=${datapath}/my.cnf
logPath=${datapath}/log
start(){
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
/bin/sh ${cmdPath}/mysqld_safe --defaults-file=${myconf} --pid-file=$mysqld_pid_file_path 2>&1 > /dev/null &
sleep 3
else
printf "MySQL is running...\n"
exit 1
fi
}
stop(){
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit 1
else
printf "Stoping MySQL...\n"
mysqld_pid=`cat "$mysqld_pid_file_path"`
if (kill -0 $mysqld_pid 2>/dev/null)
then
kill $mysqld_pid
sleep 2
fi
fi
}
initdb(){
printf "数据库初始化开始 \n"
# 创建数据库相关文件目录和配置
if [ ! -d ${datapath} ];then
mkdir ${datapath}
fi
# 设置my.conf配置文件
if [ ! -f ${myconf} ];then
touch ${myconf}
echo [client] >>${myconf}
echo port = ${port} >>${myconf}
echo socket = ${mysql_sock} >>${myconf}
echo [mysqld] >>${myconf}
echo user = mysql >>${myconf}
echo port = ${port} >>${myconf}
echo socket = ${mysql_sock} >>${myconf}
echo basedir = /usr >>${myconf}
echo datadir = ${datapath} >>${myconf}
echo log-bin = ${datapath}/mysql-bin >>${myconf}
echo server-id = 6 >>${myconf}
echo log-error=${logPath}/mysqlclient_${port}.log >>${myconf}
echo [mysqld_safe] >>${myconf}
echo log-error=${logPath}/mysqlService_${port}.err >>${myconf}
echo pid-file=${datapath}/mysqld.pid >>${myconf}
fi
# 初始化数据库信息
printf "数据库初创建 \n"
${cmdPath}/mysql_install_db --basedir=/usr --datadir=${datapath} --user=mysql
printf "数据库密码初始化 \n"
# 设置数据库密码
${cmdPath}/mysqladmin -S ${mysql_sock} -uroot -password 'root'
# 设置远程连接属性
printf "数据库初始化完成 \n"
}
restart(){
printf "Restarting MySQL...\n"
stop
sleep 2
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
initdb)
initdb
;;
*)
printf "Usage: /data/${port}/mysql {start|stop|restart|initdb} {port} \n"
esac
参考资料
以上脚本是根据一片帖子进行修改的,增加了数据库的初始化和my.conf的配置过程,但是由于后期无法找到相关帖子,特此说明!!!!!