In my GitHub Action, I am using a Linux runner (Ubuntu 18.04 and 20.04) and I want to use a MySQL database. So in order to set things up, I run a script like
sudo apt update
sudo apt install mysql-server
sudo mysql < someInstructions.txt
However, when I run this script on GitHub Actions, I get the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
So I figured that the MySQL server is not running after it has been installed (which I verified). This is strange, since the docs of the Ubuntu package explicitly states that the server should be running after a successful installation.
In any case, I then used sudo systemctl start mysql.service
to start the MySQL server but now I get this error
'Access denied for user 'root'@'localhost' (using password: NO)'
This really baffles me as sudo mysql
is supposed to connect to the DB as root. I verified that this works on a regular Ubuntu installation.
Does anyone know how I can connect to a MySQL DB on GitHub Actions?
The source of the confusion is that when using GitHub Actions (or more specifically: one of the GitHub-hosted runners) you get a Linux image in which MySQL is already installed on. Thus, when executing sudo apt install mysql-server
, no actual installation is triggered.
Thus, the default setup that normally occurs after the installation also does not apply since that will never be executed. Instead, GitHub has pre-configured the MySQL server such that the user root
has an explicit password set (which is why a plain sudo mysql
doesn't work). The default password for user root
is root
.
Also notice that the MySQL server is disabled by default, so you have to start it first (as already described in the question). After it is up and running, you can connect to it using
sudo mysql --user=root --password=root
Refs.: