Connect to mysql server in a docker container from another docker container - python

In a nutshell
Using Dockers Desktop Community Version 2.0.0.3 (31259) in macOS Mojave I have two containers:
Alpine
MySQL
From the Alpine docker I am not be able to access the mysql server instance that is running on MySQL docker.
In details
On Alpine I am running python2 and python package mysqlclient. This is the configuration file that I use in my python application to access the mysql databases:
dbtype MySQLdb
connect-args {"user":"abe","db":"abe","passwd":"123"}
upgrade
host docker-mysql
port 32779
When I launch python application I see that it is unable to connect to mysqld server, in fact I get following error log:
python -m Abe.abe --config abe-my.conf
Reconnecting after rollback error: (2002, "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)")
Reconnecting to database.
Reconnecting after rollback error: (2002, "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)")
Reconnecting to database.
Reconnecting after rollback error: (2002, "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)")
Reconnecting to database.
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/bitcoin-abe/Abe/abe.py", line 2123, in <module>
sys.exit(main(sys.argv[1:]))
File "/bitcoin-abe/Abe/abe.py", line 2117, in main
store = make_store(args)
File "/bitcoin-abe/Abe/abe.py", line 156, in make_store
store = DataStore.new(args)
File "Abe/DataStore.py", line 3427, in new
return DataStore(args)
File "Abe/DataStore.py", line 191, in __init__
store.initialize()
File "Abe/DataStore.py", line 637, in initialize
store.configure()
File "Abe/DataStore.py", line 944, in configure
config = store._sql.configure()
File "Abe/SqlAbstraction.py", line 643, in configure
sql.configure_ddl_implicit_commit()
File "Abe/SqlAbstraction.py", line 734, in configure_ddl_implicit_commit
if sql._test_ddl():
File "Abe/SqlAbstraction.py", line 741, in _test_ddl
sql.drop_table_if_exists("%stest_1" % sql.prefix)
File "Abe/SqlAbstraction.py", line 712, in drop_table_if_exists
sql._drop_if_exists("TABLE", obj)
File "Abe/SqlAbstraction.py", line 709, in _drop_if_exists
sql.rollback()
File "Abe/SqlAbstraction.py", line 632, in rollback
sql.reconnect()
File "Abe/SqlAbstraction.py", line 305, in reconnect
return sql.conn()
File "Abe/SqlAbstraction.py", line 288, in conn
sql._conn = sql.connect()
File "Abe/SqlAbstraction.py", line 249, in connect
conn = sql._connect(cargs)
File "Abe/SqlAbstraction.py", line 281, in _connect
return sql.module.connect(**cargs)
File "/usr/lib/python2.7/site-packages/MySQLdb/__init__.py", line 84, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/MySQLdb/connections.py", line 164, in __init__
super(Connection, self).__init__(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)")
How it could be "fixed"?
I saw that the access file is /run/mysqld/mysqld.sock and I found it in the mysql docker, I suppose it should not be a problem to be able to connect the two dockers so that they "see each other", but at the moment I have no idea.

Related

connection refused while using firebirdsql to connect localhost fdb file

I'm trying to read fdb files in Ubuntu using firebirdsql:
conn = firebirdsql.connect(host='localhost', database='db.fdb',
user='sysdba', password='masterkey', charset='utf8')
But while connecting, it just said Connection refused.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "~/.local/lib/python3.6/site-packages/firebirdsql/__init__.py", line 94, in connect
conn = Connection(**kwargs)
File "~/.local/lib/python3.6/site-packages/firebirdsql/fbcore.py", line 595, in __init__
self.sock = SocketStream(self.hostname, self.port, self.timeout, cloexec)
File "~/.local/lib/python3.6/site-packages/firebirdsql/socketstream.py", line 46, in __init__
self._sock = socket.create_connection((host, port), timeout)
File "/usr/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/usr/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
the code works well on Windows Server, but on Ubuntu it can't establish the connection, how can I solve it?
In the comments you indicate you don't have a Firebird server installed on your machine. As there is no server listening on localhost port 3050, you get the connection refused error.
That firebirdsql, the driver you're using, is pure Python doesn't mean it can open database files by itself, it means it doesn't use a native library to establish a connection, but that it implements the Firebird TCP/IP wire protocol in Python. You will need a Firebird server (either localhost or remotely) to connect to.
Also: a server normally won't be able to access files in your home directory.

Connecting Python to MySql database on LAN Ubuntu server via Terminal on Mac not working

I'm trying to connect Python from a Mac to a MySql database that is running on a Raspberry Pi 4 with Ubuntu Server LTS 20.04 installed where both machines are on my LAN. Currently I have MySql Workbench on the Mac and am able to access the database via a Standard TCP/IP connection using SSH. No problem accessing the server via the Terminal over SSH or logging into the database in question.
I'm running Python 3.8.5 on the Mac and the latest version of MySQL Server on the Ubuntu Server installed on the RPi4.
The issue comes up when I write the connection code in a Python file, and attempt to run it via the Terminal. Here is the error I am getting (spaces are mine):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 216, in _open_connection
self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: Access denied for user '***'#'localhost' (using password: YES)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "testdb1PYTHON.py", line 3, in <module>
mydb = mysql.connector.connect(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/__init__.py", line 264, in connect
return CMySQLConnection(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 80, in __init__
self.connect(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/abstracts.py", line 966, in connect
self._open_connection()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 218, in _open_connection
raise errors.get_mysql_exception(msg=exc.msg, errno=exc.errno,
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user```

I cant migrate and run my server successfully in django

I am getting an error whenever i want to migrate and run my server in Django.
File "C:\Program Files\Python37\lib\site-packages\django\db\backends\base\base.py", line 195, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Program Files\Python37\lib\site-packages\django\db\backends\mysql\base.py", line 227, in get_new_connection
return Database.connect(**conn_params)
Are you using the default database?
Here are the django docs for setting up the database
docs

InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (13 Permission denied)

I'm trying to host an application on apache2 server using python CGI framework. The program works fine when compiled and there is no error.
When I try it on the web browser I get the error.
InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (13 Permission denied)
I have tried installing mysql-connector-python and checked if I'm missing anything. Everything seems to be fine. Below is the code with the error in detail.
def connectdb():
mydb = mx.connect(host='localhost',user='******',passwd='********',database='searchdb')
cur=mydb.cursor()
return mydb,cur
Error when trying to access the program.
Traceback (most recent call last):
File "/var/www/html/ftest.py", line 116, in <module>
mydb,cur=connectdb()
File "/var/www/html/ftest.py", line 55, in connectdb
mx.connect(host='localhost',user='*****',passwd='********',database='searchdb')
File "/usr/lib/python2.7/site-packages/mysql/connector/__init__.py", line 98, in connect
return MySQLConnection(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/mysql/connector/connection.py", line 118, in __init__
self.connect(**kwargs)
File "/usr/lib/python2.7/site-packages/mysql/connector/connection.py", line 382, in connect
self._open_connection()
File "/usr/lib/python2.7/site-packages/mysql/connector/connection.py", line 345, in _open_connection
self._socket_open_connection()
File "/usr/lib/python2.7/site-packages/mysql/connector/network.py", line 386, in _open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (13 Permission denied)
This is the error that I'm facing. Anyone familiar with this issue, Please let me know.
Thank you.
Some things to check, assuming this is a Linux machine:
Make sure you can connect locally using the same user name and password.
mysql -u [youruser] -p
If SELinux is running, make sure the "httpd_can_network_connect_db" boolean is set to on. Check with sudo getsebool httpd_can_network_connect_db. If it comes back as "off," set it to "on" with sudo setsebool -P httpd_can_network_connect_db on. (Make sure to include the -P, or it will revert back to the original value if the system restarts.)

Start HTTP/HTTPS server, python -m SimpleHTTPServer

I am trying to start HTTPS server on my localhost, but getting below error. I have opened cmd as an administrator.
C:\Windows\system32>python -m SimpleHTTPServer
Traceback (most recent call last):
File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Python27\lib\SimpleHTTPServer.py", line 224, in <module>
test()
File "C:\Python27\lib\SimpleHTTPServer.py", line 220, in test
BaseHTTPServer.test(HandlerClass, ServerClass)
File "C:\Python27\lib\BaseHTTPServer.py", line 595, in test
httpd = ServerClass(server_address, HandlerClass)
File "C:\Python27\lib\SocketServer.py", line 419, in __init__
self.server_bind()
File "C:\Python27\lib\BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)
File "C:\Python27\lib\SocketServer.py", line 430, in server_bind
self.socket.bind(self.server_address)
File "C:\Python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10013] An attempt was made to access a socket in a way forb
idden by its access permissions
Your windows firewall may block python from listening.
Additionaly, try adding a different port number higher than 1024 to the command line and see if it helps.
I was not able to start since some service is already running on 8000,
I was able to successfully start the service on another port 2000
C:\>python -m SimpleHTTPServer 2000
Serving HTTP on 0.0.0.0 port 2000 ..
One can get the PID/status for port 8000 by netstat -ano
C:\>netstat -ano
Active Connections
Proto Local Address Foreign Address State PID
TCP 0.0.0.0:912 0.0.0.0:0 LISTENING 2544
TCP 0.0.0.0:8000 0.0.0.0:0 LISTENING 4
TCP 0.0.0.0:19781 0.0.0.0:0 LISTENING 3100
shows PID 4 which is a system process.

Categories