I am using mysql server inside docker container and able to access inside docker. How to create connection in mysql workbench running on my local(Host Machine).
相关问题
- Docker task in Azure devops won't accept "$(pw
- sqlyog export query result as csv
- NOT DISTINCT query in mySQL
- MySQL: conduct a basic search
- Unable to run mariadb when mount volume
You have to do few configuration in you docker container. Please follow the following steps.
Specify mysql configuration block in your docker-compose.yml. I have following mysql block under services object in my docker-compose.yml file.
Restart docker container and run following commands to get to the bash shell in the mysql container
Inside the container, to connect to mysql command line type,
Use MYSQL_ROOT_PASSWORD as specified in the docker-compose.yml . Execute following commands to create new user.
The percent sign (%) means all ip's. Restart the docker container.
In your MySQL Workbench provide the connection details. Use MYSQL_PASSWORD as specified in your docker-compose.yml file.
You should now be able to connect to your mysql container.
2 docker-related conditions:
first, your docker run must map the mysql port to an host port:
(for instance:
docker run -d -p 3306:3306 tutum/mysql
)second, if you are using docker in a VM (docker-machine, with boot2docker), you need to use the ip of
docker-machine ip <VMname>
, with the host mapped port.If you need to use
localhost
, you would need to do some port forwarding at the VirtualBox level:(
controlvm
if the VM is running,modifyvm
is the VM is stopped) (replace "boot2docker-vm
" by the name of your vm: seedocker-machine ls
)2 mysql-related conditions:
As illustrated in
nkratzke/EasyMySQL/Dockerfile
, you need to enable remote access:You need to create users when startig your database in your docker image.
See for instance
nkratzke/EasyMySQL/start-database.sh
, which is called by theDockerfile CMD
:@Krishna's answer worked but with a minor change - user was added as follows
see Authentication plugin 'caching_sha2_password' cannot be loaded
I got solution for this by setting field value in Hostname: 127.0.0.1 (Localhost), port by default 3306 with your creds.
By default after deployment MySQL has following connection restrictions:
Apparently, for the security purposes you will not be able to connect to it outside of the docker image. If you need to change that to allow root to connect from any host (say, for development purposes), do:
Start your mysql image with all port mappings required:
docker run -p 3306:3306 --name=mysql57 -d mysql/mysql-server:5.7
or, if the complete port mapping is required:
If this is the fresh installation - grab the default password:
docker logs mysql57 2>&1 | grep GENERATED
Connect using
mysql
client directly to the mysqld in docker:docker exec -it mysql57 mysql -uroot -p
If this is the fresh installation you will be asked to change the password using
ALTER USER
command. Do it.Run SQL:
update mysql.user set host = '%' where user='root';
Quit the
mysql
client.Restart the container:
docker restart mysql57
Now you will be able to connect from MySQL Workbench to
After all the changes the query will show:
Specify your configuration
docker-compose.yml
. More details here. Example:docker-compose up
and another terminal rundocker ps
to see your container.docker exec -it test-mysql bash
mysql -u root -p
.