I'm trying to run another instance of MySQL on separate port with different config.
Attempt 1
docker run --name dbname -v /home/custom-mysql-configs/dbname-config-folder:/etc/mysql -p 0.0.0.0:3312:3306 -e MYSQL_ROOT_PASSWORD=mysupersecretpassword -d mysql:5.5
When I run SHOW VARIABLES; in mysql -u root -p --host=127.0.0.1 --port=3312 I get the same variables as mysql -u root -p --host=127.0.0.1 --port=3306 that are set in /etc/mysql/my.cnf
Attempt 2
docker run --name dbname -v /home/custom-mysql-configs/dbname-config-folder:/etc/mysql/conf.d -p 0.0.0.0:3312:3306 -e MYSQL_ROOT_PASSWORD=mysupersecretpassword -d mysql:5.5
Then when I ran
mysql -u root -p --host=127.0.0.1 --port=3306
I got the following error:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading
initial communication packet', system error: 0
What am I doing wrong and how should I run the new MySQL instance?
If you're running your new container with:
$ docker run --name dbname -v /home/custom-mysql-configs/dbname-config-folder:/etc/mysql/conf.d -p 0.0.0.0:3312:3306 -e MYSQL_ROOT_PASSWORD=mysupersecretpassword -d mysql:5.5
You should be connecting to it through:
$ mysql -u root -p --host=127.0.0.1 --port=3312
Your command was connecting to your original container, on port 3306.
I'd also suggest showing some of your MySQL conf.d files, as it might help further.
Related
mysql --host 127.0.0.1 --port 23306 --user root -proot
I have no idea was -proot is and I cannot find it in any documentation.
I am following the following instructions on how to set up a docker container with my SQL:
Connect to mysql in a docker container from the host
The password used is actually root.
It's the same as:
mysql --host 127.0.0.1 --port 23306 --user root --password=root
I already have installed mySql on my pc so port 3306 is already busy. This is the reason why I have to use a different port from 3306. I want to be able to connect with my machine to my docekr instance without using docker commands so I will be able to connect to that instance with my application (Spring web app).
Docker commands that I used:
docker run --name jt-mysql -e MYSQL_ROOT_PASSWORD=password -p 3307:3307 -d mysql
Then I tried to connect to that istance with:
mysql --user=root -P 3307 -p
In this case I get the following error:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using
password: YES)
Please note that if I tried to use the instance installed on my pc it works, using with:
mysql --user=root -P 3306 -p
Other information about my docker instance using:
docker ps
I get:
f52a94aa63da mysql "docker-entrypoint.s…" 4
minutes ago Up 4 minutes 3306/tcp, 33060/tcp,
0.0.0.0:3307->3307/tcp jt-mysql
with status insided my docker image (entering using docker commands) I get:
Connection id: 11
Current database:
Current user: root#localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.0.19 MySQL Community Server - GPL
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: latin1
Conn. characterset: latin1
UNIX socket: /var/run/mysqld/mysqld.sock
Binary data as: Hexadecimal
Uptime: 16 min 50 sec
using env command:
HOSTNAME=f52a94aa63da
MYSQL_ROOT_PASSWORD=password
PWD=/
HOME=/root
MYSQL_MAJOR=8.0
GOSU_VERSION=1.7
MYSQL_VERSION=8.0.19-1debian9
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env
Command to start MySQL container at port 3306 and expose at port 3307
docker container run -d --name=LocalMySQLDB -p 3307:3306 -e MYSQL_ROOT_PASSWORD=password mysql
OR
docker run -d --name=LocalMySQLDB -p 3307:3306 -e MYSQL_ROOT_PASSWORD=password mysql
The above command with start the MySQL database server inside "LocalMySQLDB" container
Now to connect to the containerized mysql instance use below attached command
mysql -h 127.0.0.1 -uroot -P 3307 -ppassword
I have tried this many a times on my local machine for testing purposes. It will definitely work for you as well.
Please comment if it will not work in your case.
Start Docker container using following command:
docker run -d -p 3307:3306 --name mysql_server -e MYSQL_ROOT_PASSWORD=123456 mysql
Connect to container from host using following command:
mysql -u root -P 3307 --protocol=tcp -p
When you run docker container, please try to add this param at the end.
docker run --name jt-mysql -e MYSQL_ROOT_PASSWORD=password -p 3307:3306 -d mysql --network host
I am trying to connect to mysql server using mysql cli. Image was created using following command:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=****** -d mysql
The docker container is running. docker ps prints:
johnd#john-pc:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
598c0f8680dc mysql "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 3306/tcp, 33060/tcp some-mysql
When i enter the following command:
mysql -h 127.0.0.1 -P 3306 -u root -p it returns:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
I also tried with --protocol=tcp attribute. How do i connect from client to mysql server running on docker using terminal from client machine (not from another docker)
EDIT:
I also tried connecting to mysql using this command:
docker run -it --rm mysql mysql -h127.0.0.1 -uexample-user -p
It returns the same error
mysql -h 127.0.0.1 -P 3306 -u root -p
You are connecting to your localhost's sql server but you didn't map the docker's container port to the host.
Try mapping the port to your host by this:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=****** -d -p 3306:3306 mysql
Then retry:
mysql -h 127.0.0.1 -P 3306 -u root -p
same error to me, just remember start mysql service first in your container.
exec in your container
/etc/init.d/mysql start
Using internal docker IP like 127.0.0.1 didn't work for me
I used my POD's IP and it worked
mysql -h 10.10.10.41 -P 3306 -u root -p
I still don't understand why I wasn't able to login and before and after this command I was able to login
is there any way to connect remote docker container mysql server?
I am installing magento web application, now I have a situation like I need to use/point the existing remote docker container database. I have make port forwarding in order to access database from remote machine but it doe not work.
docker run -it -d -p 3002:80 -h tm.gworks.mobi -v /var/www/public --privileged --name database magedev
For testing purpose in remote machine I have tried like mysql -u root -h 192.168.1.21:3002 -p in mysql console but it does not connect, it throws error ERROR 2005 (HY000): Unknown MySQL server host '192.168.1.21:3002' (-2)
Docker run command should be,
docker run -it -d -p 3002:3306 -h tm.gworks.mobi -v /var/www/public --privileged --name database magedev
default mysql port is 3306 but I listen port 80 which is my nginx port so it can't be to allow.
mysql -u root -h 192.168.1.21 -P 3002 -p
now everything works fine
I have a Dockerfile that I am working on that pulls Mysql 5.6 and configures it (mostly with a bash and sql script). I am able to build and run it but when I try to connect to the database in the container I always get:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I have tried accessing the mysql database by using:
mysql -u root -p
mysql -u root -h 127.0.0.1 -p
I have tried everything I could think of and looked up articles on the internet but nothing works. Can someone tell me why? Here is my Dockerfile and bash script respectively:
FROM mysql:5.6
MAINTAINER Ryan K.
USER root
ADD mysqlAddUser.sh /tmp/
CMD ["/tmp/mysqlAddUser.sh"]
ADD foo.sql /docker-entrypoint-initdb.d/foo.sql
EXPOSE 3306
## Starting mysqld and running Database Scripts
CMD ["/usr/bin/mysqld_safe"]
Bash script:
#!/bin/bash
DATABASE_PASSWORD=test
/usr/bin/mysqld_safe &
mysqladmin --login-path=local -uroot -p"$DATABASE_PASSWORD"
mysqladmin password "$DATABASE_PASSWORD"
mysql -uroot -p"$DATABASE_PASSWORD" -e "UPDATE mysql.user SET Password=PASSWORD('$DATABASE_PASSWORD') WHERE User='root'"
mysql -uroot -p"$DATABASE_PASSWORD" -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"
mysql -uroot -p"$DATABASE_PASSWORD" -e "DELETE FROM mysql.user WHERE User=''"
mysql -uroot -p"$DATABASE_PASSWORD" -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%'"
mysql -uroot -p"$DATABASE_PASSWORD" -e "FLUSH PRIVILEGES"
DB_ROOT_PASS=TEST
DB=portal
mysql --login-path=local -uroot -p"$DB_ROOT_PASS"
mysql -uroot -p"$DATABASE_PASWORD" -e "CREATE DATABASE portal";
mysql -uroot -p"$DATABASE_PASSWORD" -e "CREATE USER portaluser#'localhost' IDENTIFIED BY 'testing'";
mysql -uroot -p"$DATABASE_PASSWORD" -e "GRANT ALL PRIVILEGES ON portal.* TO portaluser#'localhost'";
mysql -uroot -p"$DATABASE_PASSWORD" -e "FLUSH PRIVILEGES";
mysql -uroot -p"$DATABASE_PASSWORD" $DB < /tmp/foo.sql
Was struggling with the same issue with mysql in a docker container. Sometimes I could connect with the mysql client, but more often not. Switched to mariadb and had the same problem.
What seems to have fixed it for me is to add some sleep commands in my scripts that create, start and destroy the docker containers. After commands like 'docker run' and 'docker stop' I added 'sleep 10' and that seems to help.