简体   繁体   English

如何从Docker连接到主机上的mysql?

[英]How to connect to mysql on host from docker?

I have mysql on my localhost and I am able to log in with root: 我在本地主机上有mysql,并且能够使用root登录:

[root@pocnnr1n1 etc]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+

Now, I have a docker on this host which has ip address of '172.17.0.2' 现在,我在此主机上有一个docker,其ip地址为“ 172.17.0.2”

There is no problem ping from docker to host: 从docker到主机ping是没有问题的:

 root@eaa90c1059f2:/app/airflow/dags# ping 192.168.211.251 PING 192.168.211.251 (192.168.211.251): 56 data bytes 64 bytes from 192.168.211.251: icmp_seq=0 ttl=64 time=0.208 ms 

From docker, if I manually run pymysql to create a connection: 从泊坞窗,如果我手动运行pymysql以创建连接:

conn= pymysql.connect(host='192.168.211.251', port=3306, user='root',
passwd='root', db='airflow')

I have the following error: 我有以下错误:

pymysql.err.OperationalError: (1045, "Access denied for user 'root'@'172.17.0.2' (using password: YES)") pymysql.err.OperationalError:(1045,“用户'root'@'172.17.0.2'的访问被拒绝(使用密码:是)”)

If I change the ip address to '172.17.0.2' as below: 如果我将IP地址更改为“ 172.17.0.2”,如下所示:

conn= pymysql.connect(host='172.17.0.2', port=3306, user='root',
passwd='root', db='airflow')
I have the following error:

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '172.17.0.2' ([Errno 111] Connection refused)") pymysql.err.OperationalError:(2003年,“无法连接到'172.17.0.2'上的MySQL服务器([Errno 111]连接被拒绝)”)

Update: my.cnf is as below: 更新:my.cnf如下:

[mysqld]
transaction-isolation = READ-COMMITTED
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links = 0

key_buffer = 16M
key_buffer_size = 32M
max_allowed_packet = 32M
thread_stack = 256K
thread_cache_size = 64
query_cache_limit = 8M
query_cache_size = 64M
query_cache_type = 1

max_connections = 550
#expire_logs_days = 10
#max_binlog_size = 100M

#log_bin should be on a disk with enough free space. Replace '/var/lib/mysql/mysql_binary_log' with an appropriate path for your system
#and chown the specified folder to the mysql user.
log_bin=/var/lib/mysql/mysql_binary_log
#explicit_defaults_for_timestamp = 1

binlog_format = mixed

read_buffer_size = 2M
read_rnd_buffer_size = 16M
sort_buffer_size = 8M
join_buffer_size = 8M

# InnoDB settings
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit  = 2
innodb_log_buffer_size = 64M
innodb_buffer_pool_size = 4G
innodb_thread_concurrency = 8
innodb_flush_method = O_DIRECT
innodb_log_file_size = 512M

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

This is from the mysql-doc 这是从mysql-doc

Make sure that the server has not been configured to ignore network connections or (if you are attempting to connect remotely) that it has not been configured to listen only locally on its network interfaces. 确保未将服务器配置为忽略网络连接,或者(如果您尝试远程连接)未配置为仅在其网络接口上本地侦听。 If the server was started with --skip-networking, it will not accept TCP/IP connections at all. 如果服务器以--skip-networking启动,则它将完全不接受TCP / IP连接。 If the server was started with --bind-address=127.0.0.1, it will listen for TCP/IP connections only locally on the loopback interface and will not accept remote connections. 如果服务器以--bind-address = 127.0.0.1启动,它将仅在环回接口上本地侦听TCP / IP连接,并且不接受远程连接。

When something from the docker network try to connect the localhost: it's also trying to access the mysql from remote. 当docker网络中的某项尝试连接localhost时:它还试图从远程访问mysql。

Find the my.cnf (usually /etc/mysql/my.cnf ) comment the line bind-address=127.0.0.1 找到my.cnf (通常为my.cnf ),在该行上/etc/mysql/my.cnf bind-address=127.0.0.1

I just sorted out as below: 我只是整理如下:

This issue is about the right privilege was not granted to the user airflow@172.17.0.2 此问题与未授予用户airflow@172.17.0.2的权限有关

What I need to do is on the host's mysql, 我需要做的是在主机的mysql上,

GRANT ALL TO 'airflow'@'172.17.0.2' IDENTIFIED BY 'airflow' ;
FLUSH PRIVILEGES;

This is very clear if you do: select User, Host, Password from mysql.user; 如果您这样做,将非常清楚: select User, Host, Password from mysql.user;

Now the connection can be created using pymysql. 现在可以使用pymysql创建连接。

For the convenience of testing, one does not have to test it within airflow dag, this can be tested using python only (but with pymysql imported) 为了方便测试,您不必在气流测试中进行测试,只能使用python进行测试(但必须导入pymysql)

Hope this help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM