Unable to connect to MySQL 5.7 and create database

Greetings!

Here is my config:

# PHP CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
version: 2
jobs:
  build:
    docker:
      - image: circleci/php:7.2-node-browsers
      - image: circleci/mysql:5.7-ram

    steps:
      - checkout

      - run: sudo apt update # PHP CircleCI 2.0 Configuration File# PHP CircleCI 2.0 Configuration File sudo apt install zlib1g-dev libsqlite3-dev
      - run: sudo docker-php-ext-install zip bcmath
      - run: sudo apt-get install mysql-client

      # Download and cache dependencies
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "composer.lock" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-

      # Retrieve Laravel Nova using credentials stored in CircleCI
      - run: composer config http-basic.nova.laravel.com ${NOVA_USERNAME} ${NOVA_PASSWORD}

      - run: composer install -n --prefer-dist

      - save_cache:
          key: v1-dependencies-{{ checksum "composer.lock" }}
          paths:
            - ./vendor
      - restore_cache:
          keys:
            - node-v1-{{ checksum "package-lock.json" }}
            - node-v1-
      - run: npm install
      - save_cache:
          key: node-v1-{{ checksum "package-lock.json" }}
          paths:
            - node_modules

      # prepare the database
      - run: mysqladmin create test --user="root" --password="root"
      - run: php artisan migrate --env=testing --force

      # run tests
      - run: ./vendor/bin/phpunit

When it reaches - run: mysqladmin create test --user="root" --password="root" I get the following error:

mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!

I’ve tried rerunning the job with SSH to see if I can do it there, but nothing seems to work. I’m baffled as to what I could be doing wrong, and am even referencing other CircleCI configs that have worked.

I’ve been at this for hours and would greatly appreciate it if someone could point me in the right direction.

Thank you!

You’re trying to connect to a socket, but MySQL is running on the local host interface. Update your connection string to connect to 127.0.0.1 instead of localhost and that should work.

Thanks, @levlaz! That got me one step further. Now the php artisan migrate is throwing the following:

php artisan migrate --env=testing --force

In Connection.php line 664:
                                                                               
  could not find driver (SQL: select * from information_schema.tables where t  
  able_schema = test and table_name = migrations)                              
                                                                               

In PDOConnection.php line 31:
                         
  could not find driver  
                         

In PDOConnection.php line 27:
                         
  could not find driver  
                         

Error: Exited with code 1

And here’s what’s inside the .env.testing file:

APP_ENV=testing
APP_DEBUG=true
APP_KEY=base64:SbiPo5u4ysov1BPULOpIYrj9r+n3OKy8tpZE8g/2tdQ=

TELESCOPE_ENABLED=false

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=root

The mysql driver is what’s used everywhere else, so I know that’s not the issue. Is something not installed that’s needed?

Thank’s so much for your help!

Where are you defining the php-mysql library? Is it in your composer file, or do you expect it to be in the environment? You might need to install it as a part of this step. run: sudo apt-get install mysql-client

Note that mysqladmin does not use the same driver as PHP would.

I looked into this a bit more and it looks like the php-mysql package is missing in the base image upstream.

# prevent Debian's PHP packages from being installed
# https://github.com/docker-library/php/pull/542
RUN set -eux; \
	{ \
		echo 'Package: php*'; \
		echo 'Pin: release *'; \
		echo 'Pin-Priority: -1'; \
} > /etc/apt/preferences.d/no-debian-php

This is the package that I am used to installing to connect PHP and MySQL together.

It looks like instead, its adding a module called mysqlnd.

--enable-mysqlnd is included here because it's harder to compile after the fact than extensions are (since it's a plugin for several extensions, not an extension in itself)

--enable-mysqlnd \

Then I found this post: https://blog.cloud66.com/deploying-your-laravel-php-applications-with-cloud-66/

It looks like you need to run a special command to get this to work.

docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd

Can you give that a shot and see how it goes?

1 Like

Wow, thanks so much for doing all that research @levlaz!

I just got it working! Here’s the final config that’s working, and I’ll call out the changes:

# PHP CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
version: 2
jobs:
  build:
    docker:
      - image: circleci/php:7.2-node-browsers
      - image: circleci/mysql:5.7-ram
        environment:
          - MYSQL_ROOT_PASSWORD=root

    steps:
      - checkout

      - run: sudo apt update # PHP CircleCI 2.0 Configuration File# PHP CircleCI 2.0 Configuration File sudo apt install zlib1g-dev libsqlite3-dev
      - run: sudo docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd
      - run: sudo docker-php-ext-install zip bcmath pdo_mysql
      - run: sudo apt-get install mysql-client

      # Download and cache dependencies
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "composer.lock" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-

      # Retrieve Laravel Nova using credentials stored in CircleCI
      - run: composer config http-basic.nova.laravel.com ${NOVA_USERNAME} ${NOVA_PASSWORD}

      - run: composer install -n --prefer-dist

      - save_cache:
          key: v1-dependencies-{{ checksum "composer.lock" }}
          paths:
            - ./vendor
      - restore_cache:
          keys:
            - node-v1-{{ checksum "package-lock.json" }}
            - node-v1-
      - run: npm install
      - save_cache:
          key: node-v1-{{ checksum "package-lock.json" }}
          paths:
            - node_modules

      # prepare the database
      - run: mysqladmin create test --host="127.0.0.1" --user="root" --password="root"
      - run: php artisan migrate --env=testing --force

      # run tests
      - run: ./vendor/bin/phpunit

The changes of note are:

  • Added the MYSQL_ROOT_PASSWORD=root environment variable (necessary for my test config)
  • Added pdo_mysql to the docker-php-ext-install line
  • Added the sudo docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd line
  • Changed mysqladmin create to use 127.0.0.1 instead of localhost

That was it and it worked great! Thanks again!

2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.