Oracle 8i | Oracle 9i | Oracle 10g | Oracle 11g | Oracle 12c | Miscellaneous | PL/SQL | SQL | Oracle RAC | Oracle Apps | Linux

MySQL : Password-less Logins Using Option Files

Placing login credentials into scripts is both difficult to maintain and a security risk. This article presents two methods for configuring password-less logins for MySQL, both of which require the login credentials to be placed in an "Option File", which is a local variation on the "my.cnf" file.

Silent

This method allows logins to MySQL with no explicit reference to the option file containing the credentials. In practice it feels very much like OS Authentication in Oracle.

Create an option file in the user's home directory called ".my.cnf".

vi ~/.my.cnf

Add the relevant username and password to the file in the following format.

[client]
user=MyUser
password=MyPassword

Make sure the permissions are set so only the current user can access (read/write) the file.

chmod 600 ~/.my.cnf

The user can now log in without specifying credentials on the command line.

$ mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

The current settings can be overridden by specifying the credentials on the command line.

$ mysql --user=MyUser --password

Referenced

This method is similar to that mentioned previously, but it requires an explicit reference to the option file holding the credentials.

Create an option file to hold the credentials.

vi ~/scripts/extra.my.cnf

Add the relevant username and password to the file in the following format.

[client]
user=MyUser
password=MyPassword

Make sure the permissions are set so only the current user can access (read/write) the file.

chmod 600 ~/scripts/extra.my.cnf

The user can now log in by referencing the option file containing the credentials using the --defaults-extra-file parameter.

$ mysql --defaults-extra-file=~/scripts/extra.my.cnf
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

For more information see:

Hope this helps. Regards Tim...

Back to the Top.