shell 一键改mysql数据库名
需求: 我有一个数据库叫shoppping , 想改成为shopping
下面是执行shell代码
使用步骤:
1) 修改代码并将下面代码写入一个文件,例mv_dbname
2) vi/vim 编辑器设置 set ff=unix 保存
3)给权限 chomd a+x mv_dbname
4)执行 ./mv_dbname
需要修改的地方:
1) mysql执行路径 mysqlconn 这行
2) 用户名, 密码 mysqlconn 这行
扫描二维码关注公众号,回复:
3814390 查看本文章
3) 原数据库名 olddb
4) 期待的数据库名 newdb
source /etc/profile
source ~/.bash_profile
set -o nounset
mysqlconn="/usr/local/mysql-5.7.22/bin/mysql -uroot -proot"
olddb="shoppping"
newdb="shopping"
$mysqlconn -e "drop database if exists ${newdb};create database ${newdb};"
tables=$($mysqlconn -N -e "select table_name from information_schema.tables where table_schema='${olddb}'")
for name in $tables;do
$mysqlconn -e "rename table ${olddb}.${name} to ${newdb}.${name}"
done
$mysqlconn -e "drop database ${olddb}"
/END