简体   繁体   中英

Batch file to connect mysql and run commands

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.

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.

do I have to define the server info (eg IP address & port) how do I do that

any help would be appricated

Thanks

mysql --user=XXX --password=XXXX --database=XXX < XXX.sql

if your MySQL Server ( mysqld ) is running on the same host as your MySQL client application ( mysql ), your command

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.

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.

这样,可以在批处理文件中提供sql语句。

mysql --host=ipaddress --port=3306 -u root -ppassword dbname -e "insert into emp ('id', 'name') values (1, 'hawk')"

do I have to define the server info (eg IP address & port) how do I do that

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 '.

See the manual - The MySQL Command-Line Tool

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM