Check if mariadb can be connected to and fix if not - mysql

I am selecting and inserting data in a mariadb database with my node.js web app. In the mariadb node.js documentation (https://mariadb.com/kb/en/getting-started-with-the-nodejs-connector/), it begins by querying the database with "SELECT 1 as val", and then after that, it executes the actual query, which is an insert in the example provided. It is my understanding that "SELECT 1 as val" is used to check a connection with the database can be achieved, because otherwise, if you were to query the database without checking, and a connection could not be achieved, the entire web app would crash.
My question is, is "SELECT 1 as val" the best way to check if a connection with the database can be achieved? It is true that if "SELECT 1 as val" fails, the web app will not crash? Also if a connection cannot be achieved, do I fix it? Do I have to redefine the 'pool' block again? Or the pool.connection block? Is there something I can do to restart the database server?

Don't bother with such a "ping". Instead, always check for errors after running queries.
If the error says that you lost the connection, restart your transaction.
If the server is dead, you have no way to repair that.

Related

Initial database connection error at the start of the day

Context: Telephony system (Asterisk) using the MySQL C API to connect to the database to lookup the routing for a call as it comes in. The lookup involves connecting to the database, executing a query, then closing the connection.
Sometimes the very first call in the morning generates the following error:
Access denied for user 'asterisk'#'127.0.0.1' (using password: YES)
Normally this would mean the password was wrong, but that's obviously not the case here, since it uses the same user and password all the time for all the calls. It's as if the system has somehow "gone to sleep" or perhaps a file handle has become stale somewhere, so that the first attempt to the connect to the database fails, but the rest work fine. Also it only happens occasionally, so I'm unable to replicate it - very strange!
I'm using Asterisk 1.8.32 with MySQL 5.5 on Debian 8.7.
It's a bit of a headscratcher, so I would be grateful for any suggestions!
First of all it is very bad idea use 1.8.* tree at current moment becuase of security feature.
Move to 11.* fix this issue.
Also you can do following in my.cnf
interactive_timeout=
Set to any value more then 4 days(weekend)
Other option is reload mysql module by crontab every 3 hrs.
Best option(except upgrade) is move from mysql to res_odbc, which have keepalive option. res_config_mysql considering deprecated, so any new systems should use ODBC.

How to set active my database from sleep command

I'm currently using MySQL Query browser to view the database of my website. Then I encountered this error
error 1049 unknown database 'testajax'
I already dropped this database after using it to test an ajax function. Then my website did not work liked it used to before. Specifically, I cannot INSERT but I can fetch some of the data from my database. I'm a beginner in MySQL, I looked at the tools tab, MySQL Administrator > Server Connections and then I saw this: (Please click to see the image) I found that my database (crb). Is on sleep command. Is this the reason that's why I cannot use my database? If yes, how can I set my database to active to use it again?

Sequelize.js: how to handle reconnection with MySQL

I've always used Mongo with Node, but now due to an existing datasource I need to connect a node app with Mysql.
Sequelize seems a good solution, but I don't get how to handle connection error, reconnection and re-tries.
To check for connection error on first run .authenticate().then().catch(function(error){...});
But what if I loose connection and want to reconnect?
There is an open issue for this in Sequelize:
https://github.com/sequelize/sequelize/issues/2113
Based on that, this error is handled in sequelize.
I verified the version 4.11.1 of sequelize has this issue fixed.
The queries will fail when the database server is down, but will recover to reconnect and succeed when the database server is up.
(You don't need to restart the application as faced with previous versions.)

MySQL Connection lost after successfull query

This question is theoretical. I've no real use case; I'm just trying to understand the MySQL behaviour.
Suppose I send a query (or a transaction) to the server (using transactional tables of course), and the query or transaction executes fine, but the connection is lost before the client (f.e., mysql or an App connecting to a remote server throught a C interface or any other framework like QtSQL) receives the answer of the server. So, the server knows the transaction finished properly, but the client doesn't because the answer didn't arrive.
What does it happen in this case? Does the server roll back the transaction even knowing that it finished succesfully? Any option to control the behaviour in these scenaries?

flushing database cache in SWI-Prolog

We are using swi-prolog to run our testcases. Whenever the test starts, I am opening the connection to MYSQL database and storing the Name of the Test hat is being done and then closing the DB. These tests run for about 2 days continuously. After the tests are done, the results basically gets stored in folder in the server. There is a predicate in another prolog file that is called to update the results to the MYSQL database. The code is simple, I use odbc library and just call odbc_* predicates to connect and update the mysql by issuing direct queries.
The actual problem is :
If I try to call the Predicate from the same Prolog window, where the test just got completed, I get an error as updating to the DB server. Although I do not get any error in the connection. If I close the session of that prolog with halt and closing all the open prolog windows , then open an other complete new instance of Prolog and run the predicate the update goes well.
I have a feeling that there is some connection reference to the MySQL DB in Prolog database. Is there any way to clear the database in prolog so that I can run the same predicate without closing any existing prolog windows?
Any ideas appreciated.
Thanks.
If you open the connection, than do a long processing, MySQL can drop the connection in between after a certain timeout (that I believe can do configured in my.cnf).
EDIT: swi-prolog has an odbc_disconnect that can be used to explicitly close the connection after using it and an "aliasing" mode that can be used to obtain a previously opened connection when using odbc_open. In you case you can try either closing the connection after using it. You should also avoid using an alias when opening.