Website is not working, showing Error establishing database connection


What is the Issue?

Website is showing an error establishing database connection in the browser. This means that for some reason your php source code is not able to connect your mysql database, to retrieve the informations which needs to be be placed on your web page.If you are having the dynamic web pages rather than static web pages on the server then you need to proceed next steps which we have mentioned below.


Why has this issue occurred?

This problem or issues may be due to any one of the following problem :

Incorrect Database Credentials: If the database is created with appropriate username and password ,then you need to check whether same login credentials are given under your database configuration file which you have placed under public_html. If no database is created and you are placed incorrect database connection details on your configuration file will result in connectivity error.

Database got corrupted: If your database got corrupted you are not able to perform any functions like displaying dynamic data, insertions of data’s and so on. Database Corruption will be due to following Reasons: 

a)  Improper shutdown of mysql server due to power outage.

b) Using “killall -9 mysqld” command to shutdown the server and killing all the ongoing mysql processess.

c) Mysqld Services got stopped due to high load on the server or RAM usage exceeded its limit.

d) Having a large amount of data’s in your database.You should make sure that there is enough disk space to store the data in advance.


How can we prevent it?

Follow any one of the following steps to prevent from the problem which is mentioned above:

1) You need to check whether you have written proper database connection credentials under database configuration file. If you found no data or improper database name,username and password then you need to check the proper details and update it on your source code.

2) If you have written proper database connectivity details like database name, username, password but still you are facing connectivity issue then you need to check whether mysql services status by using the command:

service mysql status

To restart the mysql services use this command:

service mysql restart


Check whether database got corrupted or not using the command:

mysqlcheck --repair --all-databases

If the database cannot get repaired you need to upload the previous backup of databases.

Next Step you need to optimize your mysql for faster response so that you can reduce too many connections and reduce RAM usage.

Use  Mysql Optimisation Code Under /etc/my.cnf file:

vim /etc/my.cnf

Remove all the lines above [mysqld_safe] and enter the below lines:

 

query_cache_size = 64M
query_cache_limit = 2M
join_buffer_size = 8M
tmp_table_size = 512M
key_buffer = 32M
innodb_buffer_pool_size = 1024M
innodb_thread_concurrency = 2
max_allowed_packet=268435456
max_user_connections=100
max_connections = 251
read_buffer_size = 2M
open_files_limit=50000
innodb_log_file_size=256M
interactive_timeout=180
wait_timeout=180

 


Did you find this article useful?