How to fix "Error establishing database connection" - mysql

Pretty much every single Wordpress site I've set up with MySQL, sooner or later gives the message Error establishing database connection.
Restarting MySQL fixes it. Until it gets to a certain point, where rebooting the whole server is required, as it just hangs trying to restart it.
This has been various versions of Wordpress, on various versions of Linux.
What is it causing this, and what's the cure?

Can you check the number of open connections using - SHOW STATUS WHERE variable_name = 'Threads_connected';. You can use SHOW processlist to get the number of running process which will show you the number of users. Mostly it is because the credentials are incorrect or the number of open connections have reached the maximum.

Related

Joomla - Could not connect to MySQL - "Sometime"

Error displaying the error page: Application Instantiation Error: Could not connect to MySQL.
This Error is shown sometimes (one every 20 pageviews) and I tried a lot of solutions without any change.
This error found with Joomla 3.5+
This will help you do some testings...
On MySql server run this query...
show processlist;
If this shows a lot of connections then you may be hitting the max_connections problem. Try to increase that to a reasonable number. But, do not make it too much because if it goes beyond the amount of available resources on your server then it will make MySql server crash and it will become a bigger problem where your website will go offline.
Try to check if you are closing sessions properly in your scripts using session_write_close(); You can find more about this function here: http://php.net/manual/en/function.session-write-close.php
This function will write user's current session on disk to be reloaded next time and most important part it will do for you with this problem is that it will close PHP's any connection made to the MySql server, so it will free up the MySql server for next visitor to come and open another page.
Lets say if you have 150 max_connections and at the same time you get 200 visitors, then PHP will still wait for some time for another 50 connections to be free before giving a timeout error where you get "Could not connect to server". But, if sessions are not closing properly, then its so bad that 50 connections will never get processed because 150 will never go away very soon, there is a separate MySql setting for that named wait_timeout. So, any MySql connection you open will keep waiting until you close yourself in script just like you open OR until wait_timeout happens. But, if you close sessions properly then next connection will become available as soon as your PHP script has finished processing and page is generated for the user for viewing and it will make the connection seat free for next connection to come in.
Server RAM was very low!
After upgrade from 0.5GB to 2GB all the kind of error starts with Error displaying the error page: gone.
I found the problem solution by using htop while connecting to the server via SSH, after that, I start opening pages rapidly which show the error plus the htop shows that the RAM was close to full.
show processlist; were helpful to find that the problem not about the connection number.which it may sometimes cost the same error in the same way.

joomla database is not responding

In the joomla 3.7 site, everything was running OK. But last night suddenly it has gone totally down, throwing error msg
"Application Instantiation Error: Could not connect to MySQL"
The last operation performed by me was editing a comment as admin. When clicked "SAVE" after editing, the error appeared. The tables were MYIASM.
The check_database or repair_database option in cpanel also do not respond. In phpMyAdmin, I can view the table list but running any query is halting with error
"Lost connection to MySQL server during query
.
Can not back up the database either from cpanel or phomyadmin or mysqldump.
2006 - MySQL server has gone away
But I have another drupal database in the same host. Tt is serving the site and all the above operation work on it without any problem.
The joomla db was in myIASM format. after all errors, I tried to convert the tables to InnoDB. But the ALTER TABLE query also halted in middle. Some tables are Innodb and some are MYIASM now.
Please help me to solve the problem.
Update:
After some checking, I found that the database is throwing error as
already has more than 'max_user_connections' active connections when
trying to connect
But there is no active connection visible in "show processlist"
In your .cfg or .ini,
log_warnings=2 for logging to your error log, including connection failure details.
To determine how many connections are possible,
SHOW GLOBAL VARIABLES LIKE 'max_connections'
and increase by 10% with
max_connections=(newlimit)
both within [mysqld], shutdown, restart
remember SET GLOBAL general_log=1 will track everything MySQL is trying to do for your analysis when you are having troubles.
Remember to turn OFF general_log before you go to lunch (or anywhere).

MySQL Query running even after losing connection

I've a MySQL 5.1.41 Server installed on a Ubuntu machine. I get connected to it through Workbench from my Windows machine over TCP/IP. I run a bigger query, after 900 seconds I got the below message, (there is no wait_timeout defined in the server's configuration file my.cnf)
Error Code: 2013. Lost connection to MySQL server during query
But when I look into the process list by using show processlist; command, I can still see my query running.
I got this link http://dev.mysql.com/doc/refman/5.0/en/gone-away.html where I found the below lines,
The problem on Windows is that in some cases MySQL does not get an
error from the OS when writing to the TCP/IP connection to the server,
but instead gets the error when trying to read the answer from the
connection.
I'm not sure whether this is the reason for my observation.
Please clarify me on this.
Thanks in advance!!
Closing connection is not a reason to stop a query. A query might be update, or kind of transaction, or select with output to remote (server) file.
Closed connection is just is just means, that you will not receive any data from DBMS after executing query (data, timings - nothing).
The reason of closing connection could be different, as SO-User posted. Try increasing
on server side:
wait_timeout
max_allowed_packet
on client side:
any kinds of timeout you find in your client (i.e. that SO-User suggests)
Do not forget to reload DBMS config and restart client (for sure)
In MySQL WorkBench we have an option to change timeout.
Find it under
Edit → Preferences → SQL Editor → DBMS connection read time out (in seconds): 600
Changed the value to 6000 or something higher.
Update
Lost connection to MySQL server
There are three likely causes for this error message.
Usually it indicates network connectivity trouble and you should check
the condition of your network if this error occurs frequently. If the
error message includes “during query,” this is probably the case you
are experiencing.
Sometimes the “during query” form happens when millions of rows are
being sent as part of one or more queries. If you know that this is
happening, you should try increasing net_read_timeout from its default
of 30 seconds to 60 seconds or longer, sufficient for the data
transfer to complete.
More rarely, it can happen when the client is attempting the initial
connection to the server. In this case, if your connect_timeout value
is set to only a few seconds, you may be able to resolve the problem
by increasing it to ten seconds, perhaps more if you have a very long
distance or slow connection. You can determine whether you are
experiencing this more uncommon cause by using SHOW GLOBAL STATUS LIKE
'Aborted_connects'. It will increase by one for each initial
connection attempt that the server aborts. You may see “reading
authorization packet” as part of the error message; if so, that also
suggests that this is the solution that you need.
If the cause is none of those just described, you may be experiencing
a problem with BLOB values that are larger than max_allowed_packet,
which can cause this error with some clients. Sometime you may see an
ER_NET_PACKET_TOO_LARGE error, and that confirms that you need to
increase max_allowed_packet.
Doc link: Error lost connection
and also check here

Difference In Phpmyadmin Mysql web client and Terminal client

I got problem (#2006 Mysql server gone away) with mysql while connecting and performing some operations through web browser.
Operation Listed below:
When Executing big procedure
Importing database dump
When Access some particular tables It immediately throws "Server gone away".
Refer this question for Scenarios: Record Not Inserted - #2006 Mysql server gone away
Note : The above operations are works fine when I perform through terminal.
I tried some configuration as googing stated. That is set wait_timeout, max_allowed_packet. I checked for the bin_log but it is not available.
But the issues will not rectified.
What is the problem & How can I figure out & fix the issue?
what is the different between access phpmyadmin mysql server from web browser and terminal?
Where I can find the mysql server log file?
Note: If you know about any one of the above questions. Please post here. It would be helpful to trace.
Please help me to figure this out..
Thanks in advance...
Basically nothing except phpMyAdmin is limited by PHP's timeout and resource limits (limits to keep a runaway script from bogging down your entire machine for all eternity; see the docs for details of those values. In some cases, you might be authenticating through a different user account (for instance, root#localhost and root#127.0.0.1 aren't the same user), but as long as you're using a user with the same permissions the differences are minimal.
You can read more about logs in the MySQL manual, note that "By default, no logs are enabled (except the error log on Windows)".
Below are answer for question
From my research the problem is that browser have some limit to disconnect the connection i.e timeout connection. So that the above problem raised.
To resolve this problem
Go to /opt/lampp/phpmyadmin and open config.inc.php
add the command $cfg['ExecTimeLimit'] = 0;
Restart the xamp server. Now you can perform any operations.
`
2. Web client is differ from terminal because Terminal client will not getting timeout. Terminal client maintain the connection till the progress completed. I recommenced to use command prompt to import/export/run process by safe way.
Basically phpmyadmin will not have any log file. If you wanna see warnings and error you should configure the log file.
Configuration steps:
Go to /opt/lampp/etc/my.cnf
Add log_bin = /opt/lampp/var/mysql/filename.log
Restart the xamp server. You can get the log information.

Mysql max user connections bug

I'm facing a really weird problem to me.
I'm trying to connect with an user to my mysql 5.5 instance but it keep saying to me that user already exceeded the max_user_connections
ERROR 1226 (42000): User 'xpto' has exceeded the 'max_user_connections' resource (current value: 100)
But,
show processlist
doesn't shows any connection used by that user. I'm pretty sure that user is not using any connection at all.
If i increase the current value to 110 for example i can connect. Then if i lower i can't connect.
EDIT: the global connections usage is 500 and only a few (10/20) are being used.
Any clue?
The server has too many open connections already. A MySQL server can only handle a specific number of open connections before refusing to allow any more, and this limit is shared amongst all users of the server. It's usually set quite high, although it's easily possible for someone to effectively DoS a MySQL server by making lots of connections.
If you got the error message (code 1226) indicates that the entire MySQL server has run out of connection slots - this is the DoS scenario.
Your database must have a user ‘A’ using which you have configured your wordpress blog.
Now the problem is that user ‘A’ has exceeded maximum_questions resource (current value: 40)
Create a new user ‘B’ in your database through your domain control panel and update the same user name you wp-config.php file in
wordpress installation directory.
Now you got the problem fixed in few minutes. It will not disturb the database or the posts you have in your wordpress or phpbb.
Copied from neuronring blog
Are you on MySQL 5, and you've set the user's MAX_USER_CONNECTIONS, and the user recently disconnected?
From MySQL docs:
For the MAX_USER_CONNECTIONS limit, an edge case can occur if the
account currently has open the maximum number of connections permitted
to it: A disconnect followed quickly by a connect can result in an
error (ER_TOO_MANY_USER_CONNECTIONS or ER_USER_LIMIT_REACHED) if the
server has not fully processed the disconnect by the time the connect
occurs. When the server finishes disconnect processing, another
connection will once more be permitted.