简体   繁体   English

在Shell脚本中运行的MySQL命令存在错误

[英]MySQL commands running in shell script possess an error

I am preparing a automated shell script but MySQL commands running in shell script possess an Warning: Using a password on the command line interface can be insecure and error of .sock. 我正在准备一个自动化的shell脚本,但是在shell脚本中运行的MySQL命令具有一个警告:在命令行界面上使用密码可能不安全,并且会出现.sock错误。 Please Help me... My Code is: 请帮我...我的代码是:

user="root"
password="7layer"
db="mysql"
mysql -u "$user" -p"$password"  <<EOF
  use $db;
mysqld_safe --skip-grant-tables
update user set Password=PASSWORD('7layer') where user='root';
UPDATE mysql.user SET password_expired='N' WHERE User='root';
FLUSH PRIVILEGES;
exit;
EOF

The Output is: 输出为:

Starting MySQL.                                            [  OK  ]
Shutting down MySQL..                                      [  OK  ]
Warning: Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@autoinstallapi AutoInstall]#

edit your users ~/.my.cnf file and add 编辑您的用户~/.my.cnf文件并添加

[mysql]
user=root
password=7layer

Secure the file: 保护文件:

chmod 600 ~/.my.cnf

then you might need to restart mysqld ( /etc/init.d/mysqld restart ), but are able to use : 那么您可能需要重新启动mysqld( /etc/init.d/mysqld restart ),但是可以使用:

mysql <<EOF
use $db;
mysqld_safe --skip-grant-tables
###your sql queries###
exit;
EOF

without a password 没有密码

update: 更新:

This is not bound to your single script, it's bound to the user. 这不绑定到您的单个脚本,而是绑定到用户。 If that's critical, setup a new user for the script that may access mysql without entering a password from command line. 如果很关键,请为该脚本设置一个新用户,该脚本可以访问mysql而无需从命令行输入密码。

update: 更新:

As you can see in your question, Shutting down MySQL.. your MySQL server is not running. 正如您在问题中看到的, Shutting down MySQL..您的MySQL服务器未运行。 Take a look into the logs why it's aborting. 查看日志中止的原因。 One of these should contain more information: 其中之一应包含更多信息:

/var/log/mysql.err
/var/log/mysql.log
/var/log/mysql/mysql.log
/var/log/mysql/mysql.err

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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