3

I am learning a new library mysql in python. I have tried executing the below command,

import mysql.connector

mydb= mysql.connector.connect(
            host= 'localhost',
            user= 'root',
            passwd= 'skviknesh'
            )
print(mydb)

and got the below error!

Traceback (most recent call last):

  File "<ipython-input-1-f9b9bc67dd68>", line 3, in <module>
    mydb= mysql.connector.connect(

AttributeError: module 'mysql.connector' has no attribute 'connect'

I looked into similar other Stack overflow questions... Didn't get a solution. I tried renaming my file too, that didn't help. Please help on the same!!!

Viknesh S K
  • 101
  • 3
  • 9
  • You haven't specified a DB value in your connection string, also i think you'd need to use a query to get a useful value in print here. see: https://www.a2hosting.co.uk/kb/developer-corner/mysql/connecting-to-mysql-using-python – C-Sway Dec 04 '19 at 13:00
  • 1
    Your code snippet doesn't match your error. In the error snippet, you're trying to import "mysql.connector.connect", not "mysql.connector". May I kindly suggest you do the whole official Python tutorial ? It will certainly save you a lot of time, pain and frustrations... – bruno desthuilliers Dec 04 '19 at 13:04
  • @brunodesthuilliers Thank You! for your kind suggestion. I have edited error part in my question. Pls check now. – Viknesh S K Dec 05 '19 at 03:17
  • @C-Sway Thank You! I hope, this should work without DB value, as i am trying to create a DB and table i a new server connection from Python using Mysql library. Also, in the link you have shared, they have used `import MySQLdb` & i am trying it with `import mysql.connector` – Viknesh S K Dec 05 '19 at 03:20

3 Answers3

7

When I had the same problem. I had to just uninstall the package with pip uninstall mysql-connector-python and reinstall it with pip install mysql-connector-python and it worked :D

Ang
  • 71
  • 1
  • 3
2

This generally happens when you install the wrong package. Probably during installation, you have used the following pip command: pip install mysql-connector

But you are required to install the package: pip install mysql-connector-python

Like this:

C:\Users\tuhin Mitra>pip install mysql-connector-python
Requirement already satisfied: mysql-connector-python in c:\users\tuhin mitra\appdata\local\programs\python\python37-32\lib\site-packages (8.0.19)
Requirement already satisfied: protobuf==3.6.1 in c:\users\tuhin mitra\appdata\local\programs\python\python37-32\lib\site-packages (from mysql-connector-python) (3.6.1)
Requirement already satisfied: dnspython==1.16.0 in c:\users\tuhin mitra\appdata\local\programs\python\python37-32\lib\site-packages (from mysql-connector-python) (1.16.0)
Requirement already satisfied: setuptools in c:\users\tuhin mitra\appdata\local\programs\python\python37-32\lib\site-packages (from protobuf==3.6.1->mysql-connector-python) (45.1.0)
Requirement already satisfied: six>=1.9 in c:\users\tuhin mitra\appdata\roaming\python\python37\site-packages (from protobuf==3.6.1->mysql-connector-python) (1.12.0)
sujay simha
  • 99
  • 2
  • 7
Tuhin Mitra
  • 555
  • 7
  • 19
0

I was also facing the same issue while i am trying to connect to mysql from python, i have corrected below issue, you may have one of the issue below

  1. use approriate library installation
  2. use exact password type to connect with database(because by default mysql use caching_sha2_password). you have to pass the password type explisit.
Programmer
  • 35
  • 7