Codeigniter Error Message on Live server : mysqli::real_connect(): (HY000/2002): Connection refused

12,563

Solution 1

In your database.php instead of putting "localhost" try putting "127.0.0.1". This works for most of the cases.

If you still get error then post you database.php file.

Solution 2

Use 'hostname' => 'localhost' instead of 'hostname' => 'testurl.com'. Even though you upload your code on live server, hostname remains "localhost" only.

Share:
12,563
ero
Author by

ero

Updated on June 13, 2022

Comments

  • ero
    ero almost 2 years

    I am creating a website using Codeigniter(version:3.05),php(version: 5.6.24), mysql(version: 10.1.16 mariaDB). At local server: operating system=OSX, php version=5.6.24, mysql version=10.1.16 mariaDB. At Hosting server: operating system=FreeBSD(unix), phpversion= 5.6.30, mysql version=:5.5.38-log. The site works fine on local server. But it gives error on live server as shown below.

        A PHP Error was encountered
    
        Severity: Warning
    
        Message: mysqli::real_connect(): (HY000/2002): Connection refused
    
        Filename: mysqli/mysqli_driver.php
    
        Line Number: 202
    
        Backtrace:
    
        File: /home/sv-001/www/user/myweb/application/third_party/MX/Loader.php
        Line: 109
        Function: DB
    
        File: /home/sv-001/www/user/myweb/application/third_party/MX/Loader.php
        Line: 65
        Function: initialize
    
        File: /home/sv-001/www/user/myweb/application/modules/home/controllers/Home.php
        Line: 8
        Function: __construct
    
        File: /home/sv-001/www/user/myweb/index.php
        Line: 294
        Function: require_once
    

    I have been checking my database file(database.php) several time, but I didn't find any error there regarding hostname,username,password, database name.

    database.php file

    $db['default'] = array(
            'dsn'   => '',
            'hostname' => 'testurl.com',
            'username' => 'username',
            'password' => 'password',
            'database' => 'dbname',
            '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
    );
    

    config.php file is as shown below:

    if($_SERVER['HTTP_HOST'] == "localhost" OR $_SERVER['HTTP_HOST'] == "testurl.com")
        $config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/myfolder/myfolder2/';
    else if($_SERVER['HTTP_HOST'] == "testurl.com" OR $_SERVER['HTTP_HOST'] == "testurl.com")
        $config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/myfolder2/';
    else
        $config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'];
    
    $config['index_page'] = 'index.php';
    //$config['index_page'] = '';
    
    $config['uri_protocol'] = 'REQUEST_URI';
    
    $config['url_suffix'] = '';
    
    $config['language'] = 'english';
    
    $config['charset'] = 'UTF-8';
    
    $config['enable_hooks'] = FALSE;
    
    $config['subclass_prefix'] = 'MY_';
    $config['composer_autoload'] = FALSE;
    
    
    $config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
    
    $config['allow_get_array'] = TRUE;
    $config['enable_query_strings'] = FALSE;
    $config['controller_trigger'] = 'c';
    $config['function_trigger'] = 'm';
    $config['directory_trigger'] = 'd';
    
    $config['log_threshold'] = 0;
    
    $config['log_path'] = '';
    
    $config['log_file_extension'] = '';
    
    $config['log_file_permissions'] = 0644;
    $config['log_date_format'] = 'Y-m-d H:i:s';
    
    $config['error_views_path'] = '';
    
    $config['cache_path'] = '';
    
    $config['cache_query_string'] = FALSE;
    
    $config['sess_driver'] = 'files';
    $config['sess_cookie_name'] = 'ci_session';
    $config['sess_expiration'] = 7200;
    //$config['sess_save_path'] = NULL;
    $config['sess_save_path'] = APPPATH . 'cache';
    $config['sess_match_ip'] = FALSE;
    $config['sess_time_to_update'] = 300;
    $config['sess_regenerate_destroy'] = FALSE;
    
    $config['cookie_prefix']    = '';
    $config['cookie_domain']    = '';
    $config['cookie_path']      = '/';
    $config['cookie_secure']    = FALSE;
    $config['cookie_httponly']  = FALSE;
    
    $config['standardize_newlines'] = FALSE;
    
    $config['global_xss_filtering'] = FALSE;
    
    $config['csrf_protection'] = FALSE;
    $config['csrf_token_name'] = 'csrf_test_name';
    $config['csrf_cookie_name'] = 'csrf_cookie_name';
    $config['csrf_expire'] = 7200;
    //$config['csrf_regenerate'] = TRUE;
    $config['csrf_regenerate'] = FALSE;
    $config['csrf_exclude_uris'] = array();
    $config['compress_output'] = FALSE;
    
    $config['time_reference'] = 'local';
    
    $config['rewrite_short_tags'] = FALSE;
    
    $config['proxy_ips'] = '';