Error when creating a MySQL database in a Dockerfile - mysql

I want to create a simple container that contains a MySQL server with an initialized database. My Dockerfile currently looks like this:
FROM ubuntu:16.10
RUN apt-get update
# install MySQL; root user with no password
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get install -y mysql-server mysql-client
RUN service mysql start
RUN mysqladmin -u root create mydb
CMD [ "bash" ]
However, when I build the image via docker build -t mysql-test . I get the following error:
Step 7 : RUN mysqladmin -u root create mydb
---> Running in a35c3d176d4f
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!
The command '/bin/sh -c mysqladmin -u root create mydb' returned a non-zero code: 1
When I comment the line where I want to create the database (RUN mysqladmin -u root create mydb) I can build the image, start it, and execute the mysqladmin command from the command line:
# this works
> docker run -it mysql-test
root#...> service mysql start
root#...> mysqladmin -u root create mydb
Why do I get the error when creating the database in the Dockerfile? Thank you!

Reading the documentation of the RUN instruction makes it clear:
The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.
So each run instruction creates a new image and starting the MySQL server and creating the database should be combined in the same RUN instruction:
RUN service mysql start && mysqladmin -u root create mydb
This solves it.

Related

How to create testing database under docker?

In Kubuntu 18 I create docker for laravel 6 app with mysql defined :
mysql:
container_name: "vanilla-crm-db"
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: "MYSQL_ROOT_PASSWORD"
MYSQL_DATABASE: "vanilla-crm-dev"
MYSQL_USER: "MYSQL_USER"
MYSQL_PASSWORD: "MYSQL_PASSWORD"
ports:
- "3330:3306"
volumes:
- "./docker/mysql/data:/var/lib/mysql"
and it works for me.
I use MySql Workbench 6.3 for db access.
Text I make http tests and I need to create new database for this and load dump of my database in it.
Name of this database is wriiten in
config/database.php under 'mysql_testing' block
I open Workbench and try to create new database : https://prnt.sc/unjie8
But I do not find “Create database” option, But I see “Create new schema” option
and in sql-statement preview I see command
CREATE SCHEMA `vanilla-crm-testing` ;
I expected
Create database ...
command.
Is it the same?
and error next:
Operation failed: There was an error while applying the SQL script to the database.
Executing:
CREATE SCHEMA `vanilla-crm-testing` ;
ERROR 1044: Access denied for user 'vanilla-crm-usr'#'%' to database 'vanilla-crm-testing'
SQL Statement:
CREATE SCHEMA `vanilla-crm-testing`
Which is valid way to create testing database?
UPDATED :
I tried to create new database in mysql console, like:
mysql
CREATE DATABASE vanilla-crm-testing;
but I got error in docker command line:
$ docker-compose exec app bash
root#09649d3a2b81:/app# mysql
bash: mysql: command not found
My docker app has in Dockerfile :
FROM php:7.3-apache
...
RUN apt-get update && apt-get install --no-install-recommends -y \
apt-utils ghostscript jq libicu-dev libmagick++-dev libpq-dev libfreetype6-dev libjpeg62-turbo-dev zlib1g-dev libzip-dev git zip && \
docker-php-ext-install intl && \
docker-php-ext-install opcache && \
docker-php-ext-install pdo_mysql && \
and no more mysql commands. Are there some more packages I need to install in Dockerfile to have mysql console under docker?
UPDATED # 2:
I enter the bash with command :
docker-compose exec mysql bash
root#f216ef80c104:/# uname -a
Linux f216ef80c104 4.15.0-118-generic #119-Ubuntu SMP Tue Sep 8 12:30:01 UTC 2020 x86_64 GNU/Linux
Where mysql is container_name in docker-compose.yml
Usually I enter mysql console with command:
mysql -u root -h localhost -p
But which must be format of this command in the docker console?
I tried several ways and failed...
UPDATED # 3:
I installed DBeaver Version 7.2.1.202009201907 and logged into
my database and tried to create new database for testing. I got error:
https://prnt.sc/uol0z7
How to fix it ?
Have I to add some more right my mysql container definitions?
Thanks!
To answer your question seems your main problem is you are trying to create a new test database but you are login with a non-root user. Non-root user has very little permission to do some operations. That's why you got that error message and can't create a new database. To solve this, try to log-in with the root user and make sure your Dbeaver config is correct:
host: 127.0.0.1
port: <Exposed MySQL port based on your docker-compose, e.g: 3330>
username: root
password: <MYSQL_ROOT_PASSWORD from your docker-compose>
With the root user, you should be able to create any new database. Another thing, if you wanted to connect via mysql cli command, make sure you also provide the correct port to the docker container. The command should be like this:
mysql -u root -h 127.0.0.1 -P <Exposed MySQL port based on your docker-compose, e.g: 3330> -p
Hope it helps and solved your problem. :)

dockerfile inheritance and RUN commands

I'm trying to create a dockerfile to build a LAMP server for development using the alpine-lamp base.
FROM glats/alpine-lamp
ENV MYSQL_ROOT_PASSWORD=password
RUN apk add wget && \
apk add php7-simplexml && \
mysql -u root -ppassword -e "CREATE DATABASE IF NOT EXISTS mydb"
When I run the docker image build -t testing . command to build my image I get an error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)
Is my mysql command being run before the "parent" dockerfile has completed running? I've been able to remove these lines and start the container, and then run the identical mysql command from the cli, so I'm thinking the mysql server part of the parent lamp stack just didn't get completely run. Is there a way to ensure my commands run after the parent?
At image build time, mysql will not be running. That is why you see the error message about not being able to connect.
This type of thing isn't something that should be run at build-time. Put the database initialization into an entrypoint script instead.

mySQL Docker "ERROR 2002 (HY000): Can't connect to local MySQL server through socket"

got a little script to pull up some mySQL server variants and test my scripts on different versions. My shellscript is:
docker pull mysql:$version
docker run -d -v $(pwd)/mysql.sh:/mysql.sh --name=mysql."$version" -e MYSQL_ROOT_PASSWORD='root' mysql:"$version"
docker exec -it mysql."$version" bash -c 'bash ./mysql.sh'
The mysql.sh file is:
/etc/init.d/mysql restart || service mysql restart || service mysqld restart
mysql -proot -e "SELECT ##version"
I get the following error:
./mysql.sh: line 1: /etc/init.d/mysql: No such file or directory
mysql: unrecognized service
mysqld: unrecognized service
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Anyone knows what's wrong?
docker does not use init.d structure for the services since it has no services. If you check inside the Dockerfile, you'll see that the mysql is started in foreground.
In your first script, the command docker exec -it mysql."$version" bash -c 'bash ./mysql.sh' is not needed. You should replace it with:
docker exec -it mysql."$version" mysql -proot -e "SELECT ##version"
The error indicates that /etc/init.d/mysql is not found. You need to change that to /usr/bin/mysql and it should work.

Connect to mysql in a docker container from the host

In a nutshell
I want to run mysql in a docker container and connect to it from my host. So far, the best I have achieved is:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
More details
I'm using the following Dockerfile:
FROM ubuntu:14.04.3
RUN apt-get update && apt-get install -y mysql-server
# Ensure we won't bind to localhost only
RUN grep -v bind-address /etc/mysql/my.cnf > temp.txt \
&& mv temp.txt /etc/mysql/my.cnf
# It doesn't seem needed since I'll use -p, but it can't hurt
EXPOSE 3306
CMD /etc/init.d/mysql start && tail -F /var/log/mysql.log
In the directory where there is this file, I can succesfully build the image and run it with:
> docker build -t my-image .
> docker run -d -p 12345:3306 my-image
When I attach to the image, it seems to work just fine:
# from the host
> docker exec -it <my_image_name> bash
#inside of the container now
$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
[...]
However I don't have that much success from the host:
> mysql -P 12345 -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Even more details
I've seen that there's a question which looks like mine. However, it isn't the same (and it doesn't have any answers anyway)
I've seen that there are images dedicated to mysql, but I didn't have more success with them
My grep -v may feel weird. Admittedly, there may be cleaner way to do it. But when I attach my image, I can observe it actually worked as expected (ie: removed the bind-address). And I can see in the container /var/log/mysql/error.log:
Server hostname (bind-address): '0.0.0.0'; port: 3306
- '0.0.0.0' resolves to '0.0.0.0';
Server socket created on IP: '0.0.0.0'.
If your Docker MySQL host is running correctly you can connect to it from local machine, but you should specify host, port and protocol like this:
mysql -h localhost -P 3306 --protocol=tcp -u root
Change 3306 to port number you have forwarded from Docker container (in your case it will be 12345).
Because you are running MySQL inside Docker container, socket is not available and you need to connect through TCP. Setting "--protocol" in the mysql command will change that.
If you use "127.0.0.1" instead of localhost mysql will use tcp method and you should be able to connect container with:
mysql -h 127.0.0.1 -P 3306 -u root
I recommend checking out docker-compose. Here's how that would work:
Create a file named, docker-compose.yml that looks like this:
version: '2'
services:
mysql:
image: mariadb:10.1.19
ports:
- 8083:3306
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: wp
Next, run:
$ docker-compose up
Notes:
For latest mariadb image tag see https://hub.docker.com/_/mariadb/
Now, you can access the mysql console thusly:
$ mysql -P 8083 --protocol=tcp -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.5-10.1.19-MariaDB-1~jessie mariadb.org binary distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Notes:
You can pass the -d flag to run the mysql/mariadb container in detached/background mode.
The password is "wp" which is defined in the docker-compose.yml file.
Same advice as maniekq but full example with docker-compose.
The simple method is to share the mysql unix socket to host machine. Then connect through the socket
Steps:
Create shared folder for host machine eg: mkdir /host
Run docker container with volume mount option docker run -it -v /host:/shared <mysql image>.
Then change mysql configuration file /etc/my.cnf and change socket entry in the file to socket=/shared/mysql.sock
Restart MySQL service service mysql restart in docker
Finally Connect to MySQL servver from host through the socket mysql -u root --socket=/host/mysql.sock. If password use -p option
OK. I finally solved this problem. Here follows my solution used in https://sqlflow.org/sqlflow.
The Complete Solution
To make the demo self-contained, I moved all necessary code to https://github.com/wangkuiyi/mysql-server-in-docker.
The Key to the Solution
I don't use the official image on DockerHub.com https://hub.docker.com/r/mysql/mysql-server. Instead, I made my own by installing MySQL on Ubuntu 18.04. This approach gives me the chance to start mysqld and bind it to 0.0.0.0 (all IPs).
For details, please refer to these lines in my GitHub repo.
SQLFLOW_MYSQL_HOST=${SQLFLOW_MYSQL_HOST:-0.0.0.0}
echo "Start mysqld ..."
sed -i "s/.*bind-address.*/bind-address = ${SQLFLOW_MYSQL_HOST}/" \
/etc/mysql/mysql.conf.d/mysqld.cnf
service mysql start
To Verify My Solution
Git clone the aforementioned repo.
git clone https://github.com/wangkuiyi/mysql-server-in-docker
cd mysql-server-in-docker
Build the MySQL server Docker image
docker build -t mysql:yi .
Start MySQL server in a container
docker run --rm -d -p 23306:3306 mysql:yi
Install the MySQL client on the host, if not yet. I am running Ubuntu 18.04 on the host (my workstation), so I use apt-get.
sudo apt-get install -y mysql-client
Connect from the host to the MySQL server running in the container.
mysql --host 127.0.0.1 --port 23306 --user root -proot
Connect from Another Container on the Same Host
We can run MySQL client from even another container (on the same host).
docker run --rm -it --net=host mysql/mysql-server mysql \
-h 127.0.0.1 -P 13306 -u root -proot
Connect from Another Host
On my iMac, I install the MySQL client using Homebrew.
brew install mysql-client
export PATH="/usr/local/opt/mysql-client/bin:$PATH"
Then, I can access the above Ubuntu host (192.168.1.22).
mysql -h 192.168.1.22 -P 13306 -u root -proot
Connect from a Container Running on Another Host
I can even run MySQL client in a container running on the iMac to connect to the MySQL server in a container on my Ubuntu workstation.
docker run --rm -it --net=host mysql/mysql-server mysql \
-h 192.168.1.22 -P 13306 -u root -proot
A Special Case
In the case that we run MySQL client and server in separate containers running on the same host -- this could happen when we are setting up a CI, we don't need to build our own MySQL server Docker image. Instead, we can use the --net=container:mysql_server_container_name when we run the client container.
To start the server
docker run --rm -d --name mysql mysql/mysql-server
To start the client
docker run --rm -it --net=container:mysql mysql/mysql-server mysql \
-h 127.0.0.1 -P 3306 -u root -proot
if you running docker under docker-machine?
execute to get ip:
docker-machine ip <machine>
returns the ip for the machine and try connect mysql:
mysql -h<docker-machine-ip>
In your terminal run: docker exec -it container_name /bin/bash
Then: mysql
You should inspect the IP address assigned to the running container, and ther connect to that host:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container name or id>
than you can connect with:
mysql -h <IP provided by inspect command> -P <port> -u <user> -p <db name>
https://mariadb.com/kb/en/installing-and-using-mariadb-via-docker/#connecting-to-mariadb-from-outside-the-container
I do this by running a temporary docker container against my server so I don't have to worry about what is installed on my host. First, I define what I need (which you should modify for your purposes):
export MYSQL_SERVER_CONTAINER=mysql-db
export MYSQL_ROOT_PASSWORD=pswd
export DB_DOCKER_NETWORK=db-net
export MYSQL_PORT=6604
I always create a new docker network which any other containers will need:
docker network create --driver bridge $DB_DOCKER_NETWORK
Start a mySQL database server:
docker run --detach --name=$MYSQL_SERVER_CONTAINER --net=$DB_DOCKER_NETWORK --env="MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD" -p ${MYSQL_PORT}:3306 mysql
Capture IP address of the new server container
export DBIP="$(docker inspect ${MYSQL_SERVER_CONTAINER} | grep -i 'ipaddress' | grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])')"
Open a command line interface to the server:
docker run -it -v ${HOST_DATA}:/data --net=$DB_DOCKER_NETWORK --link ${MYSQL_SERVER_CONTAINER}:mysql --rm mysql sh -c "exec mysql -h${DBIP} -uroot -p"
This last container will remove itself when you exit the mySQL interface, while the server will continue running. You can also share a volume between the server and host to make it easier to import data or scripts. Hope this helps!
mysql -u root -P 4406 -h localhost --protocol=tcp -p
Remember to change the user, port and host so that it matches your configurations. The -p flag is required if your database user is configured with a password
docker run -e MYSQL_ROOT_PASSWORD=pass --name sql-db -p 3306:3306 mysql
docker exec -it sql-db bash
mysql -u root -p
For conversion,you can create ~/.my.cnf file in host:
[Mysql]
user=root
password=yourpass
host=127.0.0.1
port=3306
Then next time just run mysql for mysql client to open connection.
run following command to run container
docker run --name db_name -e MYSQL_ROOT_PASSWORD=PASS--publish 8306:3306 db_name
run this command to get mysql db in host machine
mysql -h 127.0.0.1 -P 8306 -uroot -pPASS
in your case it is
mysql -h 127.0.0.1 -P 12345 -uroot -pPASS
First, see the logs Are there any errors or not.
docker ps -a
docker logs container_name
If there is any error, search for the solution of that particular error, if there are no errors you may proceed to the next step.
Remember after starting your container MySQL take a few minutes to start so run those commands after 3-4 minutes.
docker exec -it container_name bash
# ONCE YOU ARE INSIDE CONTAINER
mysql -u root -p
SHOW DATABASES;
exit
To connect from terminal or command prompt
mysql -h localhost -P 3306 --protocol=tcp -u root -p
I have tried the same thing, and I was facing same problem, but I have noticed one thing, whenever I was trying to run docker container with /bin/bash like as below:
docker container run -it --name test_mysql -e MYSQL_ROOT_PASSWORD=password -p 3306:3306 mysql:latest /bin/bash
After hitting the above command, my container getting start but facing the same error" ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)"
Now, I have tried instead of above command, I have used below command:
docker container run -itd --name test_mysql -e MYSQL_ROOT_PASSWORD=password -p 3306:3306 mysql:latest
docker container exec test_mysql /bin/bash
then using the command
mysql -uroot -P3306 -p
then able to connect, Hope This idea will work in your case.
Thanks
Love Arora
I was able to connect mysql server 5.7 running on my host using the below command:
mysql -h 10.10.1.7 -P 3307 --protocol=tcp -u root -p
where the ip given is my host ip and 3307 is the port forwaded in mysql docker. After entering the command type the password for mysql. That is it. Now you are connected the mysql docker container from the your host machine.
According to the photos, go to the project -> MySQL -> inspect tab in the docker desktop app and modify the information about the database .env file.
.env file example:
DB_CONNECTION=mysql
DB_HOST=0.0.0.0:3306
DB_PORT=3306
DB_DATABASE=testdb
DB_USERNAME=sail
DB_PASSWORD=password
In case this will help somebody:
My prefered solution
add ~/.my.cnf with contents
[client]
user=<yourusername>
password=typethepasswordhere
host=127.0.0.1
port=3306
then from the terminal run >$ mysql and you should get in your sql cmd.
The hardcore alternative
you can also connect like this
docker exec -it containername mysql
To get into the sql cmd
or
docker exec -it contaiinername bash (or sh)
to connect in the container and then run >$ mysql
change "localhost" to your real con ip addr
because it's to mysql_connect()

MySQL starting error on CentOS 6

I have removed the preinstalled MySQL from CentOS 6 using the command-
yum remove mysql mysql-*
Now, I want to install "MySQl-server-5.5.28-1.linux2.6.i386.rpm" in my system.
But it could not start on my system. Please tell me how to start the MySQL with user account as well as in root also.
I have done this:-
[rex#dhcppc0 MySQL]$
[rex#dhcppc0 MySQL]$ su -
Password:
[root#dhcppc0 ~]# cd /home/rex/Documents/Software/MySQL
[root#dhcppc0 MySQL]# rpm -i MySQL-server-5.5.28-1.linux2.6.i386.rpm
package MySQL-server-5.5.28-1.linux2.6.i386 is already installed
[root#dhcppc0 MySQL]# mysql -u
-bash: mysql: command not found
[root#dhcppc0 MySQL]# mysql -u mysql
-bash: mysql: command not found
[root#dhcppc0 MySQL]# mysql.server start
-bash: mysql.server: command not found
MySQL-server-xxxx is package that contains only MySQL Server. Executable file for server is "mysqld" - "d" letter is for a daemon).
File that you trying to execute (mysql) is MySQL client. There is no client application in server package.
Please install MySQL-client-xxxx package, where xxxx is a version.
Execute the initscript below as a root user:
# /etc/init.d/mysql start
If you try to access the database from local client, you need to install MySQL-client-5.5.28-1.linux2.6.i686.rpm package.
Then you can control the MySQL database.
$ /usr/bin/mysql -u mysql