Error start mysql docker - mysql

I am creating an image in the docker to the mysql install mode, but it is giving error to start mysql.
My Dockerfile
The error occurs when processing and line 25 of Dockerfile:
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)
I found that the error occurs because mysql is not running. The docker print below to rotate the line 22:
bin boot dev etc home lib lib64 media mnt opt proc root run sbin scripts srv sys tmp usr var MySQL is stopped.
Stack trace complete
Any suggestion?

The docker daemon will execute RUN command one by one and commit the result, and seems your mysql service status is not committed to the image. To solve this problem, you may try these ways
Put all commands into one RUN command
RUN echo $(service mysql restart) && echo $(service mysql status) && sudo mysql -uroot -pmysql_pass -e "CREATE DATABASE wordpress;" && sudo mysql -uroot -pmysql_pass -e "CREATE USER 'wordpressuser'#'%';" && sudo mysql -uroot -pmysql_pass -e "SET PASSWORD FOR 'wordpressuser'#'%'= PASSWORD('${mysql_pass}');" && sudo mysql -uroot -pmysql_pass -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'#'%'; FLUSH PRIVILEGES;"
initialize your mysql in entrypoint with a bash script.
init_mysql.sh
#!/bin/sh
sudo mysql -uroot -pmysql_pass -e "CREATE DATABASE wordpress;"
sudo mysql -uroot -pmysql_pass -e "CREATE USER 'wordpressuser'#'%';"
sudo mysql -uroot -pmysql_pass -e "SET PASSWORD FOR 'wordpressuser'#'%'= PASSWORD('${mysql_pass}');"
sudo mysql -uroot -pmysql_pass -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'#'%'; FLUSH PRIVILEGES;"
and add following lines to dockerfile
ADD init_mysql.sh /
RUN \
cd / \
chmod 777 init_mysql.sh
CMD ./init_mysql.sh

You need to figure out why MySQL isn't starting. Log into the environment and manually try to start MySQL and see what the error is.
Have you considered basing your image on one of the official MySQL Docker images?
https://registry.hub.docker.com/_/mysql/

Related

mysql command inside mysql container hung forever

On my centos host, running mysql command through docker exec is hung forever:
$ docker exec mysql /usr/bin/mysql -u root --password=passwd123 < mysql.backup.sql
The mysql DB is accessible using mysqlworkbench just fine.
Thanks for your help.
You can try following for backup
docker exec mysql /usr/bin/mysqldump -u root --password=passwd123 DATABASE > backup.sql
Instead of mysql you should use mysqldump ( Mysqldump is a command-line utility that is used to generate the logical backup of the MySQL database. )
If your purpose is restore, then
cat backup.sql | docker exec -i mysql /usr/bin/mysql -u root --password=passwd123 DATABASE
I was missing -i parameter in my original command.
This command works perfectly:
$ $ docker exec -i mysql /usr/bin/mysql -u root --password=passwd123 < mysql.backup.sql

How to execute mysqldump command from the host machine to a mysql docker container

I want to create mysql dumps for a database which is running in docker container. However I do not want to get into the container and execute the command but do it from the host machine. Is there a way to do it. I tried few things but probably I am wrong with the commands.
docker exec -d mysql sh mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql
docker exec -d mysql sh $(mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql)
docker exec -d mysql mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql
the dumps directory is already bind to the host machine.
These commands are seems not the right way to do it or probably not the right way to do it at all. These always ends up with an error:
bash: /dumps/MyNewDump.sql: No such file or directory
But if I just run mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql inside the container it works fine.
This worked on my end(just replace the container ID):
docker exec 1d3595c0ce87 sh -c 'mysqldump -uroot -pSomePassword DBName > /dumps/MyNewDump.sql'
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump takes the usual MySQL CLI options to connect to a server running somewhere else. That means you can run it directly from the host, without needing docker exec (administrator) permissions.
mysqldump -h127.0.0.1 -uroot -pSomePassword DBName > MyNewDump.sql
In contrast to the docker exec forms, this creates the dump file on the host, but that's probably what you want.

Issues escaping double quotes while connecting to windows server

I have a scenario. I need to connect to a windows server from a unix machine using sshpass and execute a MySQL query remotely. Below is the code I want to run:
sshpass -p 'passwd' ssh -q -o StrictHostKeyChecking=no Administrator#IP mysql -uroot db_name -P 3306 -e "select date(lastupdate),circlecode,count(*) from some_table where date(lastupdate)='2017-05-17' group by 1;"
If I separate the two commands i.e. login and MySQL query it runs fine
sshpass -p 'passwd' ssh -q -o StrictHostKeyChecking=no Administrator#IP
mysql -uroot db_name -P 3306 -e "select date(lastupdate),circlecode,count(*) from some_table where date(lastupdate)='2017-05-17' group by 1;"
I have tried multiple solutions for similar cases I found on net but none works.
Please help. Thanks in advance.

Docker - MySQL commands within Dockerfile using RUN (ERROR 2002)

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

Run MySQL query on remote machine through ssh in command line

I am trying to run MySQL query on remote machine with this command:
ssh user#192.168.2.26 "mysql -uroot -proot -e \"use test";""
I am not able to use that database.
Please suggest a working command.
Try this:
mysql -h host -u root -proot -e "show databases;";
Try this:
ssh root#host "mysql database -e 'query to run on table_name; more queries to run;'"
Same can be done with user#host if that user has permission to execute SQL queries let alone launch mysql in general. Using -e is the same as --execute, which will run whatever you put within the trailing quotes (single or double) and quit. The standard output format would be the same as you would see using --batch.
MySql seems to have a special command line syntax which includes the database.
mysql -u user -p -e 'SQL Query' database
This documentation is rather old but I got it to work
http://www.cyberciti.biz/faq/run-sql-query-directly-on-the-command-line/
Final working command with ssh:
ssh user#host "mysql -u user -e 'show tables;' databasename"
This ended up working for me in a bash script:
query='USE [database]; SELECT ...'
mysql='mysql -u [username] -p[password] -e '"'""$query""'"
ssh [username]#[server] -t "$mysql"
If you want to make it more safe then add a prompt for the password instead of storing it somewhere potentially unsafe.
This worked for me after a few tests (basically same answer as #King-Wzrd):
ssh -t kom "mysql -uroot -p -e 'show databases;'"
ssh -t kom "mysql -uroot -p < /home/ling/websites/jin_test/.deploy/tmp.sql"
The "trick" was the quotes around the command.
The -t option allows for prompting password interactively via the remote shell.
The kom here is just a ssh config identifier defined in my ~/.ssh/config file (see more here: https://www.cyberciti.biz/faq/create-ssh-config-file-on-linux-unix/).
Running this from my Host environment against MySQL within my Homestead VM produced a nice result... although I did have to set the root password from within the VM first in order for it to work.
ssh vagrant#192.168.10.10 mysql -h localhost -u root -p -e "'SELECT * FROM user;' mysql";