Correctly using mysql commands in docker-compose? - mysql

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.

Related

docker compose documentation tutorial, Unknown database 'todos'

i followed docker compose tutorial
https://docs.docker.com/get-started/08_using_compose/
to create composer file for todo app with mysql service
i did the same docker compose as the tutorial
but it gave me this error
app-mysql-1 | 2022-05-28T21:55:26.950553Z 3 [Note] Unknown database 'todos'
app-app-1 | Error: ER_BAD_DB_ERROR: Unknown database 'todos'
i executed mysql container services to see if todos database created or not
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
docker compose
version: "3.7"
services:
app:
image: node:12-alpine
command: sh -c "yarn install && yarn run dev"
ports:
- 3000:3000
working_dir: /app
volumes:
- ./:/app
environment:
MYSQL_HOST: mysql
MYSQL_USER: root
MYSQL_PASSWORD: secret
MYSQL_DB: todos
mysql:
image: mysql:5.7
volumes:
- todo-mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: todos
volumes:
todo-mysql-data:
Running just the mysql portion of your docker-compose file seems to have worked for me.
version: "3.7"
services:
mysql:
image: mysql:5.7
volumes:
- todo-mysql-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: todos
volumes:
todo-mysql-data
You might want to try deleting the volume and recreating your containers.
docker volume rm todo-mysql-data
docker-compose up --force-recreate
Thanks Hambrook i found that there was a volume with the same name i tried to create
docker volume ls
DRIVER VOLUME NAME
local app_todo-mysql-data
local todo-mysql-data
so i removed it and write command again
docker compose up
and it's work

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

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.

Connecting local MySQL database inside a Docker container?

Here is how I am trying to connect. But it shows:
Host 'host.docker.internal' is not allowed to connect
YAML:
environment:
- MYSQL_DSN=mysql:host=192.168.0.1;port=3307;dbname=dbname
Your MySQL user does not allow external connections.
Update your user to allow external connections using "GRANT" or create a new user using "CREATE USER": https://linuxize.com/post/how-to-create-mysql-user-accounts-and-grant-privileges/
To accept all connections, use % wildcard as a host part.
create mysql data as a container: named: mysql-data
create docker container for mysql server to use mysql DB created in step 1. this is your inside docker conatiner
mysql client should be there on the host machine
for above 2 steps use yaml file as:
services:
mysql-data:
image: image_path/mysql-data:latest
container_name: mysql-data
my-container-name:
image: mysql/mysql-server:5.6
container_name: my-container-name
env_file:
- MYSQL_ROOT_HOST=%
- MYSQL_ROOT_PASSWORD=rootpassword
- MYSQL_USER=username
- MYSQL_PASSWORD=password
ports:
- 9999:3306
volumes_from:
- mysql-data
login to mysql DB using command:
mysql -h <your-host-ip> -P9999 -uusername -ppassword

Can't create db in docker-entrypoint-initdb.d with mysql docker container

I created a docker-compose.yml.
version: "3.7"
services:
db:
container_name: mysql
image: mysql
ports:
- 3306:3306
volumes:
- ./sql:/docker-entrypoint-initdb.d
- ./mysql:/var/lib/mysql
environment:
MYSQL_ROOT_USER : root
MYSQL_ROOT_PASSWORD: root_pass
MYSQL_DATABASE: wp1
MYSQL_USER: wp
MYSQL_PASSWORD: pass
restart: always
And in sql directory, I put a init.sql.
CREATE DATABASE IF NOT EXISTS wp2;
Then run docker-compose up.
In the log there is a entry for the init script.
mysql | 2020-04-08 02:17:32+00:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
But the database wp2 is not created.
$ docker exec -it mysql bash
# mysql -u wp -ppass
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| wp1 |
+--------------------+
How can I create a database with mysql container init script?
Your wp user doesn't have access rights on wp2.
Append to your sql file:
GRANT ALL on wp2.* to wp#'%';

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!