Python Forum
mysql db connection using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mysql db connection using python
#1
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'
Reply
#2
There is an indention error starting from line 21. 'try' should be at the same level as 'logging' at line 20.
Reply
#3
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"
Reply
#4
You have to import MySQLdb and you may have to install it first.
Reply
#5
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
Reply
#6
(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
Reply
#7
Thank you for your response.Let me check

How can I connect the existing mysql database
Reply
#8
You can get more information about the error by using
logging.exception('Could not connect to database')
at line 25 in the original post.
Reply
#9
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
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I don't know what is wrong (Python and SQL connection) shereen 1 114 Yesterday, 01:03 PM
Last Post: deanhystad
  No Internet connection when running a Python script basil_555 8 441 Mar-11-2024, 11:02 AM
Last Post: snippsat
  Connection LTspice-Python with PyLTSpice bartel90 0 280 Feb-05-2024, 11:46 AM
Last Post: bartel90
  Virtual Env changing mysql connection string in python Fredesetes 0 320 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  connection python and SQL dawid294 4 580 Dec-12-2023, 08:22 AM
Last Post: Pedroski55
  Mysql and mysql.connector error lostintime 2 607 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Networking Issues - Python GUI client and server connection always freezes Veritas_Vos_Liberabit24 0 679 Mar-21-2023, 03:18 AM
Last Post: Veritas_Vos_Liberabit24
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,672 Feb-09-2022, 09:55 AM
Last Post: ibreeden
Question Debian 11 Bullseye | Python 3.9.x | pip install mysql-connector-python-rf problems BrandonKastning 4 6,567 Feb-05-2022, 08:25 PM
Last Post: BrandonKastning
  Python MYSQL connection does not work after 1h idle zazas321 9 6,616 Oct-07-2021, 12:02 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020