Magento 1.9 – Fix ‘Access Denied for User root@localhost’

databaselocal.xmlmagento-1.9

my local.xml file:

<config>
<global>
    <install>
        <date><![CDATA[Fri, 13 Dec 2013 10:19:07 +0000]]></date>
    </install>
    <crypt>
        <key><![CDATA[a1e2164673b9815575543621066bc628]]></key>
    </crypt>
    <!-- CACHE -->
    <disable_local_modules>false</disable_local_modules>
    <resources>
        <db>
            <table_prefix><![CDATA[]]></table_prefix>
        </db>
        <default_setup>
            <connection>
                <host><![CDATA[10.0.2.2]]></host>
                <username><![CDATA[root]]></username>
                <password><![CDATA[toor]]></password>
                <dbname><![CDATA[mage_allbrands]]></dbname>
                <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
                <model><![CDATA[mysql4]]></model>
                <type><![CDATA[pdo_mysql]]></type>
                <pdoType><![CDATA[]]></pdoType>
                <active>1</active>
            </connection>
        </default_setup>
    </resources>
    <session_save><![CDATA[db]]></session_save>
    <!-- COMPOSER -->
</global>
<admin>
    <routers>
        <adminhtml>
            <args>
                <frontName><![CDATA[factoryx]]></frontName>
            </args>
        </adminhtml>
    </routers>
</admin>
</config>

Example of me connecting to the database through mysql:

[deployment@dt report]$ mysql -u root -ptoor
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.6.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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 |
| mage_allbrands     |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

mysql>

I can also connect using localhost as the host by running this command:
mysql -h localhost -u root -ptoor

So I'm not sure exactly why Magento is giving me this error. From my research the answers people are getting is to ensure their password is correct in local.xml, which I already know is correct as shown above.

Thanks so much for the help!

edit: complete error log file here

   a:4:{i:0;s:86:"SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)";i:1;s:2820:"#0 /var/www/deployment/releases/20171003164940/lib/Zend/Db/Adapter/Pdo/Mysql.php(111): Zend_Db_Adapter_Pdo_Abstract->_connect()
#1 /var/www/deployment/releases/20171003164940/lib/Varien/Db/Adapter/Pdo/Mysql.php(396): Zend_Db_Adapter_Pdo_Mysql->_connect()
#2 /var/www/deployment/releases/20171003164940/lib/Zend/Db/Adapter/Abstract.php(460): Varien_Db_Adapter_Pdo_Mysql->_connect()
#3 /var/www/deployment/releases/20171003164940/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('SET NAMES utf8', Array)
#4 /var/www/deployment/releases/20171003164940/lib/Varien/Db/Adapter/Pdo/Mysql.php(504): Zend_Db_Adapter_Pdo_Abstract->query('SET NAMES utf8', Array)
#5 /var/www/deployment/releases/20171003164940/app/code/core/Mage/Core/Model/Resource.php(179): Varien_Db_Adapter_Pdo_Mysql->query('SET NAMES utf8')
#6 /var/www/deployment/releases/20171003164940/app/code/core/Mage/Core/Model/Resource.php(110): Mage_Core_Model_Resource->_newConnection('pdo_mysql', Object(Mage_Core_Model_Config_Element))
#7 /var/www/deployment/releases/20171003164940/app/code/core/Mage/Core/Model/Resource/Db/Abstract.php(320): Mage_Core_Model_Resource->getConnection('core_write')
#8 /var/www/deployment/releases/20171003164940/app/code/core/Mage/Core/Model/Resource/Db/Abstract.php(350): Mage_Core_Model_Resource_Db_Abstract->_getConnection('write')
#9 /var/www/deployment/releases/20171003164940/app/code/core/Mage/Core/Model/Resource/Db/Abstract.php(335): Mage_Core_Model_Resource_Db_Abstract->_getWriteAdapter()
#10 /var/www/deployment/releases/20171003164940/app/code/core/Mage/Core/Model/Resource/Cache.php(53): Mage_Core_Model_Resource_Db_Abstract->_getReadAdapter()
#11 /var/www/deployment/releases/20171003164940/app/code/core/Mage/Core/Model/Cache.php(478): Mage_Core_Model_Resource_Cache->getAllOptions()
#12 /var/www/deployment/releases/20171003164940/app/code/core/Mage/Core/Model/Cache.php(520): Mage_Core_Model_Cache->_initOptions()
#13 /var/www/deployment/releases/20171003164940/app/code/core/Mage/Core/Model/App.php(1202): Mage_Core_Model_Cache->canUse('config')
#14 /var/www/deployment/releases/20171003164940/app/code/core/Mage/Core/Model/Config.php(417): Mage_Core_Model_App->useCache('config')
#15 /var/www/deployment/releases/20171003164940/app/code/core/Mage/Core/Model/Config.php(297): Mage_Core_Model_Config->_canUseCacheForInit()
#16 /var/www/deployment/releases/20171003164940/app/code/core/Mage/Core/Model/App.php(424): Mage_Core_Model_Config->loadModulesCache()
#17 /var/www/deployment/releases/20171003164940/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Model_App->_initModules()
#18 /var/www/deployment/releases/20171003164940/app/Mage.php(684): Mage_Core_Model_App->run(Array)
#19 /var/www/deployment/releases/20171003164940/index.php(83): Mage::run('default', 'store')
#20 {main}";s:3:"url";s:1:"/";s:11:"script_name";s:10:"/index.php";}

Best Answer

There may be many reasons because of which you are facing the issue

1) It seems like you are trying to access the database remotely. You may need to add your IP to whitelist which can access the database remotely. to do this follow below steps

a) Login to your cpanel
b) find the section on the main page labeled Databases.
c) under databases section you will see Remote MySQL , click on it
d) Add a hostname or IP address that you want to grant remote MySQL access to and then click the Save button 

Now you should be able to connect using magento

2) If you are not accessing database remotely , use localhost in <host><![CDATA[10.0.2.2]]></host> in place of IP , however you may contact your server support for connection settings or you may check server documentation

3) This might not be the case here but i would like to mention it here so it may help someone in future. Make sure the user you are using to connect has proper permissions-

Run SHOW GRANTS FOR 'root'@'localhost'; in mysql terminal to see what permission have been granted to your user
If no proper and required permissions are there run this command GRANT ALL PRIVILEGES ON%.* TO '[user]'@'[hostname]' IDENTIFIED BY '[password]' WITH GRANT OPTION; to provide permissions.

Hope it helps

Related Topic