简体   繁体   中英

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

[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 :

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.

update:

As you can see in your question, Shutting down MySQL.. your MySQL server is not running. 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

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