Jace06

shell 下执行mysql 命令

在shell开发中,很多时候我们需要操作mysql数据库(比如:查询数据、导出数据等),但是我们又无法进入mysql命令行的环境,就需要在shell环境中模拟mysql的环境,使用mysql相关命令。

法一:

mysql -uuser -ppasswd -e"insert LogTable values(...)" 

优点:语句简单

缺点:支持的sql相对简单
 
法二:
先写一个SQL脚本:如insert.sql
insert into shiyan select * from shiyan02;

然后写一个shell脚本:如insert.sh

use test;
source shiyan.sql

接着执行命令:

cat shiyan.sh | mysql --user=root -ppassword  

优点:支持复杂的sql脚本

缺点:无法处理异常

 法三:

新建shell脚本:如insert.sh

#!/bin/bash  
mysql -u* -h* -p* <<EOF  
    Your SQL script.  
EOF 

 

法四:

mysql -uroot -ppassword < shiyan.sql

 

 

 

 

 

 

 

 

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-10-31
  • 2021-06-21
  • 2022-01-02
  • 2021-12-21
  • 2022-01-02
  • 2022-01-11
  • 2022-01-18
猜你喜欢
  • 2021-11-12
  • 2021-11-03
  • 2021-09-19
  • 2021-08-24
  • 2022-12-23
相关资源
相似解决方案