CodeIgniter Forums

Full Version: Failing to connect to database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Just started using CodeIgniter today following the tutorial on the site and ran into a problem with setting up the database. Looked around various other forums and sites for a couple hours to no avail of how to fix the problem. My setup is a local MySQL database, CodeIgniter and  XAMPP.

The exact error message is:
Quote:Message: mysqli::real_connect(): (HY000/1045): Access denied for user 'unityAdmin'@'localhost' (using password: YES)

Filename: mysqli/mysqli_driver.php
Line Number: 201

Now I know the credentials themselves are working fine since I can manually connect to the database via command line or Sequel Pro with the credentials that are in the database.php config file which looks like:

PHP Code:
$db['default'] = array(
 
'dsn' => '',
'hostname' => 'localhost',
'username' => 'unityAdmin',
'password' => '*****',
'database' => 'crm',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE,
);

$db['default']['port'] = '3306'
I've tried the common fixes of changing pconnect and db_debug etc but nothing seems to be working. I'm pretty stumped and any help would be greatly appreciated. Not sure if this is an issue with code igniter itself or XAMPP or what.  Confused 

Including the MVC code here for reference.
 
Model:
PHP Code:
<?php

    
class News_model extends CI_Model {

        public function 
__construct(){

            
$this->load->database();

        }

        public function 
get_news($slug){

            if(
$slug === FALSE){

                
$query $this->db->get('news');

                return 
$query->result_array();

            }

            
$query $this->db->get_where('news', array('slug' => slug));

        }

    }

?>


Controller:
PHP Code:
<?php

    
class News extends CI_Controller {

        public function 
__construct() {

            
parent::__construct();

            
$this->load->model('News_model');

            
$this->load->helper('url_helper');

        }

        public function 
index(){

            
$data['news'] = $this->News_model->get_news();

            
$this->load->view('templates/header'$data);

            
$this->load->view('news/index'$data);

            
$this->load->view('templates/footer'$data);

        }

        public function 
view($slug NULL){

            
$data['news_item'] = $this->News_model->get_news($slug);

            if (empty(
$data['news_item'])){

                
show_404();

            }

            
$data['title'] = $data['news_item']['title'];

            
$this->load->view('templates/header'$data);

            
$this->load->view('news/view'$data);

            
$this->load->view('templates/footer'$data);

        }

    }

?>


Index View:
PHP Code:
<h2><?php echo $title?></h2>

<?php foreach ($news as $news_item): ?>

    <h3><?php echo $news_item['title']; ?> </h3>

    <div class="main">

        <?php echo $news_item['text']; ?>

    </div>

    <p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View Article</a></p>

<?php endforeach; ?>

Specific Post:
PHP Code:
<?php

    
echo '<h2>'.$news_item['title'].'</h2>';

    echo 
$news_item['text'];

?>
Check what is the host of your MySQL user account.

If you have only one account like 'unityAdmin'@'127.0.0.1' this could be the issue. Create other account with privileges for 'unityAdmin'@'localhost' or update to 'unityAdmin'@'%'. Or, just update from 'localhost' to '127.0.0.1' in the database.php.
and $active_group = 'default';
(02-21-2018, 10:58 PM)natanfelles Wrote: [ -> ]Check what is the host of your MySQL user account.

If you have only one account like 'unityAdmin'@'127.0.0.1' this could be the issue. Create other account with privileges for 'unityAdmin'@'localhost' or update to 'unityAdmin'@'%'. Or, just update from 'localhost' to '127.0.0.1' in the database.php.

Thanks for the suggestions I will play with this now and get back to you. The user isn't the only user for the mysql account but it is the only user for the database I'm trying to use and setting the host to 127.0.0.1 in the database.php file changes nothing for me.

(02-21-2018, 11:00 PM)natanfelles Wrote: [ -> ]and $active_group = 'default';

This is already set as default so I didn't include it in the post sorry.
SOLVED

Issue was with using XAMPP. XAMPP only expects to see user = root and password = ' ' so anything else isn't accepted even though I had created a new user on phpmyadmin...
HOW TO RESET MySQL PASSWORD phpMyAdmin:

If the MySQL Server is running stop it!

1) Open the file my.ini - in C:\xampp\mysql\bin\my.ini

2) Under:
# The MySQL server
[mysqld]

3) add:
    skip-grant-tables

4) Start the MySQL Server and run this SQL query in phpMyAdmin.

UPDATE mysql.user SET Password=PASSWORD('Your-Password')
WHERE User='root';
FLUSH PRIVILEGES;

5) Close phpMyAdmin and Stop the MySQL Server

6) Open the file my.ini - in C:\xampp\mysql\bin\my.ini

7) Remark out the line skip-grant-tables - #skip-grant-tables

8) Re-start the MySQL Server and you should now be able to login with the new password.


For the command line version see this article:

B.5.3.2 How to Reset the Root Password
(02-22-2018, 04:35 AM)IcyGrey Wrote: [ -> ]SOLVED

Issue was with using XAMPP. XAMPP only expects to see user = root and password = ' ' so anything else isn't accepted even though I had created a new user on phpmyadmin...
thanks man! much appreciated
Update: 03-03-2021

XAMPP - Make sure MySQL is not running.


Change User and Password.

Code:
// Shell from the XAMPP Control Panel for Windows
cd mysql\bin
mysqladmin --user=root password "Your_Password"
Please refer https://codeigniter.com/userguide3/database/index.html if you've downloaded codeigniter 3 and this https://codeigniter.com/user_guide/database/index.html# if you're using codeigniter 4. It looks like you've CI4 and the code in the Model is for CI3.
Do the following using XAMPP

In the XAMPP Control Panel on the right side.


Code:
// Shell from Control Panel
cd mysql\bin
mysqladmin --user=root password "your_password"

It thinks the password is yes.


Change the above root to your user name.
Change the above your_password to yours that works.