MySQL starts refusing connections when I start my game server - mysql

MySQL works fine until I start my game server. When I start my game server, I get the error "Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306"
If I run the webserver after running the game server, I will get that error as well, but if I run the webserver first, it works fine UNTIL I run the game server.
Similarly if I do mysql -u root -p and try to log in after I start the game server I get the error
' Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' '
I can literally use mysql -u root -p up until I get the first error message from the game server. Does anyone know why this is happening?

Related

mysql_secure_installation ERROR 2002 Can't connect to local MySQL server

After installing Mariadb on Debian 9, I am trying to run /usr/bin/mysql_secure_installation. The script starts okay, but after I press 'enter' for no root password, I get the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
I have to ctl-c to get out of the script.
The socket is set in /etc/mysql/debian.cnf to /var/run/mysqld/mysqld.sock
It is also set in /etc/mysql/mariadb.conf.d/50-server.cnf to /var/run/mysqld/mysqld.sock
The server is not running.
After digging around many, many places, the answer was right under my nose on the Troubleshooting Connection Issues page at the MariaDB website 1
The error means that the server is either not running or running on a different port, socket or pipe. Since I knew the server was not running, I started it with
/etc/init.d/mysql start
before running the script and no more problems with the error or the script.

How do you set up MySQL in Docker and connect to it?

I'm trying to set up an e2e test. For this, I need a local test database, preferably in Docker so that test setup is simple. Here is a minimal project showing what I currently have: https://gitlab.com/MakotoE/mysql-test If this issue is fixed, npm test will finish with exit code 0. This is being run on Windows 10.
In main.js, I start an instance of the Docker image mysql:8.0 like this:
const command = 'docker run -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -p 3306:3306 -d mysql:8.0';
ChildProcess.execSync(command);
I want to connect to it with mysql2, but it fails with an Error: connect ETIMEDOUT.
await mysql.createConnection({
host: 'localhost',
user: 'root',
});
If I try to connect to the database created in the JavaScript code, through command prompt (mysql -uroot), I get this error: ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0. If I create the container in command prompt with the same docker run command used in the code, I can connect successfully, so this issue might be related to ChildProcess.execSync, but I don't know how I can fix this.
How can I fix my code so that I can start a MySQL server in Docker and connect to it in Node.js running on Windows 10?
Edit: I'm not sure if this is related, but running this on Debian shows this error message when creating the container:
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:
Post http://%2Fvar%2Frun%2Fdocker.sock/v1.39/containers/57f5b631e551466e1f9928ba62f7a5e2a5ad2a481da4686bda8684f1a71b8d7/stop: dial unix /var/run/docker.sock: connect: permission denied
And connection fails with ETIMEDOUT.
Edit: I added branch secondAttempt where I unsuccessfully tried running all commands outside the .js file, through NPM scripts. After running the test for this branch, you must manually stop the Docker container.
In the mysql -uroot command, it fails with:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0
which I believe is the same error as experienced previously, with different messages (due to different clients).
This experiment tells me two things: ChildProcess.execSync is not the root cause to this issue, and that there is something about this system (whether it is Docker, or the TCP connection to the Docker) that prevents scripted commands.
I finally figured this out. All I need to do is wait 15 seconds before connecting.
The mistake I made is a common one: not reading the readme. In the mysql Docker Hub page, under section "No connections until MySQL init completes", it mentions how you must wait for database initialization to complete. The example repos achieve this via looping to retry the connection, but I think its simpler to delay for a set period of time.
Before, I was under the impression that I only needed to wait ~1 second after docker run, but actually it takes ~14 seconds for the database to be initialized. The fix was as simple as using setTimeout to wait 15-20 seconds before connecting to the database. I have given an example in branch fixed!.

Problems connecting Laravel to a remote MySQL database: "Lost connection to MySQL server at handshake" and "MySQL server has gone away"

I got this problem where I have developed a Laravel application which uses or connects to a external database.
I have copied all Laravel files in a CentOS server that needs to connect to a MySQL database that is located in another (Ubuntu) server.
Locally, I didn't have any trouble with Laravel Homestead.
But now the Laravel app throws the following error:
SQLSTATE[HY000] [2006] MySQL server has gone away
And if I try to connect to the external database from the CentOS terminal,
$ mysql -h xxx.xxx.xxx.xxx -P 3306 -u user4laravel -p
I get this other error:
ERROR 2013 (HY000): Lost connection to MySQL server at 'handshake:
reading inital communication packet', system error: 11
Notes: I connect to that server via SSH Tunnel with SSH-KEY and passphrase. Do I have to create a tunnel connection?
How do I make it persistent? Is this practical? Or ...
Does anybody know how to solve this or a workaround?

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/lampp/var/mysql/mysql.sock' (111 "Connection refused")

hello guys i have been trying to fix this problem for about 3 hours and can't fix it i want to start mysql in lampp
sudo /opt/lampp/bin/mysql -u root -p
and this is what i got
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/opt/lampp/var/mysql/mysql.sock' (111 "Connection refused")
i dont have mysql server installed and i cant even get into localhost/phpmyadmin as well someone please help me im stuck :(
if you want some specific detail just comment below :)
To connect to local MySQL server, you need to have it installed and running in the first place. Lamp will try to connect to existing (running) MySQL server but it won't start a server on it's own.
Here are the instructions to install and start MySQL server (locally). Once it's up and running, you can connect to it, e.g. with php:
$mysqli = new mysqli("localhost", $username, $password, $database);
Update
If you are running an instance provided by Lamp image then you can check the status by executing service mysqld status command and start it if it's not running. Have a look at this article for troubleshooting.

Laravel homestead Vagrant: ERROR 2002 (HY000): Can't connect to local MySQL server through socket

I set up Laravel HomeStead due do documentation here https://laravel.com/docs/5.2/homestead
After commands
vagrant up
vagrant ssh
My sites works correctly and I can connect to mysql (MariaDB) with default username/password
mysql -uroot -psecret
without problem. But unfortunately my happiness is not long because after 5 mins since moment of initialization vagrant (vagrant up) connection to mysql has been lost and I got error on attempt of connection to mysql from console
vagrant#homestead:~$ mysql -uroot -psecret
ERROR 2002 (HY000): Can't
connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
As a result file /var/run/mysqld/mysqld.sock is disappeared though it has been there at once after initialization (vagrant up)
If say all shortly:
Everything works nice for 5 mins then connection to mysql is lost (
Could someone help me to resolve this issue ?
Thanks in advance,
Yevgenii