shell脚本中导入mysql数据&&执行mysql语句
来源:互联网 发布:mac空间不够用 编辑:程序博客网 时间:2024/06/10 03:37
前言
在线下做mysql数据分析的时候,会遇到执行shell脚本里导入sql文件到mysql数据库里或者连接mysql执行指定sql语句的情况,这里介绍一下我采用的方法
导入sql文件到mysql数据库
示例代码
- #变量定义
- sqlname="test.sql"
- dir="/sdb2/backup/mysql_db_backup/backup/databases"
- host="127.0.0.1"
- user="root"
- passwd="123456"
- dbname="test"
- #导入sql文件到指定数据库
- mysql -h$host -u$user -p$passwd $dbname < $dir/$sqlname
关键点
"<"运算符的使用
执行指定的sql语句
mysql的-e参数
参数解释
- --execute=statement, -e statement
- xecute the statement and quit. The default output format is like that produced with --batch
- --silent, -s
- Silent mode. Produce less output. This option can be given multiple times to produce less and less output.
示例代码
- select_sql="select count(distinct id) from tb_test"
- num=$(mysql -s -h$host -u$user -p$passwd $dbname -e "$register_sql")
注意:
- -s参数的使用是减少查询字段的输出(ps:我这里只需要查询的结果值,并不需要查询的字段名,不加-s参数会输出查询的字段名)
管道运算符
- echo "select count(distinct id) from tb_test" | mysql -h$host -u$user -p$passwd $dbname
mysqldump导出mysql数据
导出指定条件的数据库
命令格式
- mysqldump -u用户名 -p密码 -h主机 数据库名 表名 --where "sql语句"> 路径
示例代码
- #!/bin/bash
- #变量定义
- host="127.0.0.1"
- user="root"
- passwd="123456"
- dbname="test"
- tablename="tb_test"
- mysqldump -u$user -p$passwd -h$host $dbname $tablename --where "id > 1 and id < 1000" > 1.sql
导出一个数据结构(无数据)
参数
- --no-data, -d
- Do not write any table row information (that is, do not dump table contents). This is useful if you want to dump only the CREATE TABLE statement for the table (for example, to create an empty copy of the table by loading the dump file).
命令格式
- mysqldump -u用户名 -p密码 -h主机 数据库名 -d > 路径
示例代码
- #!/bin/bash
- #变量定义
- host="127.0.0.1"
- user="root"
- passwd="123456"
- dbname="test"
- mysqldump -u$user -p$passwd -h$host $dbname -d > 2.sql
参考链接
http://blog.linezing.com/2012/02/mysql%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E6%95%B0%E6%8D%AE%E6%96%B9%E6%B3%95
0 0
- shell脚本中导入mysql数据&&执行mysql语句
- shell脚本中导入mysql数据&&执行mysql语句
- shell脚本中导入mysql数据&&执行mysql语句
- shell脚本中执行mysql语句
- Shell 脚本中执行mysql语句
- Shell脚本中执行mysql语句
- shell脚本执行mysql语句
- Shell脚本中执行sql语句操作mysql
- Shell脚本中执行sql语句,操作mysql数据库
- Shell脚本中执行sql语句操作mysql
- 用shell脚本执行mysql语句
- shell执行mysql数据导入导出
- shell 执行mysql语句
- 在SHELL中执行MYSQL语句
- 在SHELL中执行MYSQL语句
- shell中执行mysql语句的方法
- MySQL 执行.sql文件导入数据和执行sql语句
- Shell脚本中执行sql语句操作mysql的5种方法
- 基于ip san的iscsi操作实现过程
- ASP.NET--Request和Response的区别
- 《老罗的Android之旅》导读PPT
- java 5线程池技术
- 杭电ACM 2029 Palindromes _easy version
- shell脚本中导入mysql数据&&执行mysql语句
- bool 类型数据比较的问题
- 函数练习题
- 第二十一天 慵懒的投射在JDBC上的暖阳 —Hibernate的使用(三)
- hdu 1011 Starship Troopers (依赖背包 树形dp)
- 12款基于SVG的HTML5应用和动画
- ubuntu14.04LTS 下安装java
- 处理字符串的字符数组和字符指针
- 用JAVA实现数据结构之单链表