Docker - Can't access MySQL CLI in container - mysql

In my MySQL container that I have configured per the official image, it does not allow me to access the CLI from my host machine, nor any other of my containers, despite being linked:
Now, I know that I have not configured external access on my MySQL container, but how can I configure it if I can't even access the CLI to grant myself permissions? Could I create a Dockerfile and change some system settings before the initial installation of MySQL? If so, how would I go about doing that?
My fig.yml file, which is used to set up my MySQL container:
mysql:
image: mysql:latest
volumes:
- .:/mydir
working_dir: /mydir
ports:
- "3306:3306"
environment:
- MYSQL_DATABASE=mydb
- MYSQL_ROOT_PASSWORD=mypass

The answer is in your screenshot. (using password: NO). Add a -p to your mysql command line.

You can try to set the environment variable MYSQL_PWD with the password for root. It's the official environment variable for the client.
http://dev.mysql.com/doc/refman/5.6/en/password-security-user.html

Related

Cannot connect to mysql server (root) inside docker container executed with docker-compose: Access denied

I am running a mysql server inside a docker container using docker-compose, here is my yaml file:
version: '3'
services:
mysqltest:
image: mysql
network_mode: host
ports:
- "3306:3306"
volumes:
- "/home/myuser/bds/mysql/:/var/lib/mysql"
user: "1000:1000"
environment:
- "MYSQL_ROOT_PASSWORD=mysecret"
The container loads file with the docker-compose up command, but when i try to connect from the host machine to the mysql server with user root, it fails:
mysql -h 127.0.0.1 -u root -pmysecret
ERROR 1045 (28000): Access denied for user 'root'#'127.0.0.1' (using password: YES)
However, i can connect to the server inside the container using the same command, if i start the container using docker command line:
docker run -it --rm --name mysqltest --user 1000:1000 -v /home/myuser/bds/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysecret -p 3306:3306 -d mysql
I tried setting network_mode to host, none and also without specifying it, but the result is the same using docker-compose.
What could be wrong with my YAML file so that i cannot connect as when i use docker command ?
Thanks in advance
I assume you didn't change your default Docker configuration. By default Docker will run in the network mode: 'bridge' and not host. See the difference here.
You can check using docker inspect container.
When you just start the container using the command you will start your container in bridge mode and the container port 3306 will be mapped on 3306. This will not happen when you try host. Again see the link for the difference.
So update your docker-compose.yaml and define bridge as network mode:
version: '3'
services:
mysqltest:
image: mysql
network_mode: bridge
ports:
- "3306:3306"
volumes:
- "/home/myuser/bds/mysql/:/var/lib/mysql"
user: "1000:1000"
environment:
- "MYSQL_ROOT_PASSWORD=mysecret"
SOLVED !! The issue was originated from setting different mysql root passwords with the different docker commands:
The first time i was testing the container i user std docker command and the environment MYSQL_ROOT_PASSWORD=othersecret. Everything worked fine. i connected from host using mysql command and restored a database
Once i finished testing, i wrote yaml file, but with the root password i wanted to use for producction:
environment:
MYSQL_ROOT_PASSWORD: "mysecret"
But then, when i started the container with docker-compose and the yaml file, i could not connect from host using mysql command. It seems the original root password ("othersecret") was written to the user table inside the mysql schema and that is why i could not connect using the password set in the yaml file. I cleaned up the mysql directory and started the container using docker-compose (without specifying a network mode) and finally i was able to connect from host.

Can't connect to database: Access denied for user

I am setting up a CakePHP 3.7 application and using docker compose. I have a mysql service as well that I'm trying to connect to, but I am getting this error: Access denied for user 'ws_user'#'172.20.0.3' (using password: YES)
I am granting permissions to the user like so: GRANT ALL PRIVILEGES ON mydb.* TO 'ws_user'#'%' IDENTIFIED BY '<superSecretPasswordHere>'.
If I use the root credentials, cakephp is able to make the connection just fine.
I also expose the mysql service on port 3030 to my local machine and I am able to connect with the ws_user credentials just fine.
I also setup mysql running on my local machine with the same credentials and cake is able to connect to host 172.17.0.1 just fine as well.
I'm perplexed as what could be the problem. It sure seems like it's a permissions problem (because of the error message), but I'm able to connect via the exposed port via the command line. My next thought was that it might be because of special characters in the password, but again, if I connect to mysql running on my host machine, it works fine with the same password.
Here is my docker-compose file:
version: '2'
# define all services
services:
# our service is called CakePHP ;-)
cakephp:
# we want to use the image which is build from our Dockerfile
build:
context: .
dockerfile: Dockerfile
# apache is running on port 80 but we want to expose this to port 4000 on our local machine
ports:
- "80:80"
# we are depending on the mysql backend
depends_on:
- mysql
# we mount the working dir into the container, handy for development
volumes:
- .:/var/www/html/
environment:
- SECURITY_SALT
- MYSQL_HOST
- MYSQL_USERNAME
- MYSQL_PASSWORD
mysql:
# we use the mysql base image, version 5.6.36
#image: mysql:5.6.39
build:
context: .
dockerfile: Dockerfile.mysql
ports:
- "3030:3006"
# we mount a datavolume to make sure we don't lose data
volumes:
- mysql_data:/var/lib/mysql
# setting some envvars to create the DB
environment:
- MYSQL_ROOT_PASSWORD
- MYSQL_DATABASE
volumes:
mysql_data:
From "cakephp" you connect to "mysql:3306". This should be in your connection string.
From your host you can connect to "127.0.0.1:3030" to verify that your database accepts remote login.
Then you should check the credentials that they are the same. I suggest you put them in a .env file and then test the connection by "copy-paste" of the values.
you can check the values that are actually passed to the containers by running:
docker-compose config
This shows you the exact version of the docker-compose file that will be sent to the docker engine.
Hope this works, otherwise drop me a comment.

Docker Wordpress connection to the database server on the localhost

I run docker wordpress image with command
docker run --name test-wordpress -p 8081:80 -d wordpress
MySQL 8 is on localhost on Windows 10. Database credentials are valid.
In wordpress setup I use this configuration
Database name: wordpress (not exist yet)
Username: root
Password: ***
Database host: localhost || 127.0.0.1 || host.docker.internal
I get error
Error establishing a database connection
What is correct database host?
By default, docker will attach your new container to a bridged network. This means that addresses such as: localhost and 127.0.0.1 only refers to the container itself. Not the host machine.
The easy was to solve this, is to wrap the MySQL database in a container of it's own. This way your containers can address eachother without issues.
Connect to MySQL database on the host
If you really want to connect the service in the container with a service on the host, you will need to connect the container to the appropriate network.
First, you will need to create a network. Assuming that your local machine has a fixed IP of 192.168.0.1, you should be able to do this with:
docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 dockernet
You can then:
docker run --name test-wordpress --net=dockernet -p 8081:80 -d wordpress
And you should then be able to refer to the host from inside the container by the IP: 192.168.0.1.
Create stack with wordpress and MySQL
The better alternative here though, is to create an application stack definition with docker-compose, that includes both the database and the wordpress application.
You can create a docker-compose.yml file like this:
docker-compose.yml
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
And the start the stack with this:
docker-compose up
Then visit: http://localhost:8000
Notice that the database data will be stored in the docker managed volume called db_data.
Details on installing docker-compose can be found here: https://docs.docker.com/compose/install/
Start both containers with just docker run
The same can be achieved with just docker run, like this:
docker volume create db_data
docker network create mysqlnet
docker run --name test-mysql -e MYSQL_ROOT_PASSWORD=somewordpress -e MYSQL_DATABASE=wordpress -e MYSQL_USER=wordpress -e MYSQL_PASSWORD=wordpress -v db_data:/var/lib/mysql --net=mysqlnet -d mysql:5.7
docker run --name test-wordpress -e WORDPRESS_DB_HOST=test-mysql:3306 -e WORDPRESS_DB_USER=wordpress -e WORDPRESS_DB_PASSWORD=wordpress -e WORDPRESS_DB_NAME=wordpress --net=mysqlnet -p 8081:80 -d wordpress:latest
You can change the mapping of the mysql datafiles to a local directory instead, and just ommit the docker volume create statement.
Found a great article explaining How to setup Wordpress using Docker.
The issue is MySQL container is not accessible from the wordpress container, so to fix this we will have to create docker network and connect both the containers.
Lets create a docker network -
> docker network create --attachable wordpress-network
Now attach both the containers with following command -
> docker network connect wordpress-network mysql-container
> docker network connect wordpress-network wordpress-container
Now open the wordpress in browser and set mysql-container as Database Host
The accepted answer is incomplete and inaccurate. You can very easily connect to MySQL running in host machine with wordpress running inside container by providing --network=host option during docker run.
Issue With Wordpress:
The only caveat is wordpress can't connect with MySQL with default setup.
In Ubuntu systems running MySQL 5.7 (and later versions), any MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external incompatible program like wordpress to access the user.
In order to use a password to connect to MySQL as user, you will need to switch its authentication method from auth_socket to mysql_native_password.
Solution:
Create a dedicated mysql user for wordpress database in legacy mysql_native_password mode.
$> sudo mysql
mysql> CREATE USER 'wpuser'#'localhost' IDENTIFIED WITH mysql_native_password BY 'wp_password';
mysql> CREATE DATABASE wordpress_db CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
mysql> GRANT ALL ON wordpress_db.* TO 'wpuser'#'localhost';
mysql> FLUSH PRIVILEGES;
Run Wordpress container:
docker run --rm --name my_wordpress --network=host -e WORDPRESS_DB_USER=wpuser -e WORDPRESS_DB_PASSWORD=wp_password -e WORDPRESS_DB_NAME=wordpress_db -e WORDPRESS_DB_HOST=127.0.0.1 wordpress

MySQL in docker-compose -- access denied

I try to start MySQL server with docker-compose. Here is docker-compose.yaml part:
mysql:
restart: always
image: mysql:latest
ports:
- "3306:3306"
volumes:
- /Users/user/Documents/.docker/mysql/config:/etc/mysql/
- /Users/user/Documents/.docker/mysql/data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD='123'
- MYSQL_ROOT_HOST='172.18.0.1'
You see I've specified root password and host as it is said here. Then I try to connect to db (using Intellij Idea if that matters):
jdbc:mysql://localhost:3306/?user=root&password=123&ssl=false
But it doesn't accept the credentials and writes to log:
Access denied for user 'root'#'172.18.0.1' (using password: YES)
Please advise on how to fix it. Thanks.
Most likely you have initialized the mysql data directory when these were different:
environment:
- MYSQL_ROOT_PASSWORD='123'
- MYSQL_ROOT_HOST='172.18.0.1'
MySQL image only honors those vars when the /var/lib/mysql directory is created.
So if you don't care about the data, empty your volume: /Users/user/Documents/.docker/mysql/data, or change the credentials manually from mysql terminal.
if not in production you can also use the below with docker run
-e MYSQL_ROOT_HOST=%
Also it will be better to create your own docker network
In my case
dataSource.setCatalog(...)
helped.

Using docker-compose in order to create a MySQL schema/database

I am trying to create a mysql database/schema if it doesn't already exist.
Here is what I have tried:
docker-compose.yml
mysql:
image: mysql:5.6.26
environment:
- MYSQL_ROOT_PASSWORD=root
command: "mysql -uroot -proot < createDB.sql"
ports:
- "3306:3306"
createDB.sql
CREATE DATABASE IF NOT EXISTS bignibou;
It does not work. What would be the best way to use docker/docker-compose in order to create a schema if it does not exist?
I finally found the beginning of a solution.
The MySQL image takes an environment variable i.e. MYSQL_DATABASE that initialize the container with the name of the database on image startup See here for full documentation.
Or read the excerpt below:
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.
Here is what I came up with:
mysql:
image: mysql:5.6.26
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=bignibou
ports:
- "3306:3306"
I now need a way to specify the default collation but that is another story...
edit: For those interested in specifying a different collation from the default, here are the instructions to use another config file that will override the default one. See below:
Using a custom MySQL configuration file The MySQL startup
configuration is specified in the file /etc/mysql/my.cnf, and that
file in turn includes any files found in the /etc/mysql/conf.d
directory that end with .cnf. Settings in files in this directory will
augment and/or override settings in /etc/mysql/my.cnf. If you want to
use a customized MySQL configuration, you can create your alternative
configuration file in a directory on the host machine and then mount
that directory location as /etc/mysql/conf.d inside the mysql
container.
If /my/custom/config-file.cnf is the path and name of your custom
configuration file, you can start your mysql container like this (note
that only the directory path of the custom config file is used in this
command):
$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e
MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag This will start a new
container some-mysql where the MySQL instance uses the combined
startup settings from /etc/mysql/my.cnf and
/etc/mysql/conf.d/config-file.cnf, with settings from the latter
taking precedence.
To not lost your data better use volumes as well:
version: '3'
services:
db:
image: mysql:5.7
volumes:
- mysql-db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: my_db_name
ports:
- "3307:3306"
volumes:
mysql-db:
probably what you are trying to do needs an additional script. So if building an image instead of directly using a prebuilt image is an option for you, you need to use a Dockerfile and use a script file which first imports the script in MySql and then runs the service itself.
take a look at this answer: Docker - Initialize mysql database with schema
From the docker-compose documentation - see Define Services - you can tell which Dockerfile it will use to build the image. Therefore you can create a Dockerfile based on the mysql image and create the database inside it using standard Dockerfile commands.
This might be useful in case someone lands here in future. The real issue appears to be the "command" statement in the docker-compose file. Once the command finishes successfully the container will get destroyed. This sql script must be run only after docker-compose has run and containers have been created. docker-compose "command" is really to start a service in the container. In this case you overrode the mysql service with your command.