Codeigniter error when trying to connect to databa

2020-02-15 01:37发布

I am getting Following error in my CodeIgniter application which is live on server.

Here is the output of the error:

A PHP Error was encountered

Severity: Warning

Message: mysqli::real_connect(): (HY000/1044): Access denied for user 'xxx'@'localhost' to database 'xxx'

Filename: mysqli/mysqli_driver.php

Line Number: 161

Backtrace:

File: /home/arya123/public_html/application/controllers/Home.php Line: 7 Function: __construct

File: /home/arya123/public_html/index.php Line: 292 Function: require_once

8条回答
闹够了就滚
2楼-- · 2020-02-15 01:52

Connect localhost with port solves the problem for me.

$db['default'] = array(
    'dsn' => '',
    'hostname' => 'localhost:3308', //Added port here
    'username' => 'root',
    'password' => '1234',
    'database' => 'my_database',
    '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
);
查看更多
我想做一个坏孩纸
3楼-- · 2020-02-15 01:53

Try to use the plain password with no special characters, below is the example:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'password',
    'password' => 'Mynewpass@756',
    'database' => 'database_name',
);
查看更多
甜甜的少女心
4楼-- · 2020-02-15 01:54

you have to set mysql port number which is by default 3306 check this in database.php

use either

'dbport' => '3306',

or

 'hostname' => 'mysql.hostingprovider.com:3306',   
查看更多
Melony?
5楼-- · 2020-02-15 01:59

The same error I was getting when I upload my codeigniter project from localhost to Live server.

What solution I find is to make some changes into the application => config => database.php

Following is the database setting for the

localhost

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'creator',
    '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
);

Following database setting for

live server (Just demo, setting differ as per hosting provider)

1) First you have to create database.

2) find MySql Databases(or anything related database) on dashboard,

3) create new database and put database name, username, password.

4) export database from localhost

5) open phpmyadmin on live server and import it.

6) change the setting of application=>config=>database.php by using FTP client or dashboard.

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'mysql.hostingprovider.com',    
    'username' => 'abc.username',
    'password' => 'abc.password',
    'database' => 'abc.databasename',
    '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
);
查看更多
三岁会撩人
6楼-- · 2020-02-15 02:01

u can add new user and pass and give it all privileges, like below:

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';
FLUSH PRIVILEGES;

then, u should update your config.php file, with new user and pass.

查看更多
相关推荐>>
7楼-- · 2020-02-15 02:05

Do the following:

  1. In applications/config/database.php, add dbport => '3306', to the default array $db['default'] = array();
  2. In system/database/DB_driver.php, change public $port = ''; to public $port = '3306';
查看更多
登录 后发表回答