MySQL常用shell脚本
- Rename database 重命名数据库
#!/bin/env bash echo -e "\e[5mthe program is designed to rename database!\e[0m" read -p 'the source database: ' src read -p 'the destination database: ' dst echo -e "the source database: \e[7m$src\e[0m the destination datatabase: \e[7m$dst\e[0m do you want to continue? yes/no" read opt echo $opt if [[ $opt == "yes" ]];then mysql -e "create database if not exists $dst default character set utf8mb4 collate utf8mb4_general_ci" mysql --skip-column-names -e "select table_name from information_schema.tables where table_schema=\"$src\"" |\ while read b;do printf 'rename \e[7m%-50s\e[0m to \e[7m%-50s\e[0m' $src.$b $dst.$b echo mysql -e "rename table $src.$b to $dst.$b" done fi mysql -e "drop database $src"
- 批量重命名 character set & collation
#!/bin/bash host=172.16.10.200 user=root password='arbitrary0!' declare -a database database=(CJML_QxbEPC) function bacula() { #echo $1 mysql -h"$host" -u$user --password=$password --skip-column-names -e "show full tables from $1 where table_type = 'BASE TABLE'" 2>/dev/null | awk '{print $1}' | while read b;do #echo $b #echo -e database $1 '\t\t' table "\e[7m$b\e[0m" '\t\t'convert to character set utf8mb4 collation utf8mb4_general_ci printf '%s \e[5m%-15s\e[0m %s \e[7m%-30s\e[0m %s' database $1 table $b 'convert to character set utf8mb4 collation utf8mb4_general_ci' echo mysql -h$host -u$user --password=$password -e "set @@session.foreign_key_checks=0;alter table ${1}.$b convert to character set utf8mb4 collate utf8mb4_general_ci" 2>/dev/null; done } function main() { for b in ${database[@]};do bacula $b done } main
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律