I tried something like this snippet below, but the second task gets executed on the host's shell terminal, rather than the mysql shell. How do i execute commands on the opened mysqlsh in the first task?
Eg: i want to login to the shell and check the status of the mysql cluster.
tasks:
- name: "connect as clusteradmin"
shell: mysqlsh --uri admin#host1 -p'pass'
- name: "get cluster"
shell: var cluster=dba.getCluster('cluster')
- name: "check status of cluster"
shell: cluster.status()
register: Clustersts
try to put your request into one line with parameter '--execute=command'.
Here is something which works for me:
mysqlsh -uUSER -pPASSWORD -P 3306 --execute='util.checkForServerUpgrade()'
Yours could look like as follows:
- name: "connect as clusteradmin and get cluster"
shell: mysqlsh --uri admin#host1 -p'pass' --execute='var cluster=dba.getCluster('cluster')'
and then:
- name: "connect as clusteradmin and check status of cluster"
shell: mysqlsh --uri admin#host1 -p'pass' --execute='cluster.status()'
Related
(See UPDATE at end of post for potentially helpful debug info.)
I have a CircleCI job that deploys MySQL 8 via - setup_remote_docker+docker-compose and then attempts to start a Java app to communicate with MySQL 8. Unfortunately, even though docker ps shows the container is up and running, any attempt to communicate with MySQL--either through the Java app or docker exec--fails, saying the container is not running (and Java throws a "Communications Link Failure" exception). It's a bit confusing because the container appears to be up, and the exact same commands work on my local machine.
Here's my CircleCI config.yml:
Build and Test:
<<: *configure_machine
steps:
- *load_repo
- ... other unrelated stuff ...
- *load_gradle_wrapper
- run:
name: Install Docker Compose
environment:
COMPOSE_VERSION: '1.29.2'
command: |
curl -L "https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o ~/docker-compose
chmod +x ~/docker-compose
sudo mv ~/docker-compose /usr/local/bin/docker-compose
- setup_remote_docker
- run:
name: Start MySQL docker
command: docker-compose up -d
- run:
name: Check Docker MySQL
command: docker ps
- run:
name: Query MySQL #test that fails
command: docker exec -it mysql8_test_mysql mysql mysql -h 127.0.0.1 --port 3306 -u root -prootpass -e "show databases;"
And here's my docker-compose.yml that is run in one of the steps:
version: "3.1"
services:
# MySQL Dev Image
mysql-migrate:
container_name: mysql8_test_mysql
image: mysql:8.0
command:
mysqld --default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
--log-bin-trust-function-creators=true
environment:
MYSQL_DATABASE: test_db
MYSQL_ROOT_PASSWORD: rootpass
ports:
- "3306:3306"
volumes:
- "./docker/mysql/data:/var/lib/mysql"
- "./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf"
- "./mysql_schema_v1.sql:/docker-entrypoint-initdb.d/mysql_schema_v1.sql"
It's a fairly simple setup and the output from CircleCI is positive until it reaches the docker exec, which I added to test the connection. Here is what the output from CircleCI says per step:
Start MySQL Docker:
#!/bin/bash -eo pipefail
docker-compose up -d
Creating network "project_default" with the default driver
Pulling mysql-migrate (mysql:8.0)...
8.0: Pulling from library/mysql
5158dd02: Pulling fs layer
f6778b18: Pulling fs layer
a6c74a04: Pulling fs layer
4028a805: Pulling fs layer
7163f0f6: Pulling fs layer
cb7f57e0: Pulling fs layer
7a431703: Pulling fs layer
5fe86aaf: Pulling fs layer
add93486: Pulling fs layer
960383f3: Pulling fs layer
80965951: Pulling fs layer
Digest: sha256:b17a66b49277a68066559416cf44a185cfee538d0e16b5624781019bc716c122 121B/121BkBBB
Status: Downloaded newer image for mysql:8.0
Creating mysql8_******_mysql ...
Creating mysql8_******_mysql ... done
So we know MySQL 8 was pulled fine (and therefore the previous step worked). Next step is to ask Docker what's running.
Check Docker MySQL:
#!/bin/bash -eo pipefail
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cb6b7941ad65 mysql:8.0 "docker-entrypoint.s…" 1 second ago Up Less than a second 0.0.0.0:3306->3306/tcp, 33060/tcp mysql8_test_mysql
CircleCI received exit code 0
Looks good so far. But now let's actually try to run a command against it via docker exec.
Query MySQL:
#!/bin/bash -eo pipefail
docker exec -it mysql8_test_mysql mysql mysql -h 127.0.0.1 --port 3306 -u root -prootpass -e "show databases;"
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)
Exited with code exit status 1
CircleCI received exit code 1
So now we can't connect to MySQL even though docker ps showed it up and running. I even tried adding an absurd step to wait in case MySQL needed more time:
- run:
name: Start MySQL docker
command: docker-compose up -d
- run:
name: Check Docker MySQL
command: docker ps
- run:
name: Wait Until Ready
command: sleep 120
- run:
name: Query MySQL
command: docker exec -it mysql8_test_mysql mysql mysql -h 127.0.0.1 --port 3306 -u root -prootpass -e "show databases;"
Of course adding a 2 minute wait for MySQL to spin up didn't help. Any ideas as to why this is so difficult in CircleCI?
Thanks in advance.
UPDATE 1: I can successfully start MySQL if I SSH into the job's server and run the same command myself:
docker-compose up
Then in another terminal run this:
docker exec -it mysql8_test_mysql mysql mysql -h localhost --port 3306 -u root -prootpass -e "show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| test_db |
| mysql |
| performance_schema |
| sys |
+--------------------+
So it is possible to start MySQL. It's just not working right when through job steps.
UPDATE 2: I moved the two minute wait between docker-compose up -d and docker ps and now it shows nothing is running. So the container must be starting then crashing and that's the reason for why it's not available moments later.
The cause of the problem was the volumes entry in my docker-compose.yml with this line:
- "./mysql_schema_v1.sql:/docker-entrypoint-initdb.d/mysql_schema_v1.sql"
The container appeared to be up when I checked immediately after docker-compose up -d but in actuality it would crash seconds later because CircleCI appears to have an issue with Docker volume, potentially related to this: https://discuss.circleci.com/t/docker-compose-doesnt-mount-volumes-with-host-files-with-circle-ci/19099.
To make it work I removed that volume entry and added run commands to copy and import the schema like so:
- run:
name: Start MySQL docker
command: docker-compose up -d
# Manually copy schema file instead of using docker-compose volumes (has issues with CircleCI)
- run:
name: Copy Schema
command: docker cp mysql_schema_v1.sql mysql8_mobile_mysql:docker-entrypoint-initdb.d/mysql_schema_v1.sql
- run:
name: Import Schema
command: docker exec mysql8_mobile_mysql /bin/sh -c 'mysql -u root -prootpass < docker-entrypoint-initdb.d/mysql_schema_v1.sql'
With this new setup I've been able to create the tables and connect to MySQL. However, there appears to be an issue running tests against MySQL causing hangups but that might be unrelated. I will follow up with more information, but at least I hope this can help someone else.
Following is the code inside container for the sql:
mysql:
image: mariadb
command: --max_allowed_packet=32505856
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_USER=abc
- MYSQL_PASSWORD=xyz.123
- MYSQL_DATABASE=abc
- MYSQL_ALLOW_EMPTY_PASSWORD=true
restart: always
ports:
- 3306:3306
volumes:
# Using volume mount to mimic prod setup
- /tmp/mysql:/vol_mount/
I have been trying to access the database to view tables but have been unable to do so thus far.
I have tried running commands like:
docker exec -it container_name mysql -uabc -p
and then have tried bot 'root' and 'xyz.123' when prompted to enter password. But then I get an error like
"ERROR 1045 (28000): Access denied for user 'abc'#'2001:db8:1::a7'
(using password: YES)"
I need to know the commands that will allow for me to access the DB and view tables. Thanks.
This will open your mysql console with user root:
docker exec -ti mariadb-test sh -c 'mysql -uroot -p${MYSQL_ROOT_PASSWORD}'
Please note that I named the container mariadb-test in the compose file you can use a different name if you like.
Similarly you can connect with used abc:
docker exec -ti mariadb-test sh -c 'mysql -uabc -p${MYSQL_PASSWORD}'
UPDATE:
Run the following to check the environment variables inside your container. They should match the values provided in the docker-compose.yml.
docker exec -ti mariadb-test sh -c 'env | grep MYSQL'
Since in my solution you don't even have to type the password, it should work anyway. Please try to start from a clean state: bring down all the containers, remove volumes and restart the services with this docker-compose file.
So im trying to import a MySQL dump that contains two databases with Ansible on a Windows box.
The Ansible playbook should be correct.
- hosts: win
gather_facts: no
tasks:
- name: Import DB
win_command: 'mysql -uroot -p12345678910 -hlocalhost < "C:\sql\dump.sql"'
args:
chdir: C:\Program Files\MySQL\MySQL Server 5.6\bin\
On the windows box if I open CMD and run
cd C:\Program Files\MySQL\MySQL Server 5.6\bin\ and then 'mysql -uroot -p12345678910 -hlocalhost < "C:\sql\dump.sql"' it works perfectly.
Im getting a giant error. What am I doing wrong?
Im able to check the DB with a very similar Playbook:
- hosts: win
gather_facts: no
tasks:
- name: Check DB
win_command: 'mysql -uroot -p12345678910 -e "SHOW DATABASES;"'
args:
chdir: C:\Program Files\MySQL\MySQL Server 5.6\bin\
The problem might be input redirection. Perhaps you can try something like:
win_command: 'Get-Content C:\sql\dump.sql | mysql -uroot -p12345678910 -hlocalhost'
See Redirecting standard input\output in Windows PowerShell
I want a docker container that runs a command for it's effects, and then it's fine to stop until I run it again.
I just want to run only this, to delete expired rows:
mysql --user=$MYSQL_USER --host=$MYSQL_HOST --database=$MYSQL_DB --password=$MYSQL_PASS -se "DELETE FROM sometable WHERE expiration < NOW();"
I've tried this:
FROM ?????
ENV MYSQL_USER=$MYSQL_USER
ENV MYSQL_HOST=$MYSQL_HOST
ENV MYSQL_PASS=$MYSQL_PASS
ENV MYSQL_DB=$MYSQL_DB
ENTRYPOINT mysql --user=$MYSQL_USER --host=$MYSQL_HOST --database=$MYSQL_DB --password=$MYSQL_PASS -se "DELETE FROM sometable WHERE expiration < NOW();"
and running it with:
docker run --name some-process \
-e MYSQL_USER=$MYSQL_USER \
-e MYSQL_PASS=$MYSQL_PASS \
-e MYSQL_HOST=$MYSQL_HOST \
-e MYSQL_DB=$MYSQL_DB \
-d some-image:latest
And I've tried many permutations of this, but, I'm really lost.
Any advice what my Dockerfile and docker run command should look like to run that?
EDIT:
I've tried so many things and gotten so many errors that I think it's futile to "fix" my attempts, and instead just solicit, "how do I have a container run mysql -se "DELETE ...""
But if it helps, here are some of the errors I've been getting ( i get the first one most frequently)
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
------
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
------
Error response from daemon: No such container:
------
docker: Error response from daemon: repository my-mysql-server not found: does not exist or no pull access.
------
ERROR: mysqld failed while attempting to check config
------
error: database is uninitialized and password option is not specified
You don't need to build an image to do this. You can just use the MySQL client in the official image:
docker run -it -e MYSQL_PWD=yourpass mysql:5.6 mysql -h $MYSQL_HOST --port 3306 -u youruser -e "DELETE FROM sometable WHERE expiration < NOW();"
I am new to using Travis CI and i am trying to use the MySQL database along with a docker container. I am able to start docker service and run my container or I am able to connect to mysql. But I am not able to do both.
MySQL - Works
language: bash
sudo: required
before_script:
- "mysql -e 'create database atg_core;'"
- "mysql -e 'create database atg_ca;'"
script:
- "mysql -e 'show databases;'"
Docker - Works
sudo: required
language: bash
services:
- docker
install:
- docker pull asnagaraj/oracle-atg-11.1:v4
script:
- docker run -v $TRAVIS_BUILD_DIR:/workspace/test-atg-module asnagaraj/oracle-atg-11.1:v4 /bin/bash -c ". ~/.bash_profile; cd /workspace/test-atg-module; gradle --stacktrace gATGM; "
Combined - Doesnt Work
sudo: required
language: bash
services:
- docker
install:
- docker pull asnagaraj/oracle-atg-11.1:v4
before_script:
- "mysql -e 'create database atg_core;'"
- "mysql -e 'create database atg_ca;'"
script:
- docker run --net="host" -v $TRAVIS_BUILD_DIR:/workspace/test-atg-module asnagaraj/oracle-atg-11.1:v4 /bin/bash -c ". ~/.bash_profile; cd /workspace/test-atg-module; gradle --stacktrace gATGM; telnet localhost 3306; telnet 127.0.0.1 3306"
I am not even expecting the telnet to work, even the mysql -e create commands fail..
Please help.
-Naga
Under the services section of you travis.yml file you will have to let travis know that you are using mysql as such:
services:
- mysql
as taken from https://docs.travis-ci.com/user/database-setup/#MySQL
So your services section will become:
services:
-docker
-mysql