4

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!!!

4
  • 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: a2hosting.co.uk/kb/developer-corner/mysql/… Commented Dec 4, 2019 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... Commented Dec 4, 2019 at 13:04
  • @brunodesthuilliers Thank You! for your kind suggestion. I have edited error part in my question. Pls check now. Commented Dec 5, 2019 at 3: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 Commented Dec 5, 2019 at 3:20

3 Answers 3

11

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

Sign up to request clarification or add additional context in comments.

2 Comments

This helped me, but I wonder why this was needed
this dont help me.
1

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)

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.