Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How do I use MySQL in Bitbucket Pipelines

SebC
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 12, 2017

How do I get a mysql database to run with by pipeline?

4 answers

1 accepted

1 vote
Answer accepted
SebC
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 12, 2017

You'll want to take advantage of services in bitbucket pipelines.

For the simple uses, you can create an empty database named 'pipelines' and use the 'root' user with password 'let_me_in' to connect on localhost:3306

 

pipelines:
  default:
    - step:
        image: node
        script:
          - npm install
          - npm test
        services:
          - mysql
 
definitions:
  services:
    mysql:
      image: mysql
      environment:
        MYSQL_DATABASE: pipelines
        MYSQL_ROOT_PASSWORD: let_me_in

For more complex situations, you can create a test user to help validate database permissions and other priviledged settings.

The below bitbucket-pipelines.yml will create an empty database named 'pipelines' and user 'test_user' with password 'let_me_in' to connect on localhost:3306. The root user will have a random password created for it and not be accessible.

pipelines:
  default:
    - step:
        image: node
        script:
          - npm install
          - npm test
        services:
          - mysql
 
definitions:
  services:
    mysql:
      image: mysql
      environment:
        MYSQL_DATABASE: pipelines
        MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
        MYSQL_USERNAME: test_user
        MYSQL_PASSWORD: test_user_password

 

Daniele Piaggesi April 17, 2017

I tried with your configuration above:

MYSQL_DATABASE: pipelines
MYSQL_RANDOM_ROOT_PASSWORD: yes
MYSQL_USERNAME: test_user
MYSQL_PASSWORD: test_user_password

but Pipeline said to me that:

The 'environment' section in your bitbucket-pipelines.yml file must be a map.

Obviously it was a map. I remove this environment and put original ones, taken from mysql docker hub page. It worked.

Probably this configuration is broken and you should update your documentation.

Like # people like this
Yuriy Znatokov April 20, 2017

Having the same problem, obviously the configuration posted above and the one in the docs are broken.

How did you manage to solve it?

Like Дмитрий likes this
caiquecssilva April 20, 2017

I had the same issue and i changed to MYSQL_DATABASE: 'pipelines' for example

Yuriy Znatokov April 20, 2017

Got it working with the following config:

environment:
MYSQL_DATABASE: pipelines
MYSQL_ROOT_PASSWORD: root_user_password
MYSQL_USER: test_user
MYSQL_PASSWORD: test_user_password
SebC
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 26, 2017

Thanks for the feedback! Turns out there was an issue where values like "yes" or "true" are being converted to booleans, which is causing some problems when they're loaded into the service container (yes becomes true for example).

Quoting these values will force them to be treated as strings - I'll be discussing this within the Pipelines team to try and improve the experience.

kenkinder November 1, 2018

So, this is still broken, and the documentation is still giving you examples that don't work. Is a fix coming any time?

Ulrich Jahnke January 14, 2019

@SebCPlease change documentation https://confluence.atlassian.com/bitbucket/how-to-run-common-databases-in-bitbucket-pipelines-891130454.html to use

MYSQL_USER

in MySQL - test user section.

Like # people like this
Dheeraj Kumar April 1, 2019

+ mysql -u root -p root -h 127.0.0.1 < test.sql

bash: mysql: command not found

 

while running it i got this error

Like Leonid Lezner likes this
maciejgut August 13, 2019

Try to install something that provides mysql command. For example on Debian image (in my case "php:7.1.29") this installs the standard mysql client:

- apt-get update && apt-get install -y mysql-client

1 vote
Jonas Lomholdt June 22, 2017

I just can't make this work. I get a `Exception: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known` no matter what I do. I have tried a regular user and root user. I also tried using the hostname `mysql`. Nothing works. Can anyone confirm that this is actually a working thing?

I also get a `mysqld: Shutdown complete` at the end of the database service log. Why?

mtumaini October 20, 2017

Just wondering if anyone got a solution to this issue.

I am experiencing same issue with similar mysql configuration. My pipeline reports php_network_getaddresses failure. 

Reece Alexander April 16, 2021

Ensure your connecting to the DB with the hostname 127.0.0.1 and not "localhost" as the latter means nothing in docker.

0 votes
AS November 6, 2020

I eventually got this working. But how can I change the mysql cnf file and then reboot the mysql server/service?
Really struggling with this bit, if anyone has any ideas?

0 votes
Alfred_Bez July 4, 2019

Somehow this does not work for me, here's my bitbucket-pipelines.yml:

image: debian:stretch

pipelines:
pull-requests:
'*':
- step:
script:
- apt-get update && apt-get install -y mysql-client
- mysql -h 127.0.0.1 -u root -ptest_user_password -e "SHOW DATABASES"
definitions:
services:
mysql:
image: mysql:5.7
variables:
MYSQL_DATABASE: 'pipelines'
MYSQL_ROOT_PASSWORD: 'test_user_password'

 Output:

+ mysql -h 127.0.0.1 -u root -ptest_user_password -e "SHOW DATABASES"
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111 "Connection refused")

 Auswahl_767.png

Alfred_Bez July 4, 2019

I forgot to add the service to the pull-requests pipeline:

image: debian:stretch

pipelines:
pull-requests:
'*':
- step:
script:
- apt-get update && apt-get install -y mysql-client
- mysql -h 127.0.0.1 -u root -ptest_user_password -e "SHOW DATABASES"
services:
- mysql

definitions:
services:
mysql:
image: mysql:5.7
variables:
MYSQL_DATABASE: 'pipelines'
MYSQL_ROOT_PASSWORD: 'test_user_password'
Like pcsdev likes this
Usama Habib July 31, 2019

Hi @Alfred_Bez did you experience this error after mentioning services tag in you yml file as well or was it gone? since my pipelines file is through me same error but not everytime time only sometimes i do have this *services* tag but its very unusual sometimes pipeline throws this error and sometime it passes build successfully for the same file. 

 

Any help will be appreciated if you know anything.

Thanks

Alfred_Bez July 31, 2019

Hello @Usama Habib , it works now for me, I don't see the error at all.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events