I have 2 containers, one for the main logic in my app and another for a mysql db. This is the docker-compose file
version: '3'
services:
database:
image: mysql
container_name: mysql-db
command: --default-authentication-plugin=mysql_native_password
restart: always
env_file:
- environment-variables.env
ports:
- 3306:3306
logic:
container_name: main-logic
build: ./logic/.
The logic is in golang and I am attempting to connect to the database. Here is the go code.
err := godotenv.Load("/environment-variables.env")
if err != nil {
log.Fatalln("Error in fetching environment variables", err)
}
dataSourceName := fmt.Sprint("root:", os.Getenv("MYSQL_ROOT_PASSWORD"), "@tcp(docker.for.mac.localhost:3306)/some-db")
log.Println(dataSourceName)
db, err := sql.Open("mysql", dataSourceName)
if err != nil {
log.Fatalln("Some error in connecting to the database in the method", err)
}
db.SetMaxOpenConns(1)
err = db.Ping()
log.Println("Error is", err.Error())
defer db.Close()
Within the environment variables file is
MYSQL_ROOT_PASSWORD=password
MYSQL_DATABASE=some-db
When I attempt to connect, I always get the error:
Error is Error 1045: Access denied for user 'root'@'localhost' (using password: YES)
How to resolve this?