I've used a golem pipeline to package & dockerize my app.
For starters, I am trying to deploy the app locally on windows pc using docker (also tried to run it on linux with a same problem). The app collects the data from a local SQlite database also running on my pc (which will be similar once deployed on a server).
When I run the app as a package, app functions alright.
But once I create a docker image & run it, the app launched but is unable to connect to my local sql database, returning this error:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
Connection to the database inside app looks like this:
con = dbConnect(RMariaDB::MariaDB(), dbname = "training_dash_db", user = "root", password = "", host = '127.0.0.1')
My docker file looks like this:
FROM rocker/tidyverse:3.5.3
RUN R -e 'install.packages("remotes")'
RUN R -e 'remotes::install_github("r-lib/remotes", ref = "97bbf81")'
RUN R -e 'remotes::install_cran("shiny")'
RUN R -e 'remotes::install_github("Thinkr-open/golem")'
RUN R -e 'remotes::install_cran("processx")'
RUN R -e 'remotes::install_cran("attempt")'
RUN R -e 'remotes::install_cran("DT")'
RUN R -e 'remotes::install_cran("glue")'
RUN R -e 'remotes::install_cran("htmltools")'
RUN R -e 'remotes::install_cran("shinydashboard")'
RUN R -e 'remotes::install_cran("shinydashboardPlus")'
RUN R -e 'remotes::install_cran("lubridate")'
RUN R -e 'remotes::install_cran("dplyr")'
RUN R -e 'remotes::install_cran("purrr")'
RUN R -e 'remotes::install_cran("plotly")'
RUN R -e 'remotes::install_cran("DBI")'
RUN R -e 'remotes::install_cran("tibbletime")'
RUN R -e 'remotes::install_cran("tsibble")'
RUN R -e 'remotes::install_cran("shinyWidgets")'
RUN R -e 'remotes::install_cran("leaflet")'
RUN R -e 'remotes::install_cran("pool")'
RUN R -e 'remotes::install_cran("RMariaDB")'
RUN R -e 'remotes::install_cran("roxygen2")'
COPY K2dashboard_*.tar.gz /app.tar.gz
RUN R -e 'remotes::install_local("/app.tar.gz")'
EXPOSE 80
EXPOSE 3306
CMD R -e "options('shiny.port'=80,shiny.host='0.0.0.0');K2dashboard::run_app()"
Thanks.
Here are the issues I can see:
You're using 127.0.0.1 as a host for your database. Once in the container, this address refers to the inner IP of the container, not the one from your host machine / another container. So your app can't access the host DB.
You haven't installed the drivers for MariaDB inside your container
Here are solutions:
You can use the r-db docker image as a source : http://colinfay.me/r-db/, which contains drivers for MariaDB: http://colinfay.me/r-db/mariadb-rmariadb.html
Depending on wether your MYSQL is in a docker or not, but you can create a Docker network, and plug your mysql container and your golem container on this network. Then use host = "my-network". See the r-db documentation for information about that: http://colinfay.me/r-db/intro.html#creating-docker-network, and http://colinfay.me/r-db/mariadb-rmariadb.html for the mariadb part
You want to access the database on your machine from inside the container: see How to access host port from docker container for information about that.
Related
I want to run a query agains mysql database stored in openshift.
As far I know there are two ways to do that:
1 - Make a port forward of the mysql pod and run the query pointing to that port from my local environment.
oc port-forward mysql-9-999aa 3306:3306
2 - run a rsh of the pod and execute mysql commands
oc rsh mysql-9-999aa
> mysql ...
However I want to ask if there is a way to run the query at once with exec command, something like:
oc exec mysql-9-999aa mysql --user=test --password=test myDatabase -e "SELECT * FROM test;"
I've tried that and it's not working. Anybody knows if it's possible to run a MySQL command with oc exec?
Thanks,
Try the following:
oc exec mysql-9-999aa -- bash -c "mysql --user=test --password=test myDatabase -e 'SELECT * FROM test;'"
I'm setting an script to configure Docker environments for developer local machines. A specifically part of the script consists to create a temporary container with a local volume to set the database i'll use in next step. this is the code used in the script:
docker run -d --name mysql_temp -v ~/dev/mysql:/var/lib/mysql/data -e MYSQL_ROOT_PASSWORD=test -p 3306:3306 centos/mysql-57-centos7:latest
the next step in the script is this line:
mysql -u root -h 127.0.0.1 --protocol=tcp --password=test -e "CREATE DATABASE db_test;"
then, the script returns me this error:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2
Curiously, if i force the exit with exit command after the first command and i execute the second one by my hand, directly in the terminal, it works and creates me the database.
Any idea what is this happening?
thanks in advance,
In your script, when the first line executes, it creates the docker container. But it takes time to initialise the mysql server on the container. The script executes the second line without waiting for mysql to initialise. Hence it cannot connect.
I'm assuming you have written a shell script. Try the following script.
docker run -d --name mysql_temp -v ~/dev/mysql:/var/lib/mysql/data -e MYSQL_ROOT_PASSWORD=test -p 3306:3306 centos/mysql-57-centos7:latest
sleep 10
mysql -u root -h localhost --protocol=tcp -ptest -e "CREATE DATABASE db_test;"
Note the second line having sleep 10. Basically this will create a delay of 10 seconds before executing the next command. Try varying the sleep time to higher if it still doesn't work.
Also change 127.0.0.1 to localhost and --password=test to -ptest
Note: You said that if you exit the script and run the mysql command manually, it works. Thats because by the time you tried running manually, the container has initialised mysql. Try running the docker run command in one terminal window. Once the container starts, try running the mysql command immediately in another terminal window to see if it works as quickly as you expected.
I am using Docker to create a dockerfile with mysql as the base image:
FROM mysql
#set root pass
ENV MYSQL_ROOT_PASSWORD password
#update linux
RUN apt-get update
#create database
RUN mysql -u root -ppassword -e "CREATE DATABASE dbname"
#install vim
RUN apt-get install vim -y
The dockerfile fails on the step where I try to create a database, it doesn't finish building and i receive this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
When I remove the #create database run command the dockerfile will build and I am able to run a container from that image. I know that it isn't a problem with the mysql server as I can enter the container and run the mysql command manually with success and the service status is running.
Using an environment variable i.e MYSQL_ROOT_PASSWORD within the file also allows me to create a database successfully but this will only work with a single database, I need to be able to use the mysql command to make queries, such as creating additional databases / assigning users etc.
This may be because I need to specify the host and port of the docker container but this still does not allow me to connect
RUN mysql -u root -ppassword -h 127.0.0.1 -P 3308 -e "CREATE DATABASE dbname"
Strangely, doing this also often crashes the container and puts it in a state where it will crash again on start-up every time that I try to restart it again.
I think the issue might be that in the service hasn't started within the container used to build your Dockerfile.
Try starting and configuring MySQL server within a single step. As a reference please check this file: https://github.com/dockerfile/mysql/blob/master/Dockerfile
Use below-given commands in your Dockerfile:
RUN service mysql restart && echo 'CREATE DATABASE db_name;' | mysql -uroot -
pYOUR_ROOT_PASSWORD
Had the very same problem: When starting the container and running a set of RUN instructions, or .sh or .sql scripts in /docker-entrypoint-initdb.d/ no connection to the database server could be established.
I found the solution by a comment of #wpalmer on the mysql-image:
The init scripts run by the entrypoint, internally, use the variable "${mysql[#]}" to call mysql (for example, when loading .sql files placed in the docker-entrypoint-initdb.d directory. Any .sh files which are processed by the entrypoint are included by "sourcing" them, meaning that variable is available for use by any .sh files which are run).
So what this means for you, instead of providing the plain mysql command with user, pass etc. as in
RUN mysql -u root -ppassword -e "CREATE DATABASE dbname"
use the placeholder instead:
RUN "${mysql[#]}" -e "CREATE DATABASE dbname"
You can try to build other image and run the create DB from there.
Example of docker-compose.yml
web:
build: web
links:
- "db:db.local"
entrypoint: entrypoint.sh
db:
build: db
environment:
MYSQL_ROOT_PASSWORD: password
command: mysqld
For entrypoint.sh you put something like this:
#!/bin/sh
#this is a hack to wait until the DB image is up and the port is open
until mysqladmin -u root -ppassword -e -h db.local ping; do
echo "$(date) - waiting for mysql"
sleep 3
done
if ! mysql -u root -ppassword -e -h db -e 'use dbname'; then
mysql -u root -ppassword -e "CREATE DATABASE dbname"
fi
exec "$#"
You can copy your queries as .sql file into "/docker-entrypoint-initdb.d" container directory. mysql will execute them after starting container
COPY ./init/db.sql /docker-entrypoint-initdb.d/
read official doc https://hub.docker.com/_/mysql?tab=description&page=1
Initializing a fresh instance
Two servers, one on Windows server + WAMP, one on Linux Debian + MySQL
I would like to understand why I don't get the same result from these two following ways:
From a phpmyadmin (MySQL on linux) I import a sql file
From MySQL (on windows server) I launch this powershell
Script file content:
$mysqlpath = "C:\wamp\bin\mysql\mysql5.6.12\bin\"
$username = xxx
$password = xxx
$backuppath = "C:\"
$backupfile = $backuppath + "file.sql"
CD $mysqlpath
&cmd /c "mysql -u $username -p$password -h 0.0.0.0 < $backupfile"
CD C:\
NOTE: (0.0.0.0 is Linux server IP address)
Why i don t get the same result from these two ways?
How can I set up the powershell file with the right parameters to get the same results that the first solution ?
In using WinRM and ssh, can I lauch my powershell file on windows to execute via ssh the mysql command on linux server ?
I need to execute a mysql command on a remote server but seem to be hitting problem when it comes to executing the actual mysql bit
#!/usr/bin/expect -f
spawn /usr/bin/ssh -t root#10.0.0.2
expect "password: "
sleep 1
send "password\r"
sleep 2
/usr/bin/mysql databasename -e "update device_log set status = 'Y' where device_id in ('1','2');"
basically I want to change the flag to Y on device id's 1&2
but the script outputs
invalid command name "/usr/bin/mysql"
Just append the mysql command to the ssh command to run it in one go, like this:
#!/usr/bin/expect -f
spawn /usr/bin/ssh -t root#10.0.0.2 /usr/bin/mysql databasename -e "the query"
expect "password: "
sleep 1
send "password\r"
I'm not very much into expect, but I'm expecting that your attempt in the mysql line isn't actually valid syntax for expect to run a command.
Additionally:
You should use SSH keys for passwordless login instead of having a root password hardcoded in a script.
Consider running MySQL remotely e.g. mysql -h 10.0.0.2 -e "the query", or
Use port forwarding in SSH to connect to MySQL securely, e.g. run ssh -L 3307:localhost:3306 root#10.0.0.2 in the background and then connect to TCP port 3307 on localhost mysql -h 127.0.0.1 -P 3307.
It sounds like /usr/bin/mysql is not the the path to the mysql binary on that remote server. You could use just mysql instead, assuming that the binary is somewhere in that remote server's PATH. Otherwise you will have to go and find out where the binary is actually located and alter the absolute path accordingly.