MySQL 批处理文件连接mysql并运行命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5402765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Batch file to connect mysql and run commands
提问by MRQ
I want to run a batch file from windows, which connect to mysql server on different machine, and run a procedure from database or run a sql file which is sitting in my local machine.
我想从 Windows 运行一个批处理文件,它连接到不同机器上的 mysql 服务器,并从数据库运行一个程序或运行一个位于我本地机器上的 sql 文件。
is there's a way to do it. I know that I need the below script in my batch file to run sql commands but I believe it's only work when you run the batch file in mysql server enviornment.
有没有办法做到这一点。我知道我的批处理文件中需要以下脚本来运行 sql 命令,但我相信它仅在您在 mysql 服务器环境中运行批处理文件时才有效。
do I have to define the server info (e.g IP address & port) how do I do that
我是否必须定义服务器信息(例如 IP 地址和端口)我该怎么做
any help would be appricated
任何帮助都会被应用
Thanks
谢谢
mysql --user=XXX --password=XXXX --database=XXX < XXX.sql
mysql --user=XXX --password=XXXX --database=XXX < XXX.sql
回答by Konerak
if your MySQL Server (mysqld
) is running on the same host as your MySQL client application (mysql
), your command
如果您的 MySQL 服务器 ( mysqld
) 与 MySQL 客户端应用程序 ( mysql
)运行在同一主机上,则您的命令
mysql --user=XXX --password=XXXX --database=XXX < XXX.sql
works.
作品。
If your server is on another host (as in your case), you have to add the hostname:
如果您的服务器在另一台主机上(如您的情况),则必须添加主机名:
mysql --host=IP.ADDR.HERE --port=3306 --user=XXX --password=XXXX --database=XXX < XXX.sql
The XXX.sql
file is on the same host as your MySQL Client.
该XXX.sql
文件与您的 MySQL 客户端位于同一主机上。
Offcourse your server has to accept connections from other hosts (bind-address defined, no skip-networking, and the correct user@host privileges defined) so check your server configuration.
当然,您的服务器必须接受来自其他主机的连接(定义了绑定地址、没有跳过网络以及定义了正确的 user@host 权限),因此请检查您的服务器配置。
回答by Devart
do I have to define the server info (e.g IP address & port) how do I do that
我是否必须定义服务器信息(例如 IP 地址和端口)我该怎么做
You need to set port if it is not 3306. You need to set host if you want to specify user you want to connect, as you know MySQL user has a name and host - 'user1'@'host_name'.
如果不是 3306,则需要设置端口。如果要指定要连接的用户,则需要设置主机,因为您知道 MySQL 用户具有名称和主机 - 'user1'@' host_name'。
See the manual - The MySQL Command-Line Tool
请参阅手册 - MySQL 命令行工具
回答by street hawk
This way the sql statement can be given inside the batch file.
这样可以在批处理文件中给出 sql 语句。
mysql --host=ipaddress --port=3306 -u root -ppassword dbname -e "insert into emp ('id', 'name') values (1, 'hawk')"