当前位置: 首页 > article >正文

shell脚本--MySQL简单调用

实现功能

数据库的创建,数据表的创建已经实现

创建用户

删除数据库,

删除库下的某个表,

删除某个用户

暂无

查看所有的数据库,

查看某个库下的所有数据表,

查看某个表的结构,

查看某个库的某个表中的所有数据,

查看所有用户

后续计划

准备添加向表中插入数据,包含一次插入多条数据,一次对某列或者多列插入数据

修改某表中的数据

删除表中数据

代码展示

#!/bin/bash
#登录mysql
menu() {
echo -e "\e[32m\t1--查看所有可用数据库\e[0m"
echo -e "\e[32m\t2--查看某个数据库下所有的数据表\e[0m"
echo -e "\e[32m\t3--查看某个库下某个表的结构\e[0m"
echo -e "\e[32m\t4--查看某个库下的某个表中的所有数据\e[0m"
echo -e "\e[32m\t5--创建一个数据库\e[0m"
echo -e "\e[32m\t6--在某个数据库下创建一个数据表\e[0m"
echo -e "\e[32m\t7--在表中插入数据\e[0m"
echo -e "\e[32m\t8--创建一个用户\e[0m"
echo -e "\e[32m\t9--删除数据库\e[0m"
echo -e "\e[32m\t10--删除某个数据库下的数据表\e[0m"
echo -e "\e[32m\t11--删除某个用户信息\e[0m"
echo -e "\e[32m\t12--查看所有用户\e[0m"
echo -e "\e[32m\t13--退出\e[0m"
echo -e "\e[32m\t14--待补充\e[0m"
echo -e "\e[32m\t15--待补充\e[0m"
}
test_db(){
mysql -u${mysql_user:-root} -p"${mysql_password}" -h ${mysql_host:-localhost} -P${mysql_port:-3306} -e "exit" &> /dev/null
if [ $? -eq 0 ];then
  echo -e  "\e[32m登录成功,当前数据库信息正确,数据库可以使用\e[0m"
  echo -e  "\e[35m<-------------------------------------------------------->\e[0m"
else
  echo  -e "\e[31m登录失败,数据库用户或密码错误,请重新进行信息收集\e[0m"
  exit
fi
}
useage() {
echo -e "\e[33m请输入选项1-15\e[0m"
}
date() {
sleep 0.5
}
get_message() {
  echo -e "\e[33m请先进行数据库登录!!!\e[0m"
  read -p "请输入你要使用的用户(默认为root):" mysql_user
  mysql_user=${mysql_user:-root}
  read -sp "请输入用户密码:"  mysql_password
  echo ""
  read -p "请输入你要登录的主机(默认为localhost)" mysql_host
  mysql_host=${mysql_host:-localhost}
  read -p "请输入你要登录的服务端口号(默认为3306)" mysql_port
  mysql_port=${mysql_port:-3306}
}
show_dbs() {
echo -e "\e[32m所有数据库信息如下所示:\e[0m"
date
mysql -u${mysql_user} -p"${mysql_password}" -h ${mysql_host} -P${mysql_port} -e "show databases"  2> /dev/null
}
show_tbs() {
read -p "输入你要查看的数据库名(默认为mysql):" db_name
mysql -u${mysql_user} -p"${mysql_password}" -h ${mysql_host} -P${mysql_port} -e "use ${db_name:-mysql} ; show tables" 
}
show_tb_desc() {
read -p "输入你要查看的数据库(默认为mysql)" db_name 
read -p "输入你要查看的数据表(默认为user)" tb_name
message=${db_name:-mysql}.${tb_name:-user}
mysql -u${mysql_user} -p"${mysql_password}" -h ${mysql_host} -P${mysql_port} -e "desc  $message"
}
select_datas() {
read -p "输入你要查看的数据库(默认为mysql)" db_name 
read -p "输入你要查看的数据表(默认为user)" tb_name
message=${db_name:-mysql}.${tb_name:-user}
mysql -u${mysql_user} -p"${mysql_password}" -h ${mysql_host} -P${mysql_port} -e "select * from $message"
read -p "输入g展示旋转后的数据,输入其他跳过" choice
if [ $choice == 'g' ];then 
mysql -u${mysql_user} -p"${mysql_password}" -h ${mysql_host} -P${mysql_port} -e "select * from $message\G"    
else 
echo -e "\e[33m跳过\e[0m"
fi
}
create_db() {
read -p "输入你要创建的数据库名(默认为dbs)" db_name
read -p "输入你要创建的数据库默认字符集(默认utf8)" character_name
character_name=${character_name:-utf8}
mysql -u${mysql_user} -p"${mysql_password}" -h ${mysql_host} -P${mysql_port} -e "create database if not exists ${db_name:-dbs} default character set $character_name"    
if [ $? -eq 0 ];then 
  echo -e "\e[32m数据库${db_name}创建成功,使用的字符集为"$character_name"\e[0m"
  date
  echo -e  "\e[33m也有可能该数据库已存在,推荐先使用功能9将数据库删除在进行创建\e[0m"
fi
}
create_tb() {
echo "某个库下创建数据表"
read -p "输入你要创建的表名" tb_name
read -p "输入你的创建的表基于的库名" db_name
read -p "输入你的创建的表的默认字符集(默认为utf8)" tb_type
table_message=${db_name:-mysql}.${tb_name:-user}
echo $table_message
read -p "输入你要创建的字段个数" columns_number
for (( i=1; i<=$columns_number; i++ ));
  do
    read -p "输入第${i}列的字段名" column_name
    read -p "输入第${i}列的数据类型" column_type
    read -p "输入第${i}列的约束\n(默认为default null)" column_constraint
    column_constraint=${column_constraint:-default null}
    if [ $i -ne $columns_number ] ;then
       column_message="$column_name $column_type $column_constraint, "
       column_array[$i]=$column_message
    else
       column_message="$column_name $column_type $column_constraint"
       column_array[$i]=$column_message
    fi
  done
echo "所有列的信息为${column_array[@]}"
all_columns="(${column_array[@]})"
mysql -u${mysql_user} -p"${mysql_password}" -h ${mysql_host} -P${mysql_port} -e "create table if not exists $table_message $all_columns;" &> /dev/null && echo -e "\e[32m数据表${table_message}创建完成\e[0m" || echo -e "\e[31m数据表${table_message}创建失败\e[0m"
}
insert_data() {
read -p "输入插入表所属库名(默认为mysql)" db_name
read -p "输入你要插入数据的表名(默认为user)" tb_name
tb_message=${db_name:-mysql}.${tb_name:-user}
array=`mysql -u${mysql_user} "-p${mysql_password}" -h ${mysql_host} -P${mysql_port} -e "desc ${tb_message}"  | grep -v "Extra" | awk '{print $1,$2}'`
flag=0
for i in $array;
do flag=$(( $flag + 1 ))
   if [ $(($flag % 2)) -eq 1 ];then
       echo -e  "\e[35m第$(($(($flag + 1 )) / 2 ))个字段名称为$i\e[0m"
   else 
       echo -e  "\e[35m第$(($flag / 2))个字段类型为$i\e[0m"
   fi
done
#数据表中的字段已经可以进行收集,剩下数据插入部分后续进行补充
}
show_users() {
echo "当前已经存在的用户"
list=`mysql -u${mysql_user} -p"${mysql_password}" -h ${mysql_host} -P${mysql_port} -e 'select * from mysql.user\G' | egrep "User|Host" | awk -F":" '{print $NF}'`
number=`mysql -u${mysql_user} -p"${mysql_password}" -h ${mysql_host} -P${mysql_port} -e 'select * from mysql.user\G'  | egrep "User|Host" | awk -F":" '{print $NF}' | wc -l`
for (( i=1; i<=$number; i++ ));
do
   flag=$((i%2))
   if [ $flag -eq 1  ] ;then
       host=`echo $list | cut -d" " -f $i`
   else
       user=`echo $list | cut -d" " -f $i`
       message=$user@$host
       echo $message
   fi
done
}
create_user() {
show_users
read -p "输入你要创建的用户名" user_name
read -p "输入用户可登录的主机(默认为localhost)" host_name 
read -sp "输入你要设置的密码(必须符合密码复杂策略,不输入则为默认密码)" password
echo ""
host_name=\'${host_name:-localhost}\'
user_message=$user_name@$host_name
password=\'${password:-'ABCDE2002@'}\'
mysql -u${mysql_user} -p"${mysql_password}" -h ${mysql_host} -P${mysql_port} -e "create user ${user_message} identified by ${password};" &> /dev/null && echo -e "\e[32m用户${user_message}创建成功\e[0m"  || echo -e  "\e[31m用户${user_message}创建失败\e[0m"   
}
drop_db() {
read -p "输入你要删除的数据库" db_name 
mysql -u${mysql_user} -p"${mysql_password}" -h ${mysql_host} -P${mysql_port} -e "drop database if exists $db_name;" &> /dev/null
if [ $? -eq 0 ];then
   echo -e "\e[32m数据库${db_name}成功删除\e[0m"
   date
   echo -e "\e[33m数据库${db_name}也可能本身就不存在\e[0m" 
fi
}
drop_tb() {
read -p "输入你要删除的数据表所在的数据库" db_name
read -p "输入你要删除的数据表" tb_name
drop_message=$db_name.$tb_name
mysql -u${mysql_user} -p"${mysql_password}" -h ${mysql_host} -P${mysql_port} -e "drop table if exists $drop_message;" &> /dev/null
if [ $? -eq 0 ];then
   echo -e "\e[32m数据表${drop_message}成功删除\e[0m"
   date
   echo -e "\e[33m数据表${drop_message}也可能本身就不存在\e[0m" 
fi
}
drop_user() {
show_users
read -p "输入你要删除的用户名" user_name
read -p "输入你要删除的用户主机(默认为localhost)" host_name
host_name=\'${host_name:-localhost}\'
user_message=$user_name@$host_name
echo $message
mysql -u${mysql_user} -p"${mysql_password}" -h ${mysql_host} -P${mysql_port} -e "drop user if exists $user_message;" &> /dev/null && echo -e "\e[32m用户${user_message}删除成功\e[0m"
}
main() {
get_message
test_db
while true;do
  menu
  read -p "输入你要使用的功能" choice
  case $choice in 
    1) 
      show_dbs
      date;;
    2) 
      show_tbs
      date;;
    3) 
      show_tb_desc
      date;;
    4) 
      select_datas
      date;;
    5) 
      create_db
      date;;
    6) 
      create_tb
      date;;
    7) 
      insert_data
      date;;
    8) 
      create_user
      date;;
    9) 
      drop_db
      date;;
    10) 
      drop_tb
      date;;
    11) 
      drop_user
      date;;
    12) 
      show_users
      date;;
    13) 
      break;;
    14) 
      echo "待补充";;
    15) 
      echo "待补充"
      date;;
    *)
      useage
      date;;
  esac 
done
}
main


http://www.kler.cn/a/614890.html

相关文章:

  • UI产品经理基础(四):用价值链视角来分析项目需求
  • JVM考古现场(十三):混沌重启——从量子永生到宇宙热寂的终极编译
  • 批量合并 PDF 文档,支持合并成单个文档,也支持按文件夹合并 PDF 文档
  • Spring AI Alibaba 实战:集成 OpenManus 实现智能体应用开发
  • 软件测试之接口测试
  • 【最后203篇系列】025 FastAPI+Celery
  • 企业级知识库建设:自建与开源产品集成的全景解析 —— 产品经理、CTO 与 CDO 的深度对话
  • python纯终端实现图片查看器(全彩)(windows)
  • 数据化管理(一)---什么是数据化管理
  • 【Linux】常见信号 + 进程和作业
  • 【弹性计算】异构计算云服务和 AI 加速器(五):EAIS AI 加速器
  • Apache Iceberg 解析,一文了解Iceberg定义、应用及未来发展
  • 【SDMs分析1】基于ENMTools R包的生态位分化分析和图像绘制(identity.test())
  • 全局思维与系统思考
  • Vue学习笔记集--computed
  • Jenkins插件安装失败如何解决
  • Spring Cloud Consul使用指南
  • 【现代深度学习技术】现代卷积神经网络04:含并行连接的网络(GoogLeNet)
  • 指针和引用
  • 通过一个led点灯的demo来熟悉openharmony驱动编写的过程(附带hdf详细调用过程)