Topic: Setup DB Connection Error

I get the following error messages when trying to connect to the db:
mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in file: /var/www/html/includes/db/connect_db_mysqli.inc at line 206
mysqli_select_db() expects parameter 1 to be mysqli, boolean given in file: /var/www/html/includes/db/connect_db_mysqli.inc at line 208
mysqli_query() expects parameter 1 to be mysqli, boolean given in file: /var/www/html/includes/db/connect_db_mysqli.inc at line 213
Cannot connect to database. User or password is invalid or you have no permittions to create database.

I have read every thread I have found, but nothing seems to address this issue. One post suggested issues with PHP7, so I added a modification to revert back to PHP5.6. This did not make any difference. I've also read threads suggesting changes to the connect_db_mysqli.inc file, but my file already seems to include the proposed changes.

I've had no problem accessing MySQL via SSH, so I know it's running. Am I missing something obvious? Oh, and I'm using Ubuntu 16.04

Hoping for a solution...

Re: Setup DB Connection Error

Which version of FA are you using? Try the latest Git Master version. It is obvious that there is no dbname given in your config_db.php file for the company chosen during login.

Line 208 of the file includes/db/connect_db_mysqli.inc is:

if (!mysqli_select_db($db, $connection["dbname"]))

A possible modification to it would be:

if (!$connection["dbname"] && !mysqli_select_db($db, $connection["dbname"]))

@joe: want to commit it?

Re: Setup DB Connection Error

Which versions of php do have this problem?

Joe

Re: Setup DB Connection Error

All the later ones including PHP 5.6 with standard defaults (debs use saner ones).

In fact a separate test to check if dbname exists in the connection array with an appropriate error would be better. many just copy over the standard FA files and populate the db manually from some default chart without installing FA and find that the files like config_db.php,  installed_extensions.php (both in the webroot and in each company), lang/installed_languages.inc and the log files are not present. WHilst some get created on use, the likes of config_db.php cannot be fathomed.

Re: Setup DB Connection Error

@apmuthu

I am having a meeting with Janusz next weekend.

If you mail me the issues that I should discuss with Janusz, please email me them.

This issue here, can be fixed as you describe, but is it good enough?

/Joe

Re: Setup DB Connection Error

We can let this pass as the error will indicate to the user that FA needs to be installed. PHP returns false on error for such common functions like db access. This is due to strict syntax checks in later versions of PHP trying to make it more rigid instead of retaining it to be loosely typed. Otherwise, each missing variable will have to be tackled making it long winded.

Re: Setup DB Connection Error

As mentioned, I've been trying both PHP7 and 5.6

I'll follow up on your comments with a question: You mention a config_db.php I thought this was created by the system during the setup process?

I've followed the steps of the installation guide, meaning extracted all the files at the location where I want them, and created a database. If there's a step I'm missing that needs me to manually create a config_db.php file, this should have been included in the installation guide. Better yet - it should be automated during installation.

I'll try your suggestions, but I don't think this is the solution.

Re: Setup DB Connection Error

Using the modification gave me these errors:
mysqli_connect(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in file: /var/www/html/includes/db/connect_db_mysqli.inc at line 206
mysqli_query() expects parameter 1 to be mysqli, boolean given in file: /var/www/html/includes/db/connect_db_mysqli.inc at line 194
Cannot connect to database. User or password is invalid or you have no permittions to create database.

Note: On the first page of installation it checks OK on the config_db.php file, but I have tried every search I know of to locate that file to check if the dbname is included or not, but I am not able to locate the file. Any ideas? (And yes, I did install the latest FA version as per the link given by @apmuthu)

Re: Setup DB Connection Error

Then it is a case of permissions for files/folders. Check if the contents of your config_db.php are okay and the db access credentials for the said db are fine. If the file is absent, then during the installation, the webserver process did not have sufficient permissions to write to the FA webroot folder.

Re: Setup DB Connection Error

So the first couple of errors are now taken care of. It had not so much to do with the dbname as it had to do with the fact that the db password was too long to be recognized. Once a shorter name was created, most errors went away. Now I'm only stuck with "Cannot connect to database. User or password is invalid or you have no permissions to create database." Any ideas on how to solve this one?

Re: Setup DB Connection Error

Manually create the database and then proceed to create the new company or permit the db user used to create new databases.

Re: Setup DB Connection Error

I have manually created the db as per the installation instructions, with a user that has full privileges. The DB is showing when accessed through SSH, but I am still getting the error "Cannot connect to database. User or password is invalid or you have no permittions to create database."

Re: Setup DB Connection Error

The hostname you are using it wrong. And Also another possibility the database user account doesn't have right to work on.

Subscription service based on FA
HRM CRM POS batch Themes

Re: Setup DB Connection Error

I've tried 127.0.0.1 and localhost as hostname, both of which should work. As the user I've even tried as root, and made sure that root does indeed have all privileges. I'm still running into the same error message.

Re: Setup DB Connection Error

Ok. so the problem is not yet identified. try this program and if the program works to connect. than the database can connect with login credentials you used.

 <?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?> 
Subscription service based on FA
HRM CRM POS batch Themes

16 (edited by RedCone 04/27/2018 03:32:26 pm)

Re: Setup DB Connection Error

So we figured it out. We were able to connect with the db just fine from several other methods, including the script above. The issue ended up being caused by what seems like a error in the connect_db_mysqli.inc file where it was skipping the

mysqli_select_db($db, $connection["dbname"]);

We changed it to

{
    global $db;

    $db = mysqli_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"], "",
        !empty($connection["port"]) ? $connection["port"] : 3306); // default port in mysql is 3306

    //If no name is selected we will not be able to select a database nor create one just return
    if (!$connection["dbname"])
        return 0;
    
    //Try and select the specified database
    $selectedDB = mysqli_select_db($db, $connection["dbname"]);

    //If the db doesn't exist create the database
    if (!$selectedDB)
    {
        $sql = "CREATE DATABASE IF NOT EXISTS `" . $connection["dbname"] . "`"
            . " DEFAULT COLLATE '" . get_mysql_collation($connection["collation"]) . "'";

        if (!mysqli_query($db, $sql) || !mysqli_select_db($db, $connection["dbname"]))
            return 0;
    } else {
        //else just use the selected db and ensure that the collation is set correclty
         if ($selectedDB)
         {
            if (!db_set_collation($db, $connection["collation"]))
            {
                echo mysqli_error($db);
                return 0;
            }
        }
        else
            return 0;
    }
    return $db;
}

And now it works.

Re: Setup DB Connection Error

The snippet:

$db = mysqli_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"], "",
        !empty($connection["port"]) ? $connection["port"] : 3306); // default port in mysql is 3306

can be changed to include the dbname directly like:

$db = mysqli_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"], $connection["dbname"],
        !empty($connection["port"]) ? $connection["port"] : 3306); // default port in mysql is 3306

Re: Setup DB Connection Error

That part of the code was not something we changed, I just included it as it was part of the overall section. But yes, you're right.

Re: Setup DB Connection Error

Hi,

I am trying to install 2.4.4 and am running into this same error message at the 2nd page of the installer.  I am using mariadb on debian buster, and I have pre-created the database and user, and I have tested connections to the DB as working, with the php script in this thread and standard shell commands.

I tried changing out the block of code as suggested by RedCone (thanks for providing that solution), but unfortunately, it did not work for me, I still get the error.  Also, when I replace with that block of code, I get an additional error message that access is denied to the database

When I enable logging, it appears the connection is established and the collate command is run:

| 2018-05-17 18:00:27.468765 | [fakmm] @ localhost []      |       132 |         0 | Connect      | fakmm@localhost as anonymous on                                  |
| 2018-05-17 18:00:27.468969 | fakmm[fakmm] @ localhost [] |       132 |         0 | Init DB      | fa_kmm                                                           |
| 2018-05-17 18:00:27.469053 | fakmm[fakmm] @ localhost [] |       132 |         0 | Query        | ALTER DATABASE COLLATE utf8_unicode_ci                       
| 2018-05-17 18:00:27.469137 | fakmm[fakmm] @ localhost [] |       132 |         0 | Quit         |                                                                  |

This log entry happens with the original block of code, and the one that RedCone provided; and this is logged despite the error message on the screen saying access is denied.

I tried downloading 2.4.3 and experience the same symptoms with this as well.

I am out of ideas for the moment, If there are any suggestions they would be graciously received...

Re: Setup DB Connection Error

It appears that your MySQL server is running on one collation/character set and your client connection is running on another whilst your action FA connection is trying to change the connection characteristics.

ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Refer article.

Your connection appears to be anonymous and not from a specific user with password.

Re: Setup DB Connection Error

apmuthu wrote:

It appears that your MySQL server is running on one collation/character set and your client connection is running on another whilst your action FA connection is trying to change the connection characteristics.

ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Refer article.

Okay, so this is my first time trying to use MariaDB with FA, so maybe there is something I don't know.  But in the config files, the mysqld server config has:

character-set-server  = utf8mb4
collation-server      = utf8mb4_general_ci

and the mysql client config has:

default-character-set = utf8mb4

So maybe I am misunderstanding you, but I think I have the same collation/character set on both client and server.  Additionally, this is the 3rd web app I am installing on this box, and the first two show no signs of any discrepancy

apmuthu wrote:

Your connection appears to be anonymous and not from a specific user with password.

I noticed this too and at first though it was significant, however, from this post, I think that it is not.

I am super slow to work my way through code, but from what I can tell, the function that RedCone replaced is where the problem is.  I have yet to figure out exactly what the boggle is, but something in there is not working, for sure...

Re: Setup DB Connection Error

okay, thanks to my exceptionally brilliant employee, we figured out the problem is that when you create the dbuser, you must do so with the grant option:

grant all on fa_kmm.* to fakmm@'localhost' identified by 'XXXXXXXXXXXXXXX' with grant option;

This works for me without using RedCone's changes to the function...

If this was in the documentation somewhere, I overlooked it...

Re: Setup DB Connection Error

If the db was created earlier and assigned to the specific FA user, then the grant permission is not needed. It is quite insecure too.

Re: Setup DB Connection Error

Hello I having the same problem trying to install front accounting i get the errors
mysqli_connect(): (HY000/1045): Access denied for user 'fraccdbuser'@'localhost' (using password: YES) in file: /var/www/html/fracc/includes/db/connect_db_mysqli.inc at line 206
mysqli_select_db() expects parameter 1 to be mysqli, bool given in file: /var/www/html/fracc/includes/db/connect_db_mysqli.inc at line 208
mysqli_query() expects parameter 1 to be mysqli, bool given in file: /var/www/html/fracc/includes/db/connect_db_mysqli.inc at line 213
Cannot connect to database. User or password is invalid or you have no permitions to create database

I tested the connection script  apmuthu posted and i can successfully connect to the database with the credentials, also can connect with the credentials of fraccdbuse using ssh and use the database and create tables on it, also www-data have all privilege on the web root html folder.
Finally i tried to hardcode the database name  in the script as apmuthu but is the same errors occurs.

Using FrontAccounting 2.9

PHP Version 7.4.3

installed on Apache
Server version: Apache/2.4.41 (Ubuntu)
Server built:   2020-08-12T19:46:17

Using MariaDB database
Server version: 10.3.25-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04

Help appreciated

Re: Setup DB Connection Error

Also turn the logs ON and i could see the following, the first (96) connect is via the front accounting setup and the second (97) is via ssh by mysql client.

201221  3:49:05     96 Connect  fraccdbuser@localhost as anonymous on fraccdb
                    96 Connect  Access denied for user 'fraccdbuser'@'localhost' (using password: YES)
201221  3:49:41     97 Connect  fraccdbuser@localhost as anonymous on
                    97 Query    select @@version_comment limit 1
201221  3:49:57     97 Query    SELECT DATABASE()
                    97 Init DB  fraccdb
                    97 Query    show databases
                    97 Query    show tables
201221  3:50:01     97 Quit