Using Laravel Sail with a separate testing database - mysql

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;

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.

MySQL only runs when Workbench connects to port 3306

I am running into an issue and I can't wrap my head around.
I want to use JDBC to connect to a MySQL database inside a docker container however I get this:
Cannot create PoolableConnectionFactory (Could not create connection
to database server. Attempted reconnect 3 times. Giving up.)
<Context>
<Resource name="jdbc/moviedb"
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
type="javax.sql.DataSource"
username="user"
password="pass"
url="jdbc:mysql://localhost:3306/data?autoReconnect=true&useSSL=false"/>
</Context>
I then download MySQL Workbench to make sure the MySQL was running on the port 3306. And it was running and my tables and data are being displayed. I went and refreshed my app and all of a sudden it displayed the data. Why is this happening? I feel like it has something to do with localhost vs 127.0.0.1 (or whatever it is). And this same project was working last week without the workbench, however, the only difference was I was not using a docker-compose file.
This is my docker-compose file for the container:
version: "2.1"
services:
database:
container_name: data
image: mysql
volumes:
- ./data.sql:/docker-entrypoint-initdb.d/data.sql
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: "data"
MYSQL_USER: "user"
MYSQL_PASSWORD: "pass"
MYSQL_ROOT_PASSWORD: "root"
Any insight will be greatly appreciated.
I forgot to metion that when i first create the container with docker-compose up workbench does not connect to the database. only have i just the container down then do docker compose start does workbench connect to the server.
So I found out what the issue was my assignment required me to use an SQL file with 180,000 insert statements that is why it was taking so long. I had to manually change them to be only one insert statement with multiple values.

Docker Compose - Creating and connecting to mysql container

Having a hard time trying to get to grips with mysql in docker. I have got the container running with docker-compose, but I am not able to connect to the database via any tools such as phpmyadmin, workbench or tableplus.
I have connected directly to the running container and run
mysql -uroot -p
and entered the root password which I have passed, but this fails with this error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
Here is my docker-compose.yml file:
version: '3'
services:
db:
image: mysql
restart: always
environment:
MYSQL_DATABASE: quotes
MYSQL_USER: quotes
MYSQL_PASSWORD: P#KhzKZp)56sU8n+
MYSQL_ROOT_PASSWORD: +\VrLG*<t5sq[\\shR29u#n~A3^Jp*
ports:
- '3306:3306'
volumes:
- /private/mdbdata/quotes:/etc/mysql/conf.d
expose:
- '3306'
Been on this for days... hope someone can help!
I think your container is looking for a MySQL server on 'localhost', which WILL NOT WORK. 'localhost' to a container is the container itself - NOT the host machine it's running on.
You should be able to access the MySQL server using the host machine IP address.
Finally solved this one ... I had to remove all special characters from the password strings.
I tired adding single and double quotes are around the strings to see if that would allow the special characters, but that still failed. Passwords needed to be alphanumeric.
Have you tried the solution provided here? Basically, if you had a container running previously, you have to explicitly purge it since docker keeps track of root passwds (via volumes) from previously instantiated containers. This happens with a lot of tools, it's not unique to MySQL containers. A simple docker-compose rm -v should suffice, afterwards bring up your container. This basically deletes the old volume from the disk, removing all data from previous container instantiation.
You can also call a docker ps, find your container and execute docker rm -v <CONTAINER_NAME>. Bring up your container afterwards.

Spring Boot & MySql - cannot use a new service database username / password

How can I change the database user password so that the Spring Boot application can still use the database? The environment is Docker and Jenkins.
Changing the password from 'password1' to 'password2' does not work. The Sprint Boot application fails to connect to the (MySql) database. The error is: cannot connect due to invalid user credentials.
My environment consists of:
1 - My docker-compose.yml I have:
services:
xyzdb:
image: mysql:8.0.12
environment:
- MYSQL_ROOT_PASSWORD=admin
- MYSQL_DATABASE=geosoldatabase
- MYSQL_USER=johan
- MYSQL_PASSWORD=${LOCAL_MYSQL_PASSWORD}
volumes:
- /var/mysql-data:/var/lib/mysql
ports:
- "3306:3306"
Etc.
2 - The Spring Boot apllication.properties file contains:
spring.datasource.url = jdbc:mysql://xyxdb:3306/geosoldatabase
spring.datasource.username = johan
spring.datasource.password = bladiebladiebla <== very first password
3 - To change the password I did the following actions:
Changed the user password by entering the database docker container and change the name with:
ALTER USER 'johan'#'%' IDENTIFIED BY 'xyzabc123';
ALTER USER 'johan' IDENTIFIED WITH mysql_native_password BY 'xyzabc123';
Commit;
flush privileges;
Updated 2 environment variables in my Jenkins System environment
LOCAL_MYSQL_PASSWORD=xyzabc123
SPRING.DATASOURCE.PASSWORD=xyzabc123
This does not work. Starting the Spring Boot application (after rebooting Jenkins) fails on not being able to connect to the database with the user name. There is a wrong password.
When I change all 3 updates (in the above 2 points) back to the initial password, then everything works fine.
What did I forget?
For months, no solution was suggested. That's a signal.
Since a few months I work with a Dockerized version of MySQL. That works fine. No issues yet.
The 'good' workaround is by using MySQL via Docker.

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.