How to connect to mysql on host from docker? - mysql

I have mysql on my localhost and I am able to log in with 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'
There is no problem ping from docker to host:
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:
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)")
If I change the ip address to '172.17.0.2' as below:
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)")
Update:
my.cnf is as below:
[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
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. 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.
When something from the docker network try to connect the localhost: it's also trying to access the mysql from remote.
Find the my.cnf(usually /etc/mysql/my.cnf) comment the line 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
What I need to do is on the host's 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;
Now the connection can be created using 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)
Hope this help.

Related

MySQL Remote Connection Issues

I've just learnt the basics of MySQL to allow me to upload tables onto my Wordpress site through wpdatatables. However, I can't for the life of me work out how to get a remote access connection working. There is plenty of literature and forum posts out there to help with this and I've tried a number of way but I haven't had any success.
I've created privileges for a user called morts81 using the code below:
GRANT ALL PRIVILEGES ON *.* TO 'morts81'#'%' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Then I've opened up the my.cnf file to ensure that the bind.address line is removed, however, I don't have a bind.address line so I imagine that isn't an issue.
# The MySQL server
[mysqld]
user = mysql
port=3306
socket = /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
skip-external-locking
key_buffer = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
Then finally I try and check the connection from inside the wpdatatable plug-in for Wordpress and I get the following error:
I've also tried replacing localhost in the wpdatatables input with 127.0.0.1 which seems to be recommended on some forums but I get a separate error then:
Would appreciate any help on what I'm doing wrong here.
The socket you specified is different in your config file to start the mysql server. If you can connect to the socket as shown in first error message, use the below socket configuration
socket = /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
For the 2nd error, see if your mysql server is accessible from the server/service datatables is running on.

Yii2 Remote Database Connection Issue

I am trying to get my web server to connect to my remote database (Mysql).
The connection is happening in my web application which is built on yii2.
I have created all user details, GRANTED all privileges and allowed connections from ALL hosts (%) (Progressively in troubleshooting this).
Now I can connect from home using HeidiSQL or using Mysql Workbench without issue.
I have even created a blank yii2 site at home and it works instantly, but uploading the site to the web server instantly gives the same issue.
Database Exception – yii\db\Exception
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '46.105.111.111' (110)
↵
Caused by: PDOException
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '46.105.111.111' (110)
in /home/xxxxxxx/xtest.xxxxxxxx.tv/vendor/yiisoft/yii2/db/Connection.php at line 602
I have run netstat on the db server:
I have been through the etc/mysql/my.cnf
<pre>[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 7999
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
#bind-address = *
key_buffer = 16M
max_allowed_packet = 64M
thread_stack = 192K
thread_cache_size = 8
myisam-recover = BACKUP
query_cache_limit = 1M
query_cache_size = 16M
expire_logs_days = 10
max_binlog_size = 100M
max_connections = 65535
open-files-limit = 65535
max_connect_errors = 65535
innodb_buffer_pool_size = 1G
table_open_cache = 1000
innodb_flush_log_at_trx_commit = 0
innodb_flush_method = O_DIRECT
tmp_table_size = 256M
max_heap_table_size = 256M
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
[mysql]
[isamchk]
key_buffer = 16M</pre>
I have looked at the iptables:
I am totally bedazzled as having read about 20 pieces of information about mysql security, i get the feeling its a permissions issue.
I have even tried using my own PDO Wrapper, so i a, 99% sure its not a Yii2 issue, would really appreciate any help.
Hopefully I have provided enough info as I got a downvote previously.
many thanks for the help.
It turns out that the web host was actually blocking the outgoing port for the connection.
The output error from yii being database error, nievly led me to think the issue was at the database end.
Simple logic when you read back the question.
I would say to anyone that finds this question, remember with a connection issue that communication is two way, check both ends of the connection.

Connect remotely to mysql installed on OSX machine

I search the web for a 2 hours now trying to connect from my MacBook to mysql installed using brew on iMac.
I can login locally from the iMac on mysql:
From iMac terminal:
mac-server:5.6.26 mhewedy$ ifconfig | grep netmask
inet 127.0.0.1 netmask 0xff000000
inet 192.168.1.2 netmask 0xffffff00 broadcast 192.168.1.255
Mysql:
mysql> show grants for 'root';
+--------------------------------------------------------------------------------------------------------------------------------+
| Grants for root#% |
+--------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY PASSWORD '....' WITH GRANT OPTION
And my.cnf file
cat /usr/local/Cellar/mysql/5.6.26/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
bind-address = 0.0.0.0
# skip-networking
I am trying to connect from MysqlWorkbench from the macbook:

Access Wamp server's MySQL DB 5.5.24 on local network

I would like to access to my MySQL DB through local network which is installed by Wamp(2.2e) server.
I managed to access phpMyAdmin from other local computer so I can view the content of the DB, but I would like to use the data from it by other application.
I turned off my firewall just for testing on my "server" and my "client" machine as well.
I cannot telnet to the DB (telnet "local network IP of my server" 3306) from my client machine.
on my server netstat -an shows the MySQL DB:
Proto | Local Address | Foreign Address | State
TCP | 0.0.0.0:3306 | 0.0.0.0:0 | Listening
I have add users("client" local network ip, "server" local network ip) to the database with all of the permissions.
On the "server" machine I can telnet to its own MySQL DB by using its local network IP after I add it to the DB users table as well.
Here is my my.ini file without the comment lines:
[client]
port = 3306
socket = /tmp/mysql.sock
[wampmysqld]
port = 3306
socket = /tmp/mysql.sock
key_buffer = 16M
max_allowed_packet = 1M
table_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
basedir=c:/wamp/bin/mysql/mysql5.5.24
log-error=c:/wamp/logs/mysql.log
datadir=c:/wamp/bin/mysql/mysql5.5.24/data
skip-federated
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[isamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
[mysqld]
port=3306
I have tried a few things already which did not work:
skip-name-resolve at mysqld
bind-address=0.0.0.0 (it is the same as the default) at mysqld
bind-address="local network IP" at mysqld did not do any difference
I was rewriting other files as well, but I guess I need to do changes here(my.ini) to access to the database on local network.
I my wamp server is in offline mode just now, as I can access to phpMyAdmin I thought I should be able to access to the database with left it offline. I tried it in online mode as well, did not do any difference.
I spent quite a bit of a time solving the problem but unfortunately I have no more ideas. Every suggestions, ideas will be appreciated.
So I kind of Figured it out.
I am using Kaspersky endpoint security on my development machine. For some reason it is running weird on it (the restrictions managed my a central server, computers on the same group as me acting normal; responding for the rules as they should). It was not enough to disable my firewall I needed to disable whole kaspersky then I was able to telnet to the MySQL port from an other machine.
So if firewall rules added to "normal" machines (not my weird one), client and server as well; Wamps MySQL DB was accessible in local network without any other changes.
I know it is rather a special case but I hope it still could help for some one.

Can't connect to MySQL server on 'localhost' (10061) (mainly remote connections)

Good day,
firstly I'd like to sorry for my English.
after installing latest WAMP I have problem with access to my MySQL server from remote machines in my LAN. It looks like it refuse tcp connections (before adding bind-address telnet localhost 3306 refuse tcp connections).
My WAMP icon in tray is green, so all looks fine (even in my.ini after comparing to old version). My WAMPy is putted online.
Apache has Rewrite module enabled if it is important.
I have even firewall disabled. I use Free AVG (so I think it didn't block anything)
When my coworkers want to connect, they get:
mysql_connect(): [2002] trying to connect via tcp://my-pc:3306.
When I try to use Navicat Lite, I get "Can't connect to MySQL server on 'localhost' (10061)"
Previous all wamp services was disabled before installing new WAMPy.
At home everything works fine but here I have third day lost ;/
C:\Documents and Settings\Administrator\Pulpit>mysqladmin -uroot -p version
Enter password: *****
mysqladmin Ver 8.42 Distrib 5.5.8, for Win32 on x86
Copyright (c) 2000, 2010, 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.
Server version 5.5.8-log
Protocol version 10
Connection localhost via TCP/IP
TCP port 3306
Uptime: 2 min 0 sec
Threads: 1 Questions: 1 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 26 Queries per second avg: 0.8
c:/wamp/bin/mysql/mysql5.5.8/my.ini content
[client]
port = 3306
socket = /tmp/mysql.sock
[wampmysqld]
port = 3306
socket = /tmp/mysql.sock
key_buffer = 16M
max_allowed_packet = 1M
table_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
basedir=c:/wamp/bin/mysql/mysql5.5.8
log-error=c:/wamp/logs/mysql.log
datadir=c:/wamp/bin/mysql/mysql5.5.8/data
bind-address=127.0.0.1 # after adding this line I can connect via Navicat
skip-federated
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[isamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
[mysqld]
port=3306
I have also new users created for my mysql (root#localhost, root#127.0.0.1, root#192.168.%.%, root#%).
Does anyone have any idea what can I change to make it work properly?
"I have also new users created for my mysql (root#localhost, root#127.0.0.1, root#192.168.%.%, root#%). Does anyone have any idea what can I change to make it work properly?"
I suggest that you create non-root users for other users.
Can you connect using the mysql command line shell on the server? mysql -uroot -ppassword -h127.0.0.1
Can you "ping my-pc" from another workstation?
Can you connect from another computer to the server using the ip address of the server. mysql -uroot -ppassword -h192.168.whatever your server IP is.