---
title: "shell脚本中执行mysql命令"
author: "junmail"
date: "2015-04-01T11:30:11Z"
tags: []
categories: ["数据库"]
slug: "shell-mysql"
draft: false
lastmod: "2015-04-01T11:30:11Z"
id: 2197642
url: "https://dbajun.iteye.com/blog/2197642"
---

1.mysql -hhostname -uuser -ppsword -e "mysql\_cmd"

2\.

mysql -hhostname -uuser -ppsword &lt;&lt; EOF  
    mysql\_cmd  
EOF

如下简单例子：

#!/bin/bash  
mysql -hservicedb-online -uroot -proot123 -e "use test;select * from tests;"  #方法1实例  
mysql -hservicedb-online -uroot -proot123 &lt;&lt; EOF   #方法2实例  
use test;  
select * tests;  
EOF