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?
In my experience with Github Actions and MySQL databases, the Can't connect to local MySQL server through socket error is due to the fact you're using localhost, which the linux host apparently can't resolve. It should work if you use 127.0.0.1 however.
Also make sure that if you're creating databases, your user has permissions to do so - since we're testing, I prefer to use a root user with all permissions, to avoid any such issues.
In general, the ideal way for me to use a mysql test server in Github Actions, is by spinning up a container on the host, like so (this example is based on Python/Django, but reusable for any tech stack you're using):
jobs:
tests:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
# The MySQL docker container requires these environment variables to be set
# so we can create and migrate the test database.
# See: https://hub.docker.com/_/mysql
MYSQL_DATABASE: testdb
MYSQL_ROOT_PASSWORD: testrootpass
ports:
# Opens port 3306 on service container and host
# https://docs.github.com/en/actions/using-containerized-services/about-service-containers
- 3306:3306
# Before continuing, verify the mysql container is reachable from the ubuntu host
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout@v3
- name: Install Ubuntu dependencies
run: |
sudo apt-get update
sudo apt-get install libcurl4-openssl-dev libmysqlclient-dev libgirepository1.0-dev
- name: Setup Python
uses: actions/setup-python@v3
with:
python-version: 3.8
- name: Install Python dependencies
run: |
pip install -r requirements.txt
- name: Run tests
# These environment variables will take precedence over the ones in the Django settings file
env:
# Set the DJANGO_SECRET_KEY in the Github repo settings: Go to Secrets and variables > Actions > New repository secret
DJANGO_SECRET_KEY: ${{ secrets.DJANGO_SECRET_KEY }}
DATABASE_NAME: testdb
DATABASE_USERNAME: root
DATABASE_PASSWORD: testrootpass
DATABASE_ENDPOINT: 127.0.0.1 # Will not work with 'localhost', since that will try a Unix socket connection (!)
run: |
python manage.py migrate
python manage.py test
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.:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With