如何使用批处理脚本在mysql5.5中输入密码?

时间:2023-01-11 02:09:32

i have written a batch script but it still ask me for a password. i want to enter it automatically. please help me

我已经写了一个批处理脚本,但它仍然要求我输入密码。我想自动进入。请帮我

here is my batch script :

这是我的批处理脚本:

setlocal EnableDelayedExpansion
c:
cd "C:\Program Files\MySQL\MySQL Server 5.5\bin"

mysql -u root -p root

but still in output it ask for a password as:

但仍在输出中要求输入密码:

Enter Password :

输入密码:

i got the answer. for that find below my comment

我得到了答案。在我的评论下面。

7 个解决方案

#1


4  

You can't have a space between the option and the password. So it should be:

在选项和密码之间不能有空格。所以它应该是:

mysql -u root -proot

mysql - u root -proot

Or use --password=root

或者使用——密码=根

#2


2  

For other googlers like me, I'll add my solution. Unfortunately, official documentation doesn't tell this clearly

对于像我这样的谷歌人,我会添加我的解决方案。不幸的是,官方文件并没有明确说明这一点。

Short parameters like these didn't work to me and prompted for password

mysqldump -uroot -p "qwerty" mybase > Z:\mybase.sql

With "full" name parameters it worked, just warning about this action as insecure

mysqldump  --user="root" --password="qwerty" mybase > Z:\mybase.sql

echo 'Backup OK' > mysql_dump.log

#3


1  

Problem Solved Got the solution

问题得到了解决。

c:

c:

cd "C:\Program Files\MySQL\MySQL Server 5.5\bin"

cd“C:\程序文件\MySQL\MySQL Server 5.5\bin”

mysql -uroot -proot -e "delete from db.tablename where columnname = 'something';

mysql -uroot -proot -e“从db中删除。tablename, columnname = 'something';

The problem is In this line "setlocal EnableDelayedExpansion" This should be deleted. and the code runs smoothly :)

问题是在这一行中,“setlocal EnableDelayedExpansion”应该被删除。代码运行流畅:)

#4


0  

AFAIK you cannot do this using the mysql bin, but mysqladmin can.

AFAIK不能使用mysql bin,但mysqladmin可以。

See the docs here: http://dev.mysql.com/doc/refman/5.5/en/mysqladmin.html

查看这里的文档:http://dev.mysql.com/doc/refman/5.5/en/mysqladmin.html。

#5


0  

-ppassword displays warning

-ppassword显示警告

Warning: Using a password on the command line interface can be insecure.

警告:在命令行界面上使用密码可能是不安全的。

https://dev.mysql.com/doc/refman/5.7/en/password-security-user.html

https://dev.mysql.com/doc/refman/5.7/en/password-security-user.html

Solution: You can use cnf options file

解决方案:您可以使用cnf选项文件。

https://dev.mysql.com/doc/refman/5.7/en/option-files.html

https://dev.mysql.com/doc/refman/5.7/en/option-files.html

linux:

linux:

echo -e "[client]\nhost=mysqlhost\nport=3306\nuser=root\npassword=${ROOT_PASS}" > root.cnf
mysql --defauls-file=root.cnf -e "SELECT * FROM users;" ${MYDB}

windows:

窗口:

Ship root.cnf file with batch script and run

使用批处理脚本和运行的root.cnf文件。

"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql" --defauls-file=root.cnf -e "SELECT * FROM users;" %MYDB%

#6


0  

try this create a file for password .pw and below command works in batch fine. mysql --default-file=C:\path*.pw -u %username% blah blah..

试试这个创建一个文件的密码。pw和下面的命令工作在批处理好的。mysql——默认文件= C:\路径*。pw -u %的用户名等等。

#7


0  

Your best option is to use MySQL options files.

最好的选择是使用MySQL选项文件。

MySQL 5.5 makes use of the .my.cnf file, you just need to supply your username and password. This approach means you can login to mysql just by typing mysql in your script and the .my.cnf file will fill in the rest; Here's how:

MySQL 5.5使用.my.cnf文件,只需要提供用户名和密码即可。这种方法意味着,只需在脚本中输入mysql,然后在.my.cnf文件中填入mysql,就可以登录到mysql;方法如下:

Create a .my.cnf file in the home directory of the user which will run the script. The contents should look like the following code block, swapping out your_user and your_password for their actual values.

在用户的主目录中创建一个.my.cnf文件,该文件将运行该脚本。内容应该像下面的代码块,将your_user和your_password替换为它们的实际值。

[client]
user=your_user
password=your_password   

It's prudent to change the permission of this file to either 400 or 600, this will mean only your user and root will be able to see the file.

谨慎地将此文件的权限更改为400或600,这意味着只有您的用户和根用户能够看到该文件。

chmod 600 .my.cnf

You're all set!

你都准备好了!

N.B.

From version 5.6 onwards you can use the mysql_config_editor utility, which generates a .mylogin.cnf, it has the same result as the above apporoach with the added benefit that the password will be encrypted by the utility.

从版本5.6开始,您可以使用mysql_config_editor实用程序,它生成一个.mylogin.cnf,它的结果与上面的appleach相同,附加的好处是密码将由实用程序加密。

Links

MySQL 5.5 https://dev.mysql.com/doc/refman/5.5/en/password-security-user.html

MySQL 5.5 https://dev.mysql.com/doc/refman/5.5/en/password-security-user.html

MySQL 5.6+ https://dev.mysql.com/doc/refman/8.0/en/mysql-config-editor.html

MySQL 5.6 + https://dev.mysql.com/doc/refman/8.0/en/mysql-config-editor.html

#1


4  

You can't have a space between the option and the password. So it should be:

在选项和密码之间不能有空格。所以它应该是:

mysql -u root -proot

mysql - u root -proot

Or use --password=root

或者使用——密码=根

#2


2  

For other googlers like me, I'll add my solution. Unfortunately, official documentation doesn't tell this clearly

对于像我这样的谷歌人,我会添加我的解决方案。不幸的是,官方文件并没有明确说明这一点。

Short parameters like these didn't work to me and prompted for password

mysqldump -uroot -p "qwerty" mybase > Z:\mybase.sql

With "full" name parameters it worked, just warning about this action as insecure

mysqldump  --user="root" --password="qwerty" mybase > Z:\mybase.sql

echo 'Backup OK' > mysql_dump.log

#3


1  

Problem Solved Got the solution

问题得到了解决。

c:

c:

cd "C:\Program Files\MySQL\MySQL Server 5.5\bin"

cd“C:\程序文件\MySQL\MySQL Server 5.5\bin”

mysql -uroot -proot -e "delete from db.tablename where columnname = 'something';

mysql -uroot -proot -e“从db中删除。tablename, columnname = 'something';

The problem is In this line "setlocal EnableDelayedExpansion" This should be deleted. and the code runs smoothly :)

问题是在这一行中,“setlocal EnableDelayedExpansion”应该被删除。代码运行流畅:)

#4


0  

AFAIK you cannot do this using the mysql bin, but mysqladmin can.

AFAIK不能使用mysql bin,但mysqladmin可以。

See the docs here: http://dev.mysql.com/doc/refman/5.5/en/mysqladmin.html

查看这里的文档:http://dev.mysql.com/doc/refman/5.5/en/mysqladmin.html。

#5


0  

-ppassword displays warning

-ppassword显示警告

Warning: Using a password on the command line interface can be insecure.

警告:在命令行界面上使用密码可能是不安全的。

https://dev.mysql.com/doc/refman/5.7/en/password-security-user.html

https://dev.mysql.com/doc/refman/5.7/en/password-security-user.html

Solution: You can use cnf options file

解决方案:您可以使用cnf选项文件。

https://dev.mysql.com/doc/refman/5.7/en/option-files.html

https://dev.mysql.com/doc/refman/5.7/en/option-files.html

linux:

linux:

echo -e "[client]\nhost=mysqlhost\nport=3306\nuser=root\npassword=${ROOT_PASS}" > root.cnf
mysql --defauls-file=root.cnf -e "SELECT * FROM users;" ${MYDB}

windows:

窗口:

Ship root.cnf file with batch script and run

使用批处理脚本和运行的root.cnf文件。

"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysql" --defauls-file=root.cnf -e "SELECT * FROM users;" %MYDB%

#6


0  

try this create a file for password .pw and below command works in batch fine. mysql --default-file=C:\path*.pw -u %username% blah blah..

试试这个创建一个文件的密码。pw和下面的命令工作在批处理好的。mysql——默认文件= C:\路径*。pw -u %的用户名等等。

#7


0  

Your best option is to use MySQL options files.

最好的选择是使用MySQL选项文件。

MySQL 5.5 makes use of the .my.cnf file, you just need to supply your username and password. This approach means you can login to mysql just by typing mysql in your script and the .my.cnf file will fill in the rest; Here's how:

MySQL 5.5使用.my.cnf文件,只需要提供用户名和密码即可。这种方法意味着,只需在脚本中输入mysql,然后在.my.cnf文件中填入mysql,就可以登录到mysql;方法如下:

Create a .my.cnf file in the home directory of the user which will run the script. The contents should look like the following code block, swapping out your_user and your_password for their actual values.

在用户的主目录中创建一个.my.cnf文件,该文件将运行该脚本。内容应该像下面的代码块,将your_user和your_password替换为它们的实际值。

[client]
user=your_user
password=your_password   

It's prudent to change the permission of this file to either 400 or 600, this will mean only your user and root will be able to see the file.

谨慎地将此文件的权限更改为400或600,这意味着只有您的用户和根用户能够看到该文件。

chmod 600 .my.cnf

You're all set!

你都准备好了!

N.B.

From version 5.6 onwards you can use the mysql_config_editor utility, which generates a .mylogin.cnf, it has the same result as the above apporoach with the added benefit that the password will be encrypted by the utility.

从版本5.6开始,您可以使用mysql_config_editor实用程序,它生成一个.mylogin.cnf,它的结果与上面的appleach相同,附加的好处是密码将由实用程序加密。

Links

MySQL 5.5 https://dev.mysql.com/doc/refman/5.5/en/password-security-user.html

MySQL 5.5 https://dev.mysql.com/doc/refman/5.5/en/password-security-user.html

MySQL 5.6+ https://dev.mysql.com/doc/refman/8.0/en/mysql-config-editor.html

MySQL 5.6 + https://dev.mysql.com/doc/refman/8.0/en/mysql-config-editor.html