mysql数据库改名,官方没有直接修改数据库名称的命令 只有通过修改表名方式实现
#!/bin/bash
mysqlconn="mysql -uroot -p123456"
需要修改的数据库名
olddb="test1"
# 修改后的数据库名
newdb="test2"
# 创建新数据库
$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}"
执行上述shell脚本 chmod +x ./test.sh #使脚本具有执行权限 ./test.sh #执行脚本
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有