Skip to content

Commit 4f0b998

Browse files
Merge pull request circleci#3412 from circleci/tjs/update-mysql-example
Update MySQL example with proper docker image / example.
2 parents 5b22ad9 + e31834c commit 4f0b998

File tree

1 file changed

+58
-16
lines changed

1 file changed

+58
-16
lines changed

jekyll/_cci2/postgres-config.md

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -212,36 +212,78 @@ jobs:
212212
path: /tmp/test-results
213213
```
214214
215-
## Example Ruby Project with MYSQL and Dockerize
215+
## Example MYSQL project.
216216
217-
The following example uses MySQL and dockerize, see the [sample project on GitHub](https://github.com/tkuchiki/wait-for-mysql-circleci-2.0) for additional links.
217+
The following example sets up MYSQL as a secondary container alongside a PHP container.
218218
219219
```yaml
220220
version: 2
221221
jobs:
222222
build:
223-
working_directory: ~/test-circleci
224223
docker:
225-
- image: circleci/ruby:2.4-node-jessie
226-
- image: tkuchiki/delayed-mysql
224+
- image: circleci/php:7.1-apache-node-browsers # The primary container where steps are run
225+
- image: circleci/mysql:8.3
227226
environment:
228-
MYSQL_ALLOW_EMPTY_PASSWORD: yes
229-
MYSQL_ROOT_PASSWORD: ''
230-
MYSQL_DATABASE: circleci
227+
MYSQL_ROOT_PASSWORD: rootpw
228+
MYSQL_DATABASE: test_db
229+
MYSQL_USER: user
230+
MYSQL_PASSWORD: passw0rd
231+
231232
steps:
232233
- checkout
233234
- run:
234-
name: Bundle install
235-
command: bundle install
236-
- run:
237-
name: Wait for DB
238-
# preinstalled in circleci/* docker image
239-
command: dockerize -wait tcp://127.0.0.1:3306 -timeout 120s
235+
# Our primary container isn't MYSQL so run a sleep command until it's ready.
236+
name: Waiting for MySQL to be ready
237+
command: |
238+
for i in `seq 1 10`;
239+
do
240+
nc -z 127.0.0.1 3306 && echo Success && exit 0
241+
echo -n .
242+
sleep 1
243+
done
244+
echo Failed waiting for MySQL && exit 1
240245
- run:
241-
name: MySQL version
242-
command: bundle exec ruby mysql_version.rb
246+
name: Install MySQL CLI; Import dummy data; run an example query
247+
command: |
248+
sudo apt-get install mysql-client
249+
mysql -h 127.0.0.1 -u user -ppassw0rd test_db < sql-data/dummy.sql
250+
mysql -h 127.0.0.1 -u user -ppassw0rd --execute="SELECT * FROM test_db.Persons"
251+
workflows:
252+
version: 2
253+
build-deploy:
254+
jobs:
255+
- build
243256
```
244257
258+
While it is possible to make MySQL as your primary and only container, this example
259+
does not. As a more practical use case, the example uses a PHP docker image as
260+
its primary container, and will wait until MySQL is up and running before performing any `run` commands
261+
involving the DB.
262+
263+
Once the DB is up, we install the `mysql` client into the primary container so that we can run a command to connect and import the dummy data, presumably found at, `sql-data/dummy.sql` at the root of your project. In this case, that dummy data contains an example set of SQL commands:
264+
265+
```sql
266+
DROP TABLE IF EXISTS `Persons`;
267+
268+
CREATE TABLE Persons (
269+
PersonID int,
270+
LastName varchar(255),
271+
FirstName varchar(255),
272+
Address varchar(255),
273+
City varchar(255)
274+
);
275+
276+
INSERT INTO Persons
277+
VALUES (
278+
1,
279+
"Foo",
280+
"Baz",
281+
"123 Bar Street",
282+
"FooBazBar City"
283+
);
284+
```
285+
286+
245287
## See Also
246288

247289

0 commit comments

Comments
 (0)