Python Forum
mysql db connection using python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: mysql db connection using python (/thread-9621.html)

Pages: 1 2


mysql db connection using python - sunstar20 - Apr-19-2018

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'



RE: mysql db connection using python - Gribouillis - Apr-19-2018

There is an indention error starting from line 21. 'try' should be at the same level as 'logging' at line 20.


RE: mysql db connection using python - sunstar20 - Apr-19-2018

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"



RE: mysql db connection using python - woooee - Apr-19-2018

You have to import MySQLdb and you may have to install it first.


RE: mysql db connection using python - sunstar20 - Apr-20-2018

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


RE: mysql db connection using python - snippsat - Apr-20-2018

(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


RE: mysql db connection using python - sunstar20 - Apr-20-2018

Thank you for your response.Let me check

How can I connect the existing mysql database


RE: mysql db connection using python - Gribouillis - Apr-20-2018

You can get more information about the error by using
logging.exception('Could not connect to database')
at line 25 in the original post.


RE: mysql db connection using python - sunstar20 - Apr-21-2018

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


RE: mysql db connection using python - woooee - Apr-21-2018

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.