在Bitbucket Pipelines构建命令中访问MySQL遇ERROR 2002故障求助
Hey there, let's get that MySQL connection error sorted out for your CI pipeline! The core issue here is straightforward: your current Bitbucket Pipeline setup only installs the mysql-client (the tool to connect to MySQL), but there's no running MySQL server in your CI container to connect to.
Here are two reliable solutions to fix this, depending on your needs:
Option 1: Install and start MySQL directly in your step
If you want a quick fix without changing your pipeline structure too much, you can install the MySQL server package and start the service right in your script steps:
image: theotherperson/php-ci:5.6 pipelines: default: - step: caches: - composer script: # Install both client and server packages - apt-get update && apt-get install -y unzip mysql-client mysql-server # Start the MySQL service - service mysql start # Optional: Set a root password for your tests (adjust as needed) - mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'testpassword';" # Rest of your existing steps - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer - composer install --no-scripts --no-plugins - cp test-assets/vhosts/000-default.conf /etc/apache2/sites-available/ # Add your test commands that need MySQL here
Notes for this option:
- This works but can slow down your pipeline a bit, since you're installing and starting MySQL every time the step runs.
- Some base images might use
systemctlinstead ofservice, but most lightweight CI images stick withservicefor starting services.
Option 2: Use Bitbucket Pipelines Service Containers (Recommended)
For a cleaner, more efficient setup, use Bitbucket's built-in service containers. This spins up a separate MySQL container alongside your main CI container, which is faster and more maintainable:
image: theotherperson/php-ci:5.6 pipelines: default: - step: caches: - composer # Declare that this step needs the MySQL service services: - mysql script: # You only need mysql-client here (no server installation) - apt-get update && apt-get install -y unzip mysql-client - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer - composer install --no-scripts --no-plugins - cp test-assets/vhosts/000-default.conf /etc/apache2/sites-available/ # Important: Connect to the MySQL service using the host name `mysql` (not localhost!) - mysql -h mysql -u root -p"testpassword" -e "CREATE DATABASE IF NOT EXISTS test_db;" # Add your test commands here # Define the MySQL service configuration definitions: services: mysql: # Use a MySQL version compatible with your project (5.7, 8.0, etc.) image: mysql:5.7 environment: # Set environment variables to configure MySQL MYSQL_ROOT_PASSWORD: testpassword MYSQL_DATABASE: test_db # Optional: Pre-create a database for your tests
Key tips for service containers:
- Connection host: Use
mysqlas the host name (notlocalhost), since the MySQL service runs in a separate container. Bitbucket automatically links the containers so they can communicate. - MySQL 8.0 note: If you use MySQL 8.0, you might need to adjust the authentication plugin to avoid connection issues. Update the service definition like this:
mysql: image: mysql:8.0 environment: MYSQL_ROOT_PASSWORD: testpassword MYSQL_ROOT_HOST: "%" command: --default-authentication-plugin=mysql_native_password
Pick the option that fits your workflow best—service containers are the way to go for long-term CI setups, while the first option is great for quick tests.
内容的提问来源于stack exchange,提问作者Adam Copley

