mysql:5.7 docker allow access from all hosts and create DB - mysql

I got a problem with mysql:5.7 with Docker. I know there are many questions about this but I just can't get this to work. I simply want to create a mysql:5.7 container with docker-compose with the following settings on STARTUP!!:
Create user "root" with password "mypw"
Allow access with root user from ALL hosts and containers
Create a table named "mytable"
My yml:
db:
build: "."
command:
- "--default-authentication-plugin=mysql_native_password"
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD="mypw"
volumes:
- "./mysql/database:/var/lib/mysql"
What i tried:
Dockerfile: COPY ./mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf
mysqld.cnf contains the line "bind-address = 0.0.0.0" at the end
Add script to /docker-entrypoint-initdb.d/ with content "CREATE DATABASE mydb;"
MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE variables (can't connect either)
use skip-grant-tables in mysqld.cnf (errors)
MYSQL_ROOT_HOST="%"
I just don't get why especially allow access from everywhere is so hard to do.
Can anyone please help with where to put what?Thanks

There is one more variable called MYSQL_ROOT_HOST. You need to set this variable to %.
So your docker-compose.yaml file will be:
$ cat docker-compose.yaml
version: "3"
networks:
net: {}
services:
db:
# build: "."
image: mysql:5.7
command:
- "--default-authentication-plugin=mysql_native_password"
ports:
- "3309:3306"
environment:
MYSQL_ROOT_PASSWORD: mypw
MYSQL_ROOT_HOST: "%"
# volumes:
# - "./mysql/database:/var/lib/mysql"
Then I am able to connect with command:
$ mysql -uroot -h127.0.0.1 -pmypw -P3309
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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>
Your issue
Your issue may be related to volume. Once your database is initialized with some credentials you have to stop container, remove data, then modify docker-compose.yaml and start your services again.

Related

Why can't I run the mysql command in docker compose?

I have a project with a mysql database in a container. I use docker-compose to set my project up. And I want to run the mysql command to inspect te database.
So I did, and get:
docker-compose run --rm database mysql
Creating myproject_database_run ... done
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
However when I tried this it works:
docker exec -it myproject_database_1 mysql
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO)
Can anybody explain me this?
My docker-compose file:
version: "3.7"
services:
database:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
ports:
- "127.0.0.1:3306:3306"
env_file: .env
volumes:
- type: volume
source: db_data
target: /var/lib/mysql
- type: bind
source: ./my.cnf
target: /etc/my.cnf
read_only: true
volumes:
db_data:
testing_images:
docker-compose run creates a new container. That's perfectly normal, but if your mysql client is configured to connect via a Unix socket, the new container will have a new filesystem and won't be able to see the main database container's /var/run directory.
When you use docker-compose run, you need to specify a TCP connection, using the setup described in Networking in Compose in the Docker documentation. For example,
docker-compose run --rm database \
mysql -h database
Since you publish ports: out of the container, you should be able to reach it from the host, without Docker. The trick here is that the mysql command-line client interprets localhost as a magic term to use a Unix socket and not a normal host name, so you need to specifically use the IP address 127.0.0.1 instead.
# From the same host, without anything Docker-specific
mysql -h 127.0.0.1
Try adding MYSQL_ROOT_PASSWORD in the environment.
environment:
MYSQL_ROOT_PASSWORD: root
This is from one of my working compose file
services:
## -----------------------------------------------
## MySql database
## -----------------------------------------------
db_mysql:
image: mysql:8.0
restart: always
volumes:
- db_mysql:/var/lib/mysql
- ./mysql:/docker-entrypoint-initdb.d
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- app-network
deploy:
mode: global
ports:
- "3306:3306"
## map volume
volumes:
db_mysql:
## in network, we can define any name under networks:
networks:
app-network:
driver: bridge
FYI: Official MySQL docker image - Docker Hub

Correctly using mysql commands in docker-compose?

I am trying to learn docker-compose. I am having trouble running mysql commands through docker compose.
This is my docker-compose file:
version: '3'
services:
mysql:
image: mysql
container_name: mysql
restart: always
env_file: .env
environment:
- MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASS
- MYSQL_USER=$MYSQL_USER
- MYSQL_PASSWORD=$MYSQL_PASS
- MYSQL_DB=$MYSQL_DB
volumes:
- db-data:/var/lib/mysql
command: >
mysql -e "CREATE DATABASE \${MYSQL_DB}"
&& mysql -e "GRANT ALL PRIVILEGES ON \${MYSQL_DB}.* TO '\${MYSQL_USER}'#'%'"
&& mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
ports:
- $MYSQL_PORT:3306
volumes:
db-data:
But when I run the docker-compose up --force-recreate --build, I get the mysql usage guide output:
Attaching to mysql
mysql | mysql Ver 8.0.25 for Linux on x86_64 (MySQL Community Server - GPL)
mysql | Copyright (c) 2000, 2021, Oracle and/or its affiliates.
mysql |
mysql | Oracle is a registered trademark of Oracle Corporation and/or its
mysql | affiliates. Other names may be trademarks of their respective
mysql | owners.
mysql |
mysql | Usage: mysql [OPTIONS] [database]
mysql | -?, --help Display this help and exit.
mysql | -I, --help Synonym for -?
mysql | --auto-rehash Enable automatic rehashing. One doesn't need to use
mysql | 'rehash' to get table and field completion, but startup
...etc...
Am I using the command: properly to send mysql commands?
I want to suggest a different approach with MySQL:
You can mount another SQL script file that will run at the beginning:
version: '3'
services:
mysql:
image: mysql
container_name: mysql
restart: always
env_file: .env
environment:
- MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASS
- MYSQL_USER=$MYSQL_USER
- MYSQL_PASSWORD=$MYSQL_PASS
- MYSQL_DB=$MYSQL_DB
volumes:
- db-data:/var/lib/mysql
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- $MYSQL_PORT:3306
volumes:
db-data:
now create init.sql in your docker-compose.yml directory:
CREATE DATABASE ${MYSQL_DB}
GRANT ALL PRIVILEGES ON ...
You can see it in Initializing a fresh instance.

MySQL commands inside Docker container

I have the following docker compose:
version: '3.1'
services:
db:
image: mysql:5.5
container_name: mysql
environment:
- MYSQL_ROOT_PASSWORD=some_root_pw
- MYSQL_DATABASE=some_db
- MYSQL_USER=db_user
- MYSQL_PASSWORD=user_pw
ports:
- 8306:3306
volumes:
- ./db:/var/lib/mysql/some_db
I am accessing the container with the command:
docker-compose exec db /bin/sh
Which drops me at the shell as expected. However, I am unable to access mysql with the information from the docker-compose file.
mysql -u root -p
Prompts for the password, then rejects for bad username or password. I have tried with both root and user. Is there a way I can troubleshoot what is happening with user creation?
NOTE: intentionally older version of MySQL.

nextcloud and mariadb (both) on docker: SQLSTATE[HY000] [2002] No such file or directory

I've been trying to set up nextlcoud and mariadb with the linuxserver images and hit my road block when I want to get through the first run wizard of nextcloud:
Error message incl. all settings of first run wizard
Problem
I.e. the first time wizard gives me Error while trying to create admin user: Failed to connect to the database: An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory.
Question:
Where is that coming from and how to solve the problem?
System
I'm using Amahi 11 and have installed docker from the repositories. Docker verision:
Client:
Version: 18.09.0
API version: 1.39
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:48:52 2018
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.0
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:19:08 2018
OS/Arch: linux/amd64
Experimental: false
I am starting nextcloud and mariadb with docker-compose. Following the content for mariadb:
version: '2'
services:
mariadb:
image: linuxserver/mariadb
container_name: mariadb
environment:
- PUID=XX
- PGID=YYY
- MYSQL_ROOT_USER=root
- MYSQL_ROOT_PASSWORD=secret
- MYSQL_DATABASE=nextcloud
volumes:
- <path/to/my/folder>:/config
ports:
- 3307:3306
restart: unless-stopped
Already tried:
A lot of research, which came up empty or lead me to do the next point:
So from the error info I started checking if the database actually exists:sudo docker exec -it mariadb bash. There I figured, that access to command-line with 'mysql' for root was denied because the password was not set. (mmmh... is ther something wrong with my docker-compose-file?)
Anyway I corrected that one with mysql -u root -pSECRETand mysql -u root --password=SECRET. With show databases; I found no nextcloud database. (There MUST be something wrong with my docker-compose-file.) So I created it as well (create database nextcloud;). Database is now shown properly and I found it in <path/to/my/folder>. Result: No change, problem still there.
I did some more editing with on my docker-compose-file:
version: '2'
services:
mariadb:
image: linuxserver/mariadb
container_name: mariadb
environment:
- MYSQL_ROOT_PASSWORD=secret
- MYSQL_DATABASE=nextcloud
- PUID=XX
- PGID=YYY
volumes:
- <path/to/my/folder>:/config
ports:
- 3307:3306
restart: unless-stopped
So I changed the hierarchy and eliminated the MYSQL_ROOT_USER=rootline. When restarting I can mysql -u root --password=SECRET and show the nextcloud database. YET, I am not sure, if these changes remained in the volume from my last (manual) changes. Result: Problem still there.
Just for curiousity I started playing around with the localhost-port. I chose 3307 because my host-system has a mariadb running on 3306, which I do not want to use. So altering port localhost:3307 to localhost:WXYZ - you name it gives the same error... mmmh - changeing localhost to <your host-IP>!!!
SUCCESS
I changed localhost -> mariadb and it worked!
I had to use nextcloud-mariadb:3306 as the connection string. I figured it out by running $ docker ps -a which lists the name and the port.
Solution:
As other answers have mentioned, the solution is don't use localhost.
Even changing to 127.0.0.1 appears to be sufficient *(see note below)
Explanation:
No such file or directory is the result of mysql attempting to connect over a local socket. This happens when either of these settings is set to localhost:
Database host field of the WebUI
environment variable MYSQL_HOST
*Note: in the case of #2, it is not sufficient to "fix" the Database host field in the WebUI, the environment variable MYSQL_HOST always takes precedence.
(This is true as of NextCloud version 25.0.0.18)
When you run Nextcloud in docker, add --link mariadb:mariadb. You can then use mariadb to replace localhost
I had to use my custom server hostname, instead of localhost. On Linux you can get it by executing the command hostname.
I solved the same problem when I changed the Nexcloud MYSQL_HOST environment parameter from localhost to database service/image name (in my case MYSQL_HOST: mysql ) in the docker-compose.yml
version: "3.7"
services:
mysql:
image: mysql
container_name: mysql-nextcloud
restart: unless-stopped
ports:
- 3306:3306
environment:
...
volumes:
...
app:
image: nextcloud
container_name: nextcloud
restart: unless-stopped
ports:
- 80:80
links:
- mysql
volumes:
...
environment:
MYSQL_PASSWORD: ...
MYSQL_DATABASE: ...
MYSQL_USER: ...
MYSQL_HOST: mysql
I changed the database name(localhost:PORT -> container_name), and it worked!
By the way, the [localhost:PORT] wroked well before!

How to create database in database docker container?

I'm new in docker, so cant understand - if I want to build container of mysql/postgresql/clickhouse etc - how to create database and schema of database/table? Maybe in Dockerfile or i can do it from docker-compose.yml?
I mean, that I dont know when and where to use CREATE DATABASE; CREATE TABLE ...; queries if I use docker containers of popular databases
You can use both docker and docker-compose. For example with docker compose.
Create a file called docker-compose.yml like:
version: '3'
services:
db:
image: percona:5.7
container_name: whatever_you_want
environment:
- MYSQL_DATABASE=${DATABASE}
- MYSQL_ROOT_PASSWORD=${ROOT_PASSWORD}
- MYSQL_USER=${USER}
- MYSQL_PASSWORD=${PASSWORD}
volumes:
- ./data:/docker-entrypoint-initdb.d
ports:
- "3306:3306"
Additionally you need a file under ./data with whatever SQL commands you want to run and and .env file where you define the environmental variables I used in the docker-compose.yml file above like: ${DATABASE}
Your .env file:
# MySQL
DATABASE=db_name_here
ROOT_USER=root
ROOT_PASSWORD=root
USER=dev
PASSWORD=dev
Your file with SQL commands to execute ./data/init.sql (you can name the file whatever you want)
CREATE DATABASE 'whatever';
DROP DATABASE 'whatever';
-- you can do whatever you want here
This file will be executed each time you do:
docker-compose up -d db
At first you need to create docker a image for your db server, or use an already existing image.
Bellow is an example of mysql docker image.
version: "3"
services:
****************
mysql:
container_name: mysql
image: mysql:5.7
restart: on-failure
environment:
- MYSQL_DATABASE=YOUR_DB_NAME
- MYSQL_ROOT_PASSWORD=YOUR_ROOT_USER_PASSWORD
- MYSQL_USER=YOUR_USER
- MYSQL_PASSWORD=YOUR_USER_PASSWORD
ports:
- "33060:3306"
volumes:
- "./data/db/mysql:/var/lib/mysql"
Let's describe some sections:
volumes:
- "./data/db/mysql:/var/lib/mysql"
This is like "mounting" container's /var/lib/mysql to system's ./data/db/mysql. So your data will be on your system drive, because in debian the default path to MySQL data is /var/lib/mysql.
ports:
- "33060:3306"
This will map port 3306 from container to system's 33060 port, to avoid conflicts if you have installed MySQL server on system as well.
environment:
- MYSQL_DATABASE=YOUR_DB_NAME
- MYSQL_ROOT_PASSWORD=YOUR_ROOT_USER_PASSWORD
- MYSQL_USER=YOUR_USER
- MYSQL_PASSWORD=YOUR_USER_PASSWORD
This will create a database with the defined parameters: name, root password, ..., or if a database already exists it will try to access with the defined credentials. Functionality to check/create database is already defined in the image.
If you want to define your own functionality you can define your image (e.g. dockerfile: ./Dockerfile instead of image: mysql:5.7). Dockerfile can be something like this:
FROM mysql:5.7
ARG MYSQL_DATABASE
ARG MYSQL_USER
ARG MYSQL_PASSWORD
ARG MYSQL_ROOT_PASSWORD
ENV MYSQL_DATABASE=${MYSQL_DATABASE}
ENV MYSQL_USER=${MYSQL_USER}
ENV MYSQL_PASSWORD=${MYSQL_PASSWORD}
ENV MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
# copy predefined config file
COPY configs/default.cnf /etc/mysql/conf.d/
# To be sure that MySQL will not ignore configs
RUN chmod og-w /etc/mysql/conf.d/default.cnf
# DO SOMETHING ELSE YOU WANT
EXPOSE 3306
CMD ["mysqld"]
So you can build and up your container with command docker-compose up -d --build
Here is an example I used to initialise SQL Server 2017 database using container.
https://www.handsonarchitect.com/2018/01/build-custom-sql-server-2017-linux.html
The trick is to use a shell script to run which will invoke the database initialisation script. You might have to wait for few seconds for the database engine service to start before executing the initialisation script.