Skip to main content
Bumped by Community user
edited title
Link

How to gain console access to a MySQL server running on a docker container from the host?

added 508 characters in body
Source Link

but clearly not. How do I check that the container connection is accessible? I'm trying:

root $ 
root $ telnet 127.0.0.1 3306
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
J
8.0.21%#%T:/]T�e-r&UR([/caching_sha2_password
^CConnection closed by foreign host.
root $ 
root $ 
root $ telnet 127.0.0.1 33060
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
root $ 

but that seems a bit odd.

but clearly not.

but clearly not. How do I check that the container connection is accessible? I'm trying:

root $ 
root $ telnet 127.0.0.1 3306
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
J
8.0.21%#%T:/]T�e-r&UR([/caching_sha2_password
^CConnection closed by foreign host.
root $ 
root $ 
root $ telnet 127.0.0.1 33060
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
root $ 

but that seems a bit odd.

deleted 974 characters in body
Source Link

So far as I can tellthought I had this default container works fineworking a few minutes ago:

root $ 
root $ docker container listls
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                   PORTS                               NAMES
7b119ef62a859eb4a18def01        mysql/mysql-server:latest   "/entrypoint.sh mysq…"   176 hoursminutes ago        Up 176 hoursminutes (healthy)   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql
root $ 
root $ mysql -h127.0.0.1:33060u -proot -urootp
Enter password: 
ERROR 20052002 (HY000): Unknown MySQL serverCan't hostconnect '127.0.0.1:33060'to (22)
rootlocal $MySQL 
rootserver $through mysqlsocket -h127.0.0'/var/run/mysqld/mysqld.1 -p -uroot
Enter password: 
ERROR 1130sock' (HY0002): Host '172.17.0.1' is not allowed to connect to this MySQL server
root $ 
root $ docker exec -ti --user root mysql bash
bash-4.2# 
bash-4.2# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 202116
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> exit
Bye
bash-4.2# 
bash-4.2# exit
exit
root $ 

How do I connect to the database directly from the host? Using:

Products Included in the Container

A number of MySQL products are included in the Docker container you created with the MySQL Server Docker image:

MySQL Server and other MySQL Programs including the mysql client,mysqladmin, mysqldump, and so on. See the MySQL Reference

Manual for documentation of the products.

although a simple console prompt acquired through bash from the host would be sufficient.

Rather than Connecting to MySQL Server from within the Container or Container Shell Access

I'm looking for access from the host.


from the container:

mysql> 
mysql>SELECT selecthost, user,host fromFROM mysql.user;
+-----------+-------+-----------+
| userhost      | user      | host      |
+-----------+-------+-----------+
| localhost | healthchecker    |
| localhost |
| mysql.infoschema |
| localhost |
| mysql.session    |
| localhost |
| mysql.sys        |
| localhost |
| root             | localhost |
+-----------+-------+-----------+
5 rows in set (0.00 sec)

mysql> 

and:

bash-4.2# 
bash-4.2#  mysqld --verbose --help | grep bind-address
  --bind-address=name IP address(es) to bind to. Syntax: address[,address]...,
  --mysqlx-bind-address[=name] 
bind-address                                                 *
mysqlx-bind-address                                          *
bash-4.2# 

I updated the privileges as:

mysql> 
mysql> CREATE USER 'user'@'%' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.4009 sec)

mysql> 
mysql> GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.2113 sec)

mysql> 
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.0605 sec)

mysql> 
mysql> SELECT host, user FROM mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | user             |
| localhost | healthchecker    |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
6 rows in set (0.00 sec)

mysql> 
mysql> exit
Bye
bash-4.2# 
bash-4.2# mysql -u user -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> exit
Bye
bash-4.2#   
bash-4.2# exit
exit
root $ 
root $ mysql -u user -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
root $ 

but clearly not.

So far as I can tell this default container works fine:

root $ 
root $ docker container list
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                  PORTS                               NAMES
7b119ef62a85        mysql/mysql-server:latest   "/entrypoint.sh mysq…"   17 hours ago        Up 17 hours (healthy)   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql
root $ 
root $ mysql -h127.0.0.1:33060 -p -uroot
Enter password: 
ERROR 2005 (HY000): Unknown MySQL server host '127.0.0.1:33060' (22)
root $ 
root $ mysql -h127.0.0.1 -p -uroot
Enter password: 
ERROR 1130 (HY000): Host '172.17.0.1' is not allowed to connect to this MySQL server
root $ 
root $ docker exec -ti --user root mysql bash
bash-4.2# 
bash-4.2# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2021
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> exit
Bye
bash-4.2# 
bash-4.2# exit
exit
root $ 

How do I connect to the database directly from the host? Using:

Products Included in the Container

A number of MySQL products are included in the Docker container you created with the MySQL Server Docker image:

MySQL Server and other MySQL Programs including the mysql client,mysqladmin, mysqldump, and so on. See the MySQL Reference

Manual for documentation of the products.

although a simple console prompt acquired through bash from the host would be sufficient.

Rather than Connecting to MySQL Server from within the Container or Container Shell Access

I'm looking for access from the host.


from the container:

mysql> 
mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| healthchecker    | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

mysql> 

and:

bash-4.2# 
bash-4.2#  mysqld --verbose --help | grep bind-address
  --bind-address=name IP address(es) to bind to. Syntax: address[,address]...,
  --mysqlx-bind-address[=name] 
bind-address                                                 *
mysqlx-bind-address                                          *
bash-4.2# 

I updated the privileges as:

mysql> 
mysql> CREATE USER 'user'@'%' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.40 sec)

mysql> 
mysql> GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.21 sec)

mysql> 
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.06 sec)

mysql> 
mysql> SELECT host, user FROM mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | user             |
| localhost | healthchecker    |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
6 rows in set (0.00 sec)

mysql> 
mysql> exit
Bye
bash-4.2# 

I thought I had this working a few minutes ago:

root $ 
root $ docker container ls
CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                   PORTS                               NAMES
9eb4a18def01        mysql/mysql-server:latest   "/entrypoint.sh mysq…"   6 minutes ago       Up 6 minutes (healthy)   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql
root $ 
root $ mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
root $ 
root $ docker exec -ti --user root mysql bash
bash-4.2# 
bash-4.2# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> SELECT host, user FROM mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | healthchecker    |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
5 rows in set (0.00 sec)

mysql> 
mysql> CREATE USER 'user'@'%' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.09 sec)

mysql> 
mysql> GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.13 sec)

mysql> 
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.05 sec)

mysql> 
mysql> SELECT host, user FROM mysql.user;
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | user             |
| localhost | healthchecker    |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
6 rows in set (0.00 sec)

mysql> 
mysql> exit
Bye
bash-4.2# 
bash-4.2# mysql -u user -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> exit
Bye
bash-4.2#   
bash-4.2# exit
exit
root $ 
root $ mysql -u user -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
root $ 

but clearly not.

added 1023 characters in body
Source Link
Loading
added 361 characters in body
Source Link
Loading
added 498 characters in body
Source Link
Loading
Source Link
Loading