目录路径请检查修改

一键安装脚本

#!/bin/bash
############################################
#时间 2019/12/17
#这是一个备份脚本
#老王 QQ:48211701 MAIL:13875867252@139.com
#博客 www.aomtop.com
#文档 doc.aomtop.com

############################################

#变量参数
bin=/opt/mysql/bin
mycnf=/home/xlqywk/scripts/mysqlconf
data=/opt/mydata

for port in 3307 3308
do


echo "创建数据库目录"
mkdir -p $data/$port/data

echo "复制多实例主配置文件。。。。。。"
/usr/bin/cp $mycnf/my.cnf $data/$port/

echo "替换配置文件端口和路径"
sed -i "s#3306#$port#g" $data/$port/my.cnf
sed -i "s#server-id = 1#server-id = `echo "$port"|cut -c 4`#g" $data/$port/my.cnf
chown -R mysql.mysql $data/$port


echo "MySQL加入环境环境变量文件"
echo  "export  PATH=$PATH:/opt/mysql/bin:/opt/mysql/lib:/opt/mysql/scripts"  >>/etc/profile

source  /etc/profile
if [[ $? -eq 0 ]]; then
    echo "刷新环境变量成功!"
else
    echo "刷新环境变量失败!"
fi    

echo "初始化数据库实例"
mysql_install_db --basedir=/opt/mysql --defaults-file=$data/$port/my.cnf --datadir=$data/$port/data --user=mysql
chown -R mysql.mysql $data/$port

echo "启动数据库实例"    
/opt/mysql/bin/mysqld_safe --defaults-file=$data/$port/my.cnf &
sleep 5 

echo "修改数据库root密码"
$bin/mysqladmin -u root -S $data/$port/mysql.sock password 'xlqywk'



echo "检测是否安装启动成功"
netstat -lntup|grep $port
mysql -uroot -pxlqywk -S $data/$port/mysql.sock
mysql -uroot -pxlqywk -S $data/$port/mysql.sock -e "show databases;"

done

my.cnf配置文件模板

[client]
#port = 3306
#socket = /tmp/mysql.sock
#default-character-set = utf8

[mysql]
#default-character-set = utf8

[mysqld]
server-id = 1
port = 3306
basedir = /opt/mysql
datadir = /opt/mydata/3306/data
socket  = /opt/mydata/3306/mysql.sock
slow_query_log_file = /opt/mydata/3306/slow.log
log-error = /opt/mydata/3306/error.log
log-bin = /opt/mydata/3306/mysql-bin
sync_binlog = 1
binlog_format = row
transaction_isolation = REPEATABLE-READ
innodb_buffer_pool_size = 100m


[mysqld_multi]
mysqld=/opt/mysql/bin/mysqld_safe
mysqladmin=/opt/mysql/bin/mysqladmin


[mysqldump]
quick
max_allowed_packet = 32M

mysql启动脚本

#!/bin/bash
# chkconfig: 2345 64 36 #配置系统自启动
# description: A very fast and reliable SQL database engine.
##############################################################
# File Name: mysqld
# Version: V1.0
##############################################################
#引入系统函数库
. /etc/init.d/functions

#基础路径定义
basedir='/application/mysql'
bindir='/application/mysql/bin'
lockdir='/var/lock/subsys'                                       
lock_file_path="$lockdir/mysql"
mysqld_pid_file_path='$basedir/data/`uname -n`.pid'

#成功提示函数
log_success_msg(){
    #action为特殊的提示函数,$@为所有参数。
    action "SUCCESS! $@" /bin/true
}
#失败提示函数
log_failure_msg(){
    action  "ERROR! $@" /bin/false
  }

#mysql启动函数
start(){
    echo $"Starting MySQL"
    #测试mysqld_safe是否可执行
    if test -x $bindir/mysqld_safe
    then
        #后台执行启动mysql命令
        $bindir/mysqld_safe &>/dev/null &
        #获取返回值
        retval=$?
        #判断返回值是否为0
        if [ $retval -eq 0 ]
        then
            #调用成功提示函数。
            log_success_msg "mysql Startup"
            if test -w "$lockdir" #判断锁目录是否可写。
            then
                touch "$lock_file_path" #创建锁文件。
            fi
            return $retval #给返回值是专业的表现。
        else
            log_failure_msg "MySQL Startup" #调用失败函数提示。
            return $retval
        fi
    else
        log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
    fi
}
#停止MySQL函数。
stop(){
    #判断mysql pid file大小是否为0。
    if test -s "$mysqld_pid_file_path"
    then
        #读取pidfile
        mysqld_pid=`cat "$mysqld_pid_file_path"`
        #判断mysql pid对应的进程是否存在。
        if (kill -0 $mysqld_pid 2>/dev/null)
        then
            echo $"Shutting down MySQL"
            kill $mysqld_pid #停止MySQL命令。
            retval=$?
            if [ $retval -eq 0 ]
            then
                log_success_msg "MySQL Stop" #调用停止成功函数。
                if test -f "$lock_file_path"
                then
                    rm -f "$lock_file_path"  #删除锁文件。
                fi
                return $retval
            else
                log_failure_msg "MySQL Stop."
                return $retval
            fi
        else
            log_failure_msg "MySQL server process mysqld_pid is not running!"
            rm "$mysqld_pid_file_path"
        fi  
    else
        log_failure_msg "MySQL server PID file is null or not exist!"
    fi
}
#接收传参判断并执行相应函数。
case "$1" in
    start)
        start
        retval=$?
        ;;
    stop)
        stop
        retval=$?
        ;;
    restart)
        stop
        sleep 2 #这里很重要,要休息一下。
        start
        retval=$?
        ;;
    *)
        echo $"Usage:$0 {start|stop|restart}"
        exit 2
esac
exit $retval    #执行脚本后,有返回值才更专业。
文档更新时间: 2019-12-18 14:26   作者:老王