I know I can do some thing like this in script:
mysql -u root -pPassword mydb -e "select * from foo"
In my script I have a sequence of commands which some of them are periodically related to MySQL
and others are independent (so I cannot use Here Documents
). It seems it is necessary to add -u root -pPassword
part in all commands to avoid getting this error:
Access denied for user 'user'@'host' (using password: NO)
But is annoying. Is there a way to avoid adding -u root -pPassword
part to all commands?
Create a file in your home directory called .my.cnf
and add these lines to it:
[client]
user = root
password = <yourpassword>
(Obviously where I wrote " <yourpassword>
" you would write your actual password.)
Then you can run mysql
commands (or some others like mysqldump or mysqladmin) without specifying user or password.
mysql mydb -e "select * from foo"
If you want to use a different file besides $HOME/.my.cnf
you can specify it on your command-line.
mysql --defaults-extra-file=myspecial.cnf mydb -e "select * from foo"
Be careful to change the permissions on that file so it can't be read by other people:
chmod 600 $HOME/.my.cnf
Read more about using option files: http://dev.mysql.com/doc/refman/5.7/en/option-files.html
You can use login-paths via mysql_config_editor
command :
$> mysql_config_editor set --login-path=local --host=localhost --user=myuser --password
Enter password: <Password is prompted to be inserted in a more secure way>
Then login from your scripts as:
mysql --login-path=local --database=mydb
As per linked manual:
The best way to specify server connection information is with your .mylogin.cnf file. Not only is this file encrypted, but any logging of the utility execution will not expose the connection information. Thus, no user names, passwords, ports, etc. are visible in the log. This is the preferred method for using MySQL Utilities to connect to servers.
You could just create a wrapper shell script or an alias like this:
mysql
aliased to /path/to/mysql -u root -pPassword mydb
Here is the alias command:
$alias mysql="/path/to/mysql -u root -pPassword mydb"
$
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.