Python Forum

Full Version: mysql db connection using python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi,

I am new to python.

I have an ejabberd chat server. I have an external authentication python file to connect to mysql db.

On adding the below code and restarting the chat server, the external authentication script is failing. CAN ANYONE HELP PLEASE...

########################################################################
#DB Settings
#Just put your settings here.
########################################################################
db_name="test"
db_user="root"
db_pass=""
db_host="localhost"
db_table="cc_customer_profiles"
########################################################################
#Setup
########################################################################
import sys, logging, struct, hashlib
from struct import *
sys.stderr = open('/opt/ejabberd/extauth_err.log', 'a')
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(levelname)s %(message)s',
                    filename='/opt/ejabberd/extauth.log',
                    filemode='a')
logging.info("connect to db")
 try:
       database=MySQLdb.connect(db_host, db_user, db_pass, db_name)
       logging.info("success db")
 except:
       logging.info("Unable to initialize database, check settings!")
 dbcur=database.cursor()
Error:
[critical] <0.612.0>@extauth:loop:137 extauth script has exitted abruptly with reason 'normal'
There is an indention error starting from line 21. 'try' should be at the same level as 'logging' at line 20.
let me check

Thanks a lot but the database is not connected.

getting the error message:
Error:
Unable to initialize database, check settings!
are my settings correct ?
db_name="test"
db_user="root"
db_pass=""
db_host="localhost"
db_table="cc_customer_profiles"
You have to import MySQLdb and you may have to install it first.
I already have mysql in the system.

[inline]root@HR01:/# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2135
Server version: 5.7.21-0ubuntu0.16.04.1-log (Ubuntu)

Copyright © 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
[/inline]

Should I install connector for python? If so, in which path should i install?

Please help
(Apr-20-2018, 07:08 AM)sunstar20 Wrote: [ -> ]Should I install connector for python? If so, in which path should i install?
You install with pip,then it's goes in right place.

Recommend PyMySQL Pure Python MySQL Driver.
pip install PyMySQL

mysqlclient is a fork of MySQL-python. It adds Python 3 support and fixed many bugs.
pip install mysqlclient
Thank you for your response.Let me check

How can I connect the existing mysql database
You can get more information about the error by using
logging.exception('Could not connect to database')
at line 25 in the original post.
On adding logging.exception, the below error message is shown:

Error:
2018-04-21 04:35:58,314 ERROR Unable to initialize database, check settings! Traceback (most recent call last): File "/opt/ejabberd/conf/myauth.py", line 34, in <module> database=MySQLdb.connect(db_host, db_user, db_pass, db_name) NameError: name 'MySQLdb' is not defined

On checking python command prompt for 'import MySQLdb', the below error message is shown.

>>> import MySQLdb
Error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named MySQLdb
I have installed the deb connector package - mysql-connector-python_8.0.11-1ubuntu16.04_all.deb via shell command:

dpkg -i mysql-connector-python_8.0.11-1ubuntu16.04_all.deb
I am not sure if the installation is successful. PLEASE HELP
You have to install either python3-mysqldb or python3-pymysql assuming you are using python3.x Further installation problems should go to the Debian forum as this is not a Python problem.
Pages: 1 2