#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    35
    Rep Power
    2

    Problems connecting to remote MySQL database via Python script


    Hello all,

    I am trying to connect to a remote Ubuntu server running MySQL from a Python script but I keep on getting the error "OperationalError: (1130, "Host '192.168.1.2' is not allowed to connect to this MySQL server")"

    This is the test script I'm running:
    Code:
    import MySQLdb
    
    db = MySQLdb.connect(
    	host = '192.168.0.2',
    	user = 'james',
    	passwd = 'toor',
    	db = 'test'
    	)
    
    cursorCardNumber = db.cursor()
    cursorCardNumber.execute('SELECT CardNumber FROM testv1')
    resultCardNumber = cursorCardNumber.fetchmany()
    resultStringCard = str(resultCardNumber)
    
    print ("This is the card number:\n")
    for i in resultStringCard:
    	try:
    		CardInt(i[0])
    		print CardInt
    	except: pass
    
    print "\n\n"
    On my server, I have created a new user for this remote machine using the command:

    Code:
    create user 'james'@'winxp' identified by "toor";
    grant all privileges on *.* to 'james'@'winxp';
    I think the problem may stem from the 'james'@'winxp' part of the command. I'm using Windows XP with the default administrator login, not connected to a domain with the computer name winxp.

    If anyone can steer me in the correct direction, it will be greatly appreciated.
  2. #2
  3. No Profile Picture
    Problem Solver
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2001
    Location
    Stockholm, Sweden
    Posts
    4,495
    Rep Power
    534
    Correct, the host name in this command refers to the ip/host name that you _come_from_ and that the MySQL server will see when it performs a _reverse_DNS_lookup_ on the ip-address that your client has (in your case 192.168.1.2) and that name is most probably not winxp.

    Unless you are sure that you have DNS properly setup I recommend using the IP-address instead and specially together with a wildcard if your client happen to get another address from the DHCP sometimes.

    So in your case I suggest:
    Code:
    create user 'james'@'192.168.1.%' identified by "toor";
    /Stefan
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    35
    Rep Power
    2
    Originally Posted by sr
    Correct, the host name in this command refers to the ip/host name that you _come_from_ and that the MySQL server will see when it performs a _reverse_DNS_lookup_ on the ip-address that your client has (in your case 192.168.1.2) and that name is most probably not winxp.

    Unless you are sure that you have DNS properly setup I recommend using the IP-address instead and specially together with a wildcard if your client happen to get another address from the DHCP sometimes.

    So in your case I suggest:
    Code:
    create user 'james'@'192.168.1.%' identified by "toor";
    Thank you for your reply sr. I had a feeling this was completely incorrect and did a bit of research this morning and found that the IP address would be the best option. Naively, I forgot that DNS isn't set up on the network, but that's how your mind works when you've been working on it for ten hours!

IMN logo majestic logo threadwatch logo seochat tools logo