Unable to create a prepared MySql DB using docker-compose - mysql

I am trying to create a prepared Mysql DB using Docker compose, the outcome is to create the "INTERVIEW" Database and give the username "ADMIN" the access to it.
For this purpose, the folder setup_scripts contains the sql script(create_user.sql) as below to grant access:
grant all privileges on INTERVIEW.* to 'ADMIN'#'%' with grant option;
FLUSH PRIVILEGES;
The docker compose file:
services:
db:
image: mysql:8.0.17
restart: always
env_file:
- .env
ports:
- '32768:3306'
volumes:
- "./my-db-data:/var/lib/mysql"
- "./setup_scripts:/docker-entrypoint-initdb.d"
volumes:
my-db:
And the .env file:
MYSQL_DATABASE='INTERVIEW'
MYSQL_USER='ADMIN'
MYSQL_PASSWORD='password'
MYSQL_ROOT_PASSWORD='password'
Upon docker-compose up, the below error is seen, but i am unable to debug the source of the issue:
ERROR 1064 (42000) at line 5: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '''' at line 1
Is there a way to see verbose logs for the db startup to understand the file where the error originates?.

The issue was with the env-file declaration:
it should be as below:
env_file:
- ./.env

Related

Access denied when running migration on Prisma

I am learning Prisma and I can't do migration in my localhost.
I am using docker-compose to create an image of mysql and I have successfully connected to the DB, please see my docker-compose.yml and schema.prisma below:
Prisma's version
"prisma": "^4.6.1"
docker-compose.yml
services:
db:
image: mysql:8
volumes:
- db-data:/var/lib/mysql
ports:
- 3306:3306
networks:
- dev
environment:
MYSQL_ROOT_PASSWORD: prismatutorial
MYSQL_USER: prismatutorial
MYSQL_PASSWORD: prismatutorial
MYSQL_DATABASE: prisma_tutorial
command: mysqld --default-authentication-plugin=mysql_native_password
cap_add:
- ALL
networks:
dev:
volumes:
db-data:
driver: local
schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model User {
id Int #id #default(autoincrement())
name String
}
.env
DATABASE_URL="mysql://prismatutorial:prismatutorial#localhost:3306/prisma_tutorial"
Every time I run the command of npx prisma migrate dev --name firstMigration and I have the error as shown in the below message:
Error: P3014
Prisma Migrate could not create the shadow database. Please make sure the database user has permission to create databases. Read more about the shadow database (and workarounds) at https://pris.ly/d/migrate-shadow
Original error: Error code: P1010
User prismatutorial was denied access on the database prisma_tutorial
However, when I try to run npx prisma db push , I can see the table is successfully created in my localhost's DB and it doesn't have permission error.
I don't think I have to create a shadow database at this point.
Am I missing out something?
Or, the docker-compose.yml I have written is wrong?
Your help is very appreciated!
In this case, npx prisma db push is successfully creating the tables because it does not require a shadow database. Please note that you should use db push command for quick prototyping.
As you are using MySQL Database, the database user prismatutorial should have CREATE, ALTER, DROP, REFERENCES ON *.* privileges as per this reference. Once you grant these permissions you should be able to use migrate commands.

using environment variables in docker-compose mounted files for initializing mysql

I am having trouble initializing mysql via docker-compose with the use of /docker-entrypoint-initdb.d whenever my initializing scripts requires environment variables.
I have the following docker-compose.yml 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
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- $MYSQL_PORT:3306
volumes:
db-data:
This is my ./init.sql
CREATE DATABASE IF NOT EXISTS ${MYSQL_DB};
GRANT ALL PRIVILEGES ON ${MYSQL_DB}.* TO '${MYSQL_USER}'#'%';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
When I run docker-compose up, I get an error with my ./init.sql and here's what it says:
mysql | 2021-07-16 14:53:17+00:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
mysql | ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '{MYSQL_DB}' at line 1
Everything works perfectly if I change my ~/init.sql to use hardcoded values like this :
CREATE DATABASE IF NOT EXISTS testingdb;
GRANT ALL PRIVILEGES ON testingdb.* TO 'testinguser'#'%';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
# where testingdb and testinguser is my .env.MYSQL_DB and .env.MYSQL_USER respectively
How do I use environment variables in docker's volume mounted files?
Env variables can be used in .sh file, so you can achieve what you want like this:
Create an init_db.sh file (instead of init_db.sql)
Then in the init_db.sh file:
echo "** Creating default DB and users"
mysql -u root -p$MYSQL_ROOT_PASSWORD --execute \
"CREATE DATABASE IF NOT EXISTS $MYSQL_DB;
GRANT ALL PRIVILEGES ON $MYSQL_DB.* TO '$MYSQL_USER'#'%';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
echo "** Finished creating default DB and users"

Connection refused on mysql with user granted on %

I see other question like this here on S.O. but no one solved my problem.
I'm running MySQL using Docker, and on the first boot, I'm running some SQL queries to prepopolate the DB. One of those is the following:
CREATE USER 'user'#'%' IDENTIFIED BY 'user';
Grant All Privileges ON *.* to 'user'#'%';
FLUSH PRIVILEGES;
However, when I try to connect to the DB from another container, I get:
SQLSTATE[HY000] [2002] Connection refused (SQL: ...)
What am I doing wrong?
This happens both if I use the name of the container, and 127.0.0.1 as host
my docker-compose looks like this:
#MySQL Service
db:
image: mysql
container_name: db
restart: unless-stopped
tty: true
ports:
- "3307:3306"
- "8001:3306"
environment:
....
volumes:
- ./database/dbdata:/var/lib/mysql/:rw
- ./config/mysql/my.cnf:/etc/mysql/my.cnf
the strangest part of all of this, is that if I connect to the DB with a MySQL client installed on my PC, the connection works fine...
my.cnf is the following:
[mysqld]
general_log = 1
general_log_file = /var/lib/mysql/general.log
secure-file-priv = NULL
bind-address = 0.0.0.0

Connecting a NodeJS container to a MySQL database

Before marking this post as a duplicate note that I've already looked at the other related posts here and pretty much everywhere I could without finding a solution to my precise case. I just keep getting confused between docker-compose.yml versions and linking the ports to each other.
Here's the issue:
I've created two containers with Docker, one hosting a NodeJS server, one hosting a MySQL database.
My docker-compose.yml looks like this:
version: '3'
services:
server:
build: ./server/
ports:
- "8080:8080"
depends_on:
- db
db:
build: ./db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_ALLOW_EMPTY_PASSWORD: 1
MYSQL_DATABASE: dashboard_nodejs
MYSQL_USER: root
MYSQL_PASSWORD: password
client:
image: angular_app
build: ./front/
ports:
- "4242:80"
depends_on:
- server
When running docker-compose up, all of my apps are built and run without an issue until the server tries to connect to MySQL, exiting with :
Error: connect ECONNREFUSED 172.20.0.2:3306 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
The dockerfile for my database only contains
FROM mysql
COPY init_db.sql /docker-entrypoint-initdb.d/
and init_db.sql currently is
CREATE DATABASE IF NOT EXISTS dashboard_nodejs;
GRANT ALL PRIVILEGES ON dashboard_nodejs.*
TO 'root'#'%' IDENTIFIED BY 'password'
WITH GRANT OPTION;
Finally, my server attemps to establish the connection with the following parameters:
var con = mySql.createConnection({
host: "db",
user: "root",
password: "password",
database: "dashboard_nodejs"
});
I have no clue why the connection is refused by mysql even with (what looks to me like) the right credentials.
I apologize in advance if this particular case has already been discussed.
Add ports: "3306:3306" to the db service.

Can't login as root into MySQL container

I have a docker-compose file setup like this:
version: '3'
services:
database:
image: mysql:5.6
volumes:
- dbdata:/var/lib/mysql
environment:
- "MYSQL_DATABASE=my_database_name"
- "MYSQL_USER=my_database_user"
- "MYSQL_PASSWORD=my_database_password"
- "MYSQL_ROOT_PASSWORD=my_root_password"
ports:
- "33061:3306"
volumes:
dbdata:
I'm trying to login to the mysql cli client with root user & password (by first going into the container itself):
mysql -uroot -pmy_root_password
But I keep getting this error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
I didn't set the mysql root password env var from the very beginning. I had to remove the created volume "dbdata" and run docker-compose up again. That fixed it. Thanks for the help!