Access MySQL without Password

Posted on

In this article, there will be an explanation about accessing MySQL database server without any involvement of entering password. It has been tried on local machine, server or workstation which means it need a physical access to be done and it have never been tried remotely before.

So, in other word, in this article it will try to describe how to execute command to access MySQL database server via MySQL command console without having to enter password interactively since it has already been provided by MySQL configuration file which is going to be explained later.

These are steps which is needed to be executed before the goal of accessing MySQL database server without entering password can be accomplished :

1. Define a user which is going to be used to execute the command to access MySQL database server. In the case of this article, it chooses ‘root’.

2. Create a file in the home user directory, or in this case because it is ‘root’, the file will be located in /root/. The file name is .my.cnf. So, there will be a file in /root/ named .my.cnf as shown below :

[root@hostname ~]# pwd
/root
[root@hostname ~]# touch .my.cnf

Basically, .my.cnf is a MySQL configuration file which is going to be used by the user, in this case it is ‘root’ since it is located on the home user directory which is ‘/root’ in this article context.

  1. Edit the newly created file :
[root@hostname ~]# vim .my.cnf

And insert the following content :

[client]
password=the_password_which_is_going_to_be_used
Description
[client] : it is marked a new section of MySQL configuration file which is called 'client' section. 
password : it is a variable which is used to define the password which is going to be used as client to connect to MySQL database server where it is associated or it is the true password of any user registered to this MySQL database server  

Depend on the user which is being chosen it will determine the location or path of the file. The value of the password variable is also depend on user which is going to be used to access or connect to MySQL database server.

3. Make sure the file is secure by changing the permission of the file :

[root@hostname ~]# chmod 600 .my.cnf

The above command is changing the permission to read only for the owner of the file, it is represented by ‘6’ in the first digit. The combined permission 600 is set for the sake of protecting the file from the group or other direct access.

[root@hostname ~]# chown root.root .my.cnf

The above command is changing the owner and group of the file, it is represented by root.root.

Below is the output of the above command execution for changing file permission :

[root@hostname ~]# ls -al | grep .my.cnf
-r--------   1 root root        52 Sep  6 10:55 .my.cnf
[root@hostname ~]#

4. Try to connect to MySQL database server by executing the following command in the bash prompt :

[root@hostname ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 158762
Server version: 5.6.27-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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, MySQL doesn’t ask for password entry to connect although we are accessing MySQL database server using ‘root’ account.

One thought on “Access MySQL without Password

Leave a Reply