首页 » linux » 正文

宝塔linux面板数据库存放路径修改脚本

宝塔linux面板数据库存放路径修改脚本

注意:该脚本仅适合宝塔linux面板使用

支持版本:宝塔linux面板全系列

功能参数:修改 还原

sh xxx.sh #修改存放路径

sh xxx.sh old #还原数据库原路径

如果你的数据库存在在系统盘的都建议你把数据库存放路径修改到数据盘下。

修改数据库路径:

a1

还原数据库路径:

a56

shell代码:

#!/bin/bash
Stack=$1
if [ "${Stack}" = "" ]; then
 Stack="new"
else
 Stack=$1
fi
mysqld_start(){
service mysqld restart
}
mysql_alter()
{
Old_path="/www/server"
clear
echo -e "\033[33mUsage: $0 {new|old}\033[0m"
echo -e "\033[33msh $0 (default:new)\033[0m"
echo -e "\033[33msh $0 old (Restore database)\033[0m"
echo "==============================="
 if [ -d /www/server/data ]; then
 read -p "Enter a new database path (for example, /home/mysql):" Route
 if [ "${Route}" == "" ]; then
 echo "New database path cannot be empty!"
 else
 echo "Copying database file to:${Route} Please be patient"
 service mysqld stop
 killall -TERM mysqld
 mkdir ${Route}
 chown -R root:mysql ${Route}
 mkdir ${Route}/var
 cp -r -p $Old_path/data/* ${Route}/var
 chown -R mysql:mysql ${Route}/var
 mv $Old_path/data $Old_path/data.old
 cp /etc/my.cnf /etc/my.cnfold
 #/etc/my/cnf
 sed -i 's#^datadir = '$Old_path/data'#datadir = '${Route}/var'#g' /etc/my.cnf
 sed -i 's#^innodb_data_home_dir = '$Old_path/data'#innodb_data_home_dir = '${Route}/var'#g' /etc/my.cnf
 sed -i 's#^innodb_log_group_home_dir = '$Old_path/data'#innodb_log_group_home_dir = '${Route}/var'#g' /etc/my.cnf
 mysqld_start
 if netstat -ntulp |grep -w 3306; then
 echo "#####################################"
 echo "Database directory changed successfully!"
 echo -e "New database file path:\033[33m${Route}/var\033[0m"
 echo "For more information visit: http://www.tieww.com"
 echo "#####################################"
 else
 echo "#####################################"
 echo -e "Prompt:\033[31mDatabase directory changes not successful\033[0m"
 echo "#####################################"
 fi
 fi
 else
 echo -e "Prompt:\033[31mSorry, I can't find the /www/server/data directory\033[0m"
 fi
}
Old_mysql()
{
if [ -d /www/server/data ]; then
echo -e "Prompt:\033[31m/www/server/data folder already exists, not allowed to restore.\033[0m"
else
service mysqld stop
killall -TERM mysqld
rm -rf /etc/my.cnf
mv /etc/my.cnfold /etc/my.cnf
mv /www/server/data.old /www/server/data
mysqld_start
if netstat -ntulp |grep -w 3306; then
echo -e "Prompt:\033[32mrecover successfully\033[0m"
echo "For more information visit: http://www.tieww.com"
else
echo -e "Prompt:\033[31mrecovery failed\033[0m"
echo "For more information visit: http://www.tieww.com"
fi
fi
}
case "${Stack}" in
 new)
 mysql_alter
 ;;
 old)
 Old_mysql
 ;;
 *)
 echo "Usage: $0 {new|old}"
 ;;
esac

脚本下载:

wget http://www.tieww.com/soft/mysql_alter.sh && sh mysql_alter.sh

如修改或还原报错,请把相关错误信息回复到留言或者发铁网维邮箱  第一时间回复 谢谢

本教程由铁网维原创出品 服务器运维官方网址http://www.tieww.com 欢迎大家多多关注本站 感谢大家的支持。

 



本文共 1 个回复

发表评论