2013-07-02 94 views
1

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

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) 
[[email protected] AutoInstall]# 

回答

0

编辑您的用户~/.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 
+0

嗨丹,我想乌尔脚本,但得到PID –

+0

的错误{MySQL服务器PID文件找不到! [FAILED] 正在启动MySQL ..服务器退出时未更新PID文件([FAILED] /mysql/autoinstallapi.ril.com.pid)。 错误2002(HY000):无法通过套接字'/var/lib/mysql/mysql.sock'(2) } –

+0

查看日志。 **确保PID文件存在并可由数据库的uid写入** – DanFromGermany