PyMySQL / mysqlclient

MySQL/MariaDB connector for Python
https://mysqlclient.readthedocs.io/
GNU General Public License v2.0
2.44k stars 433 forks source link

port parameter of Connect factory function is ignored #275

Closed healiseu closed 5 years ago

healiseu commented 5 years ago

Hi, I installed the latest version of mysqlclient on my Ubuntu 16.04 machine pip install mysqlclient -U Successfully installed mysqlclient-1.3.13 and I have installed sudo apt-get install python3-dev

Now I am trying to connect to mysql compatible server that is bound on port 3307 not the default port 3306

from MySQLdb import connect
conn = connect(host='localhost', user='root', passwd='123', db='employees', port=3307)
sql = conn.cursor()
sql.execute('show tables')

I experimented with various port numbers and I noticed that MySQLdb always connects at port=3306 and consequently in my example the cursor returns result from tables that exist on another MySQL server that is running on 3306

Is that a bug, why port parameter is ignored, or is it something wrong with my connect command ?

methane commented 5 years ago

I can connect to Port other than 3306. Unless you show reproducible examples, it is likely bug in your code.

healiseu commented 5 years ago

OK let me try to explain the problem in some more detail, for example

from MySQLdb import connect
conn = connect(host='localhost', user='root', passwd='123', db='DemoDB', port=3307)
Traceback (most recent call last):
  File "/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2961, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-3-1a8e79805234>", line 1, in <module>
    conn = connect(host='localhost', user='root', passwd='123', db='DemoDB', port=3307)
  File "/lib/python3.6/site-packages/MySQLdb/__init__.py", line 85, in Connect
    return Connection(*args, **kwargs)
  File "/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1049, "Unknown database 'DemoDB'")

DemoDB is a database that exists in MemSQL server that is running on port 3307. I can connect with their python memsql client without any problem. I traced their connection routine and they use conn = _mysql.connect(**self._db_args) <module '_mysql' from '/lib/python3.6/site-packages/_mysql.cpython-36m-x86_64-linux-gnu.so'> I think that module is related to your package version_info: <class 'tuple'>: (1, 3, 13, 'final', 0)

from memsql.common import database
mql = database.connect(host='127.0.0.1', user='root', password='123', port=3307, database='DemoDB')

I have run the debugger. Connection.__init__ method is called the kwargs passed are {'host': 'localhost', 'user': 'root', 'passwd': '123', 'db': 'DemoDB', 'port': 3307} A copy kwargs2 is created normally, client version (5,7) then it tries to return super(Connection, self).__init__(*args, **kwargs2) and it raises the exception above I don't know what to try next, any suggestions ?

methane commented 5 years ago

Huh!

host='localhost' vs host='127.0.0.1'

It's well known pitfall of MySQL.

I don't know what to try next, any suggestions ?

methane commented 5 years ago

You can try mysql -uroot -p -h localhost -P 3307 and mysql -uroot -p -h 127.0.0.1 -P 3307 too.

healiseu commented 5 years ago

@methane thank you for the help. Indeed, it is a problem with localhost vs 127.0.0.1 their client memsql raises the same exception when you try to pass localhost. As I read in the post It seems localhost OS internal alias is not working as it is expected in the case of MySQL.