Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

On cPanel when I am logged in as root and type "mysql" without hostname and password it gives me direct access to mysql root user.

I would like to do this for one of my non-cpanel server where the linux root user gets password less logon to mysql root user in the same way as it does on cPanel.

Is this possible ?

share|improve this question

2 Answers 2

up vote 14 down vote accepted

The easiest way to do this is to use a client section of the ~/.my.cnf file, and add the credentials there.

[client]
user=root
password=somepassword
...

it's a good idea to make that file readable only by root too.

share|improve this answer
    
Yes this works and this is how cPanel is doing it –  user1066991 Dec 27 '13 at 9:23
2  
I need to point towards a potential security problem.. You need to be a bit careful that you don't edit this in the /etc/my.cnf which the mysql server uses to boot up. If you put the mysql root user in the /etc/my.cnf ANYONE can use the mysql server with ALL privileges on EVERY database without a password. Iain points towards the shell user's own .my.cnf in the root's home which is correct of course. –  Koen van der Rijt Dec 27 '13 at 9:59

Make a file with only contains the mysql root password and only root can read.

# ls -l /root/.mysqlpw 
-rw------- 1 root root 7 2013-08-19 13:24 /root/.mysqlpw

You can import a database with

# mysql -u root -p`cat /root/.mysqlpw ` yourdatabase < databasedump.sql

or connect to you database and issue mysql commands

# mysql -u root -p`cat /root/.mysqlpw ` yourdatabase
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20460
Server version: 5.1.63-0ubuntu0.10.04.1 (Ubuntu)

Copyright (c) 2000, 2011, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| yourdatabase       |
+--------------------+
3 rows in set (0.00 sec)

mysql> show tables;
...
share|improve this answer
    
This is much more work than the other answer. And your password is too short. :) –  Michael Hampton Jan 2 at 17:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.