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

[复制链接] |主动推送
查看28 | 回复0 | 2024-9-11 18:06:05 | 显示全部楼层 |阅读模式
宝塔linux面板数据库存放路径修改脚本
注意:该脚本仅适合宝塔linux面板使用
支持版本:宝塔linux面板全系列
功能参数:修改 还原
sh xxx.sh #修改存放路径
sh xxx.sh old #还原数据库原路径
如果你的数据库存在在系统盘的都建议你把数据库存放路径修改到数据盘下。
修改数据库路径:
160855lhwhgze79h8nlzhq.png
还原数据库路径:
160856rqkyxytyv5f5vufk.png
shell代码:
#!/bin/bashStack=$1if [ "${Stack}" = "" ]; then Stack="new"else Stack=$1fimysqld_start(){service mysqld restart}mysql_alter(){Old_path="/www/server"clearecho -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 ]; thenecho -e "Prompt:\033[31m/www/server/data folder already exists, not allowed to restore.\033[0m"elseservice mysqld stopkillall -TERM mysqldrm -rf /etc/my.cnfmv /etc/my.cnfold /etc/my.cnfmv /www/server/data.old /www/server/datamysqld_startif netstat -ntulp |grep -w 3306; thenecho -e "Prompt:\033[32mrecover successfully\033[0m"echo "For more information visit: http://www.tieww.com"elseecho -e "Prompt:\033[31mrecovery failed\033[0m"echo "For more information visit: http://www.tieww.com"fifi}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
如修改或还原报错,请把相关错误信息回复到留言或者发铁网维邮箱  第一时间回复 谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则