繁体   English   中英

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

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

我正在准备一个自动化的shell脚本,但是在shell脚本中运行的MySQL命令具有一个警告:在命令行界面上使用密码可能不安全,并且会出现.sock错误。 请帮我...我的代码是:

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

输出为:

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]#

编辑您的用户~/.my.cnf文件并添加

[mysql]
user=root
password=7layer

保护文件:

chmod 600 ~/.my.cnf

那么您可能需要重新启动mysqld( /etc/init.d/mysqld restart ),但是可以使用:

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

没有密码

更新:

这不绑定到您的单个脚本,而是绑定到用户。 如果很关键,请为该脚本设置一个新用户,该脚本可以访问mysql而无需从命令行输入密码。

更新:

正如您在问题中看到的, Shutting down MySQL..您的MySQL服务器未运行。 查看日志中止的原因。 其中之一应包含更多信息:

/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