Access denied when running migration on Prisma - mysql

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.

Related

Laravel Docker Container Cannot connect to remote AWS RDS Database

I'm working with a containerized Laravel app that is supposed to be connecting to a remote rds database, here is a sample .env
DB_HOST=xxxxxx.rds.amazonaws.com
DB_DATABASE=sample
DB_USERNAME=sample
DB_PASSWORD=sample
DB_PORT=3306
DATABASE_DRIVER=mysql
The container works as it should but the problem is, it cannot connect to the remote rds database, when I try running composer ie:
$ docker exec -ti laravel-php bash
$ composer install
I get this error:
[PDOException]
SQLSTATE[HY000] [1045] Access denied for user 'sample'#'192.168.66.1' (using password: YES)
Script php artisan clear-compiled handling the post-install-cmd event returned with error code 1
192.168.66.1 as my docker container's ip, I suspect that the db policy is open via #localhost access since my dev ops confirmed that it's open for public connections.
I'm using docker-compose version 2 btw, here's a sample docker-compose:
version: '2'
services:
sample-server:
build:
context: ./
dockerfile: sample.server.docker
volumes:
- ../backend:/var/www
ports:
- "8081:80"
environment:
- VIRTUAL_HOST=sample.local
links:
- sample-php
depends_on:
- sample-php
sample-php:
build:
context: ./
dockerfile: sample.php.docker
volumes:
- .:/var/www
links:
- sample-database
environment:
- "DB_PORT=3306"
- "DB_HOST=sample-database"
sample-database:
image: mysql:5.7
environment:
- "MYSQL_ROOT_PASSWORD=samplepassword"
- "MYSQL_DATABASE=sample"
ports:
- "33081:3306"
sample-nginx-proxy:
image: jwilder/nginx-proxy
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
networks:
default:
external:
name: sample-nginx-proxy
How can I fix this?
Check the following:
Database is publicly accessible:
Connecting outside the VPC that the database resides, more specifically accessed over the internet, requires that the database is configured for Public Accessibility. Which you said is already done. As you have an internal IP, and the database does not have a public IP, this is not really required.
Basic Configuration:
Check that the database name, and port is set correctly, which I am sure you have done.
Security Group Inbound Rules:
This is most likely the case, the database will have one or more security groups. Ensure that the security group is configured to allow inbound access from the client in your case: 192.168.66.1
Confirm the IP address of the client:
192.168.66.1 is a strange IP for the container, the first 4 IP Addresses of a VPC Subnet are reserved.
Confirm the network routing:
Confirm that the VPC that contains the client can connect to the database. As the client is running within a docker container ensure that the container can access the database. Easy way to do this is enable ICMP packets on an EC2 instance in the database subnet, and check you can Ping it or use the VPC route analyser.
Check the database user rights:
Can the database user connect for any address not localhost.
Security on the VPC:
Check the ACLs of the subnets for both inbound and outbound
UPDATE:
Here is a link from AWS: Troubleshooting for Amazon RDS.
I guess that's a MySql issue, how did you create the user?
If you want to allow access from everywhere just put %:
GRANT ALL PRIVILEGES ON *.* TO 'sample'#'%' IDENTIFIED BY 'samplepassword' with grant option;
FLUSH PRIVILEGES;

mysql docker image how to create databases with their own username and password

I have a docker image as follows:
version: '3.6'
services:
# MySQL
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mydb
MYSQL_USER: user
MYSQL_PASSWORD: user
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3306:3306'
expose:
# Opens port 3306 on the container
- '3306'
volumes:
- ./init:/docker-entrypoint-initdb.d
The init folder directory is as follows:
init
01.sql
The 01.sql is defined as follows:
CREATE DATABASE IF NOT EXISTS `test`;
GRANT ALL ON `test`.* TO 'user'#'%';
When I execute docker-compose up the two databases are created successfully, but both mydb and test are using same username and password. Is it possible to create a different user and password for test database?
update:
I have updated the script 01.sql to:
CREATE DATABASE IF NOT EXISTS `test`;
CREATE USER 'newuser'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON test.* TO 'newuser'#'localhost' WITH GRANT OPTION;
But when I try to connect using DBeaver(username:newuser,password:password) get the error access denied.
However when I connect using username:root, password:root i can see the table created:
Thanks in advance
Any idea what i am doing wrong please?

Using Laravel Sail with a separate testing database

I'm using Laravel Sail as my development environment. According to the docs,
when the MySQL container is starting, it will ensure a database exists whose name matches the value of your DB_DATABASE environment variable.
This works perfectly for my development environment, but not so much when it comes to testing since my .env.testing defines a separate database, and it seems this database does not get created - when I sail mysql into the container and run show databases; it is not listed. As a result, running sail test fails every test where the database is concerned.
SQLSTATE[HY000] [1045] Access denied for user ...
My .env file contains this:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=dev
My .env.testing file contains this:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME and DB_PASSWORD are the same in both files.
How can I create this database so that it's available when running sail test?
EDIT:
As I dug through the repository code I found that the database is being created when the mysql container image is built, but it doesn't look like there's an option for creating multiple databases.
MYSQL_DATABASE
This variable is optional and allows you to specify the name of a database to be created on image startup. If a user/password was supplied (see below) then that user will be granted superuser access (corresponding to GRANT ALL) to this database.
Add the following to docker-compose.yml under the services: key and set your host in .env.testing to mysql_test:
mysql_test:
image: "mysql:8.0"
environment:
MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
MYSQL_DATABASE: "${DB_DATABASE}"
MYSQL_USER: "${DB_USERNAME}"
MYSQL_PASSWORD: "${DB_PASSWORD}"
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
networks:
- sail
You should be able to create the test database alongside the original dev database on the existing mysql container:
sail mysql
mysql> CREATE DATABASE test;
I have not tested this using mysql but the same process works on postgres:
sail psql
dev=# CREATE DATABASE test;

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.

Not able to login to mysql docker container with proper credentials

Here is my docker-compose file to create mysql container
Though, it really sounds a easy problem but I have tried all possible solutions and than posted it after nothing has worked.
The container is running successfully. Before this the mysql username and pass was different.
After changing I am having the issue.
I have totally removed the images I have removed all running
containers I have removed the volumes made
Still I am not able to login to the running container. Either using root username password or the created user and pass
mysql:
image: mysql5.6image(comes from ECR)
container_name: mysqlcont
restart:always
environment:
MYSQL_ROOT_PASSWORD: some_pass
MYSQL_DATABASE: databasename
MYSQL_USER: username
MYSQL_PASSWORD: some_pass
volumes:
- ./sqldata:/var/lib/mysql
Error Message:
Access denied for user 'root'#'localhost'(using password:yes)
It works only when:
I have to set new volume, every time I run docker-compose up else
the user that I am passing via environment is not working.