1

I'm trying to connect to a database on a Linux server, everything works fine with PHP using:

$mysqli= new mysqli($_DBHOST, $_DBUSER, $_DBPASS, $_DB);

I can also connect using the command line with:

mysql -u xxx -p

But when I try to connect with Python using pymysql I get the following error:

pymysql.err.InternalError: (1130, "Host 'xxx' is not allowed to connect to
this MariaDB server")

Python code:

import pymysql
conn = pymysql.connect(host="xxx",user='xxx', passwd='xxx', db = 'xxx',
                                   ,port=3306, autocommit=True)

How can this even be possible? It cannot be a permission issue since PHP can connect fine, or can it?

Januka samaranyake
  • 2,385
  • 1
  • 28
  • 50
Charles
  • 309
  • 3
  • 11

3 Answers3

1

Try this

    try:
        import mysql.connector
        mysql_connector = mysql.connector
    except:
        import pymysql
        mysql_connector = pymysql

    mysql_connector.connect(user='root', password='root', host='xxx:3306', database='test')
Januka samaranyake
  • 2,385
  • 1
  • 28
  • 50
0

the best you can do is use MySQLdb (case sensitive) it's the best package. and then you can directly import your table in pandas dataframe.

It's easy to do, but hard to remember the correct spelling:

pip install MySQL-python

Note: Some dependencies might have to be in place when running the above command. Some hints on how to install these on various platforms:

Ubuntu 14, Ubuntu 16, Debian 8.6 (jessie)

sudo apt-get install python-pip python-dev libmysqlclient-dev

Fedora 24:

sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc

Using MySQLdb

import MySQLdb
conn = MySQLdb.connect(host="xxx.xxx.xx.x", user="name", passwd="password", db="database_name")
cursor = db.cursor()
import pandas as pd

df = pd.read_sql("select * from your_table",conn)
Shubham R
  • 7,382
  • 18
  • 53
  • 119
0

Two guesses:

  1. Run mysqladmin variables | grep socket to get where the socket is located, and try setting up a connection like so:

    pymysql.connect(db='base', user='root', passwd='pwd', unix_socket="/tmp/mysql.sock")
    
  2. Run mysqladmin variables | grep port and verify that the port is 3306. If not, you can set the port manually like so:

    pymysql.connect(db='base', user='root', passwd='pwd', host='localhost', port=XXXX)
    
Shubham R
  • 7,382
  • 18
  • 53
  • 119
  • Is there another way to get the socket location, because I don't have permission to run mysqladmin variables – Charles Nov 25 '16 at 06:13
  • @Charles [check this](http://www.tecmint.com/mysqladmin-commands-for-database-administration-in-linux/) i too have no idea as for now – Shubham R Nov 25 '16 at 06:19
  • @Charles why don't you use MySQLdb instead, its the best – Shubham R Nov 25 '16 at 06:20
  • Yeah, I'm trying to download it, but there's plenty of dependencies and I have a very limited access to this server (can barely use pip), but I don't see why it would change something? – Charles Nov 25 '16 at 06:22
  • @Charles , because, you can directly store your result table from sql in pandas dataframe. and i personally like pandas. Let me know if you're facing problems in installation. And upvote the answer if you find it useful as it is a common practice here. – Shubham R Nov 25 '16 at 06:31