Sometime it is needed to be able to login or to access MySQL Database Server without having to enter password. To be able to do it or to accomplish it, the first thing which is needed is the password of the user which is going to be used for non-interactive login. In addition, a file which can be used to be filled with user and password. MySQL Database Server will read the file each time the connection is going to be made.
In detail, the steps which is needed to be executed so the goal for MySQL Non-Interactive login can be accomplished is specified as follows :
1. Create a file named .my.cnf in home directory of the user used to connect to the database. For an example if currently logged in as jack, the home directory of jack will be in /home/jack. To be able to check the current working directory, please look at this article titled ‘Check current working directory in Linux with pwd command‘.
2. In the already created file named .my.cnf fill the content in the format as shown below :
[client] password=mypassword Description : [client] : It is a section defined as a client section password : It is a variable which is used to define password. mypassword : It is the value of password's variable and in this context the value is 'mypassword'. It need to be adjusted with user which is used to connect to MySQL Database Server.
3. Don’t forget to change the permission of the file. Execute the following command to do it :
chmod 0400 .my.cnf
The above command used to set permissoin that the only one can read the file is the owner of the file which is in this case is jack. It can be shown below :
[jack@hostname ~]# ls -al | grep .my.cnf -r-------- 1 jack jack 52 Sep 6 10:55 .my.cnf [jack@hostname ~]#
Why is the permission only set for read to user jack ?, If there is never any necessary need to edit or to change the content of the file periodically, it is best to keep it just to be read only. After all, the edit process can be done by re-modifying the permission of the file so that it can be written by changing the permission into 0600.
4. Check whether jack can login without having to enter the password using a user named ‘user’ where the password is ‘mypassword’ by logging first to MySQL Database Server and we can try to connect to MySQL Database Server just in our local environment without having to connect to the database remotely as shown below :
[jack@hostname ~]# mysql -uuser Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.12-0ubuntu1.1 (Ubuntu) Copyright (c) 2000, 2016, 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>
As it can be seen above, it doesn’t need to interactively prompt for password and type the password to be able to connect to MySQL Database Server. Since, the authentication process has already done when MySQL Database Server actually read the password exists in file .my.cnf as part of its content.
Remember it first, to be able to log in to MySQL Database Server, check the status of the service. To check service of MySQL Database Server, this article titled ‘Check MySQL Service Status‘ can be used for a reference.