Search code examples
mysqlruby-on-railsgithub-actions

MySQL Container Fails to Start in GitHub Actions


I am trying to set up a Rails app for CI in GitHub Actions that uses MySQL for persistence. My main.yml looks like this:

name: CI

on:
  pull_request:
    branches: ['*']
  push:
    branches: ['*']
jobs:
  build:
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:8
        env:
          MYSQL_HOST: 127.0.0.1
          MYSQL_DATABASE: SHOP_test
          MYSQL_USER: ${{secrets.MYSQL_USER}}
          MYSQL_PASSWORD: ${{secrets.MYSQL_PASSWORD}}
          MYSQL_ROOT_PASSWORD: root
        ports:
          - 3306:3306
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
    steps:
      - uses: actions/checkout@v2
      - name: Setup Ruby 3.2.2
        uses: ruby/setup-ruby@v1
        with:
          bundler: default
          bundler-cache: true

      - name: Install dependent libraries
        run: sudo apt-get install mysql-server

      - name: Bundle install
        run: |
          gem install bundler
          bundle install --jobs 4 --retry 3

      - name: Setup Database
        run: |
          bundle exec rake db:create
          bundle exec rake db:schema:load
        env:
          RAILS_ENV: test
          MYSQL_USER: root
          MYSQL_PASSWORD: root

      - name: Run RSpec
        run: COVERAGE=true bundle exec rspec  --require rails_helper
        env:
          RAILS_ENV: test
          MYSQL_USER: root
          MYSQL_PASSWORD: root

When I push some code to the remote branch, it will successfully set up the jobs, but when it needs to initialize containers, it starts up MySQL, and then stops the server and returns an error.

GitHub returns this in the actions console:

 /usr/bin/docker logs --details fe4f75e7be4cf7269b7e25a45ac808125db82578ea683e5b36baf50ab8bd3535
   2023-10-16 02:18:55+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.1.0-1.el8 started.
   2023-10-16T02:18:56.007684Z 0 [System] [MY-015017] [Server] MySQL Server Initialization - start.
   2023-10-16 02:18:55+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
  Warning:  2023-10-16T02:18:56.009607Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
   2023-10-16 02:18:55+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.1.0-1.el8 started.
   2023-10-16T02:18:56.009701Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.1.0) initializing of server in progress as process 80
   2023-10-16T02:18:56.016595Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
   2023-10-16T02:18:56.316133Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
  Warning:  2023-10-16T02:18:57.312550Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
   2023-10-16T02:18:59.151999Z 0 [System] [MY-015018] [Server] MySQL Server Initialization - end.
   2023-10-16T02:18:59.205278Z 0 [System] [MY-015015] [Server] MySQL Server - start.
  Warning:  2023-10-16T02:18:59.443722Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
   2023-10-16T02:18:59.445793Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.1.0) starting as process 122
   2023-10-16T02:18:59.458386Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
   2023-10-16T02:18:59.620820Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
  Warning:  2023-10-16T02:18:59.812258Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
   2023-10-16T02:18:59.812293Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
  Warning:  2023-10-16T02:18:59.813696Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
   2023-10-16T02:18:59.830687Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
   2023-10-16T02:18:59.830941Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.1.0'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
   2023-10-16T02:18:59.833484Z 0 [System] [MY-015016] [Server] MySQL Server - end.
   Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
   Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
   Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
   Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
   Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
   Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
   2023-10-16T02:19:01.763894Z 13 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.1.0).
   2023-10-16T02:19:03.296520Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.1.0)  MySQL Community Server - GPL.
   2023-10-16T02:19:03.299884Z 0 [System] [MY-015016] [Server] MySQL Server - end.
   2023-10-16T02:19:03.780491Z 0 [System] [MY-015015] [Server] MySQL Server - start.
  Warning:  2023-10-16T02:19:04.010820Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
   2023-10-16T02:19:04.012121Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.1.0) starting as process 1
   2023-10-16T02:19:04.019105Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
   2023-10-16T02:19:04.164169Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
  Warning:  2023-10-16T02:19:04.319860Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
   2023-10-16T02:19:04.319900Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
  Warning:  2023-10-16T02:19:04.321297Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
   2023-10-16T02:19:04.338712Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
   2023-10-16T02:19:04.339257Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.1.0'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
   2023-10-16 02:18:55+00:00 [Note] [Entrypoint]: Initializing database files
   2023-10-16 02:18:59+00:00 [Note] [Entrypoint]: Database files initialized
   2023-10-16 02:18:59+00:00 [Note] [Entrypoint]: Starting temporary server
   2023-10-16 02:18:59+00:00 [Note] [Entrypoint]: Temporary server started.
   '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
   2023-10-16 02:19:01+00:00 [Note] [Entrypoint]: Creating database SHOP_test
   2023-10-16 02:19:01+00:00 [Note] [Entrypoint]: Creating user ***
   2023-10-16 02:19:01+00:00 [Note] [Entrypoint]: Giving user *** access to schema SHOP_test
   
   2023-10-16 02:19:01+00:00 [Note] [Entrypoint]: Stopping temporary server
   2023-10-16 02:19:03+00:00 [Note] [Entrypoint]: Temporary server stopped
   
   2023-10-16 02:19:03+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
   
  Error: Failed to initialize container mysql:8
Error: One or more containers failed to start.

I'm not sure what I'm doing wrong or missing in the setup. Am I adding something unnecessary to this .yml or am I missing something that GitHub needs?


Solution

  • Might have to do with your health check - it's --health-cmd pg_isready; in this case you're running mysql:8 so should be --health-cmd "mysqladmin ping". I was just debugging something similar and this corrected it, so try that.

    Also as a possible gotcha, I'm pretty sure the mysql docker image doesn't let you set MYSQL_USER to root anymore, so make sure you aren't doing that