does any one has an idea how to execute MySQL command inside Kubernetes POD from GitLab Runner ?
My Problem:
I want to create two View Table for my Database that is setup and ready inside a GitLab Pipeline.
My current approach:
1 I read out the wordpress pod infos
MSPOD=$(kubectl get pods --namespace=default -o=jsonpath="{.items[*].metadata.name}" -l app=wordpress,tier=mysql)
2 I try to execute the create table view as single command as i can not sh into the POD via Runner.
kubectl exec $MSPOD -- mysql --database=wordpress --password='M*****?' -e "CREATE VIEW ...;"
But this does not work it actully tries to run the single items of the command in the Terminal.
It also does not work as a embedded execution
kubectl exec $MSPOD -- $(mysql --database=wordpress --password='M*****?' -e "CREATE VIEW ...;")
Causing the same error.
The init container with MySQL client should work for you. Your SQL code can be provided as a configmap.
Related
I am facing one issue with docker, L am using laradock docker env for laravel. Since it has issue with mysql version I had to run those command:
$ docker-compose exec mysql bash
$ mysql -u root -p
ALTER USER 'root'#'localhost' indentified WITH mysql_native_password BY 'root';
Also imported database through http://localhost:8080 and phpmyadmin
So I am trying to reproduce this issue again, so I deleted everything from docker with
$ docker system prune
but when I rebuild the containers
sudo docker-compose up -d nginx mysql phpmyadmin workspace
My previous database is loaded again.
So my question would be how to delete db and MySQL settings, so I can execute the alter command and import database again.
Overall I am trying to determine if this issue with MySQL will occur on another platform again, so I am trying to reproduce it from scratch and that is why I need to reset completely MySQL env and databases.
So not sure where MySQL settings are stored and how to delete them.
MySQL is storing most of the important information of your container in a volume.
Now, the command:
docker system prune
do not remove the volumes, per default.
If you also want to remove them, you can run:
docker system prune --volumes
If you do want to list or act on those specific volumes:
docker volume --help
would give you all the commands on volumes like rm, ls, ...
I'd need to create a PostgreSQL application in Openshift 4 using the 'oc' commandline.
I can see that by adding the app from the Catalog, the application is correctly added.
On the other hand, by using the following shell, the Pod goes into CrashLoopBackOff:
oc new-app -e POSTGRESQL_USER=postgres -e POSTGRESQL_PASSWORD=postgres -e POSTGRESQL_DATABASE=demodb postgresql
The following error is contained in the log file.
fixing permissions on existing directory /var/lib/postgresql/data ... initdb: error: could not change permissions of directory "/var/lib/postgresql/data": Operation not permitted
Which is the correct shell to start up PostgreSQL?
Thanks
I have a script which build my docker env.
I have this line:
docker exec -it guac-mysql /bin/bash
mysql -u root -p123456
CREATE DATABASE guacamole;
CREATE USER 'guacamole' IDENTIFIED BY 123456;
GRANT SELECT,INSERT,UPDATE,DELETE ON guacamole.* TO 'guacamole';
FLUSH PRIVILEGES;
quit
When I run, the script gets in the bash on the container, but the other lines dont run, I just have to run them one by one.
How can I exec and then inject the script?
Thanks :)
you can't run this as a single script. the docker exec command will create a shell into the container. it won't run the rest of it until you exit the container. then it will run the mysql etc...
you need to run docker exec -it guac-mysql /bin/bash
and then you can create a separate script to run the mysql command within the container itself
but your current script won't work. again, it will run mysql and then wait for that to exit before running the create database, etc.
see the docs on executing scripts from a file: https://dev.mysql.com/doc/refman/5.7/en/mysql-batch-commands.html
I have a mysql docker container and it is up and running. I can get into it and see the mysql prompt. I do not want to mount external storage on it. Everytime I start this container, I want to execute a test.sql script from my file system to create DB and do few such actions. When I run the script which exists in my current working directory, it complaints. I know this is trivial but I am unable to catch the issue.
/microservices/mysqlcontainer]docker exec 3a21e5e3669d /bin/sh -c 'mysql -u root -ppwd <./test.sql'
/bin/sh: ./test.sql: No such file or directory
Since your script is outside the container, there's no need to catch the shell input redirection. You should be able to run the following:
docker exec -i 3a21e5e3669d mysql -u root -ppwd <./test.sql
I’m looking to create a deploy script that I can run from a terminal and it automatically deploys my site from a repository. The steps I’ve identified are:
Connect to remote server via SSH
Fetch latest version of site from remote repository
Run any SQL patches
Clean up and exit
I’ve placed the SSH connection and git pull commands in my shell file, but what I’m stuck with is MySQL with it being an (interactive?) shell itself. So in my file I have:
#!/bin/bash
# connect to remote server via SSH
ssh $SSH_USER#$SSH_HOST
# update code via Git
git pull origin $GIT_BRANCH
# connect to the database
mysql --user $MYSQL_USER --password=$MYSQL_PASSWORD --database=$MYSQL_DBNAME
# run any database patches
# disconnect from the database
# TODO
exit 0
As you can see, I’m connecting to the database, but not sure how to then execute any MySQL statements.
At the moment, I have a directory containing SQL patches in numerical order. So 1.sql, 2.sql, and so on. Then in my database, I have a table that simply records the last patch to be run. So I’d need to do a SELECT statement, read the last patch to be ran, and then run any neccesary patches.
How do I issue the SELECT statement to the mysql prompt in my shell script?
Then what would be the normal flow? Close the connection and re-open it, passing a patch file as the input? Or to run all required patches in one connection?
I assume I’ll be checking the last patch file, and doing a do loop for any patches in between?
Help here would be greatly appreciated.
Assuming you want to do all the business on the remote side:
ssh $SSH_USER#$SSH_HOST << END_SSH
git pull origin $GIT_BRANCH
mysql --user $MYSQL_USER --password=$MYSQL_PASSWORD --database=$MYSQL_DBNAME << END_SQL
<sql statements go here>
END_SQL
END_SSH
You could get the output from mysql using Perl or similar. This could be used to do your control flow.
Put your mysql commands into a file as you would enter them.
Then run as: mysql -u <user> -p -h <host> < file.sqlcommands.
You can also put queries on the mysql command line using '-e'. Put your 'select max(patch) from .' and read the output in your script.
cat *.sql | mysql --user $MYSQL_USER --password=$MYSQL_PASSWORD --database=$MYSQL_DBNAME