简体   繁体   中英

Not able to connect to mysql database using Jython with mysql.connector module

I am creating a script which will fetch the data from mysql server and decrypt the text using Jasypt which is a java library to encrpyt/decrypt text. So I have to do this using Jython .

I am able to import java class from Jasypt library. I am also able to import mysql.connector into Jython script. But script is not able to make connection to mysql server.

Here is the code which I am trying

import sys
SYSPATH = ['/usr/local/lib/python2.7/site-packages', '/data/packages/jasypt-1.9.2.jar']
[sys.path.append(p) for p in SYSPATH]
import mysql.connector

# adding this to avoid "AttributeError: socket.SOL_TCP does not exist" listed at https://github.com/pika/pika/issues/67
import socket
socket.SOL_TCP = socket.IPPROTO_TCP

# This line is giving error "TypeError: unpack_from(): 2nd arg can't be coerced to String"
con = mysql.connector.connect(user='username', password='password', host='192.168.123.123', database='users')

last line is gving me error

 File "2017-04-05-get-bank-sms.py", line 11, in createUserIdUuidMap
   con = mysql.connector.connect(user='username', password='password',    host='192.168.123.123', database='users')
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/__init__.py", line 179, in connect
   return MySQLConnection(*args, **kwargs)
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 95, in __init__
   self.connect(**kwargs)
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/abstracts.py", line 719, in connect
   self._open_connection()
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 207, in _open_connection
   self._do_handshake()
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 99, in _do_handshake
   packet = self._socket.recv()
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/network.py", line 235, in recv_plain
   payload_len = struct.unpack_from(
 File "/usr/local/lib/python2.7/site-packages/mysql/connector/network.py", line 235, in recv_plain
   payload_len = struct.unpack_from(
TypeError: unpack_from(): 2nd arg can't be coerced to String

Is it possible what I am trying?

Based on the other related questions it appears that the most common approach is to use "MySQL Connector/J" (the JDBC driver) instead of "MySQL Connector/Python". I can confirm that this Jython code works ...

from com.ziclix.python.sql import zxJDBC

connectionUrl = "jdbc:mysql://localhost:3307"
cnxn = zxJDBC.connect(
        connectionUrl,
        "root",
        "mypassword",
        "com.mysql.jdbc.Driver")
crsr = cnxn.cursor()
crsr.execute("SHOW DATABASES")
rows = crsr.fetchall()
print(rows)

... when I run it using the following batch file:

SET CLASSPATH=C:\Users\gord\.m2\repository\mysql\mysql-connector-java\5.1.41\mysql-connector-java-5.1.41.jar
C:\jython2.7.0\bin\jython mysql_test.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM