Why am I unable to connect to a mysql database using python and mysql.connector?

I am trying to connect to a mysql database, but having issues. I am trying to use mysql connector (not sure if this is the most efficient library to use), but I am having an error.

Here is my basic script:

import mysql.connector

def test_sql_query():
    db = mysql.connector.connect(host='hostname', database='db', user='dbuser', password='dbpass' port=000)
    cur = db.cursor()
    if db.is_connected():
        print('Connected to MySQL database')
    try:
        sql_command = "select * from test where test like '%FileUploadAgent%' and status='00' order by test desc;"
        cur.execute(sql_command)
        db.commit()
        rows = cur.fetchall()
        for row in rows:
            print "   ", row[1][1]
    except:
        print "did not work"
    db.close()

Error:

 File "sql_test.py", line 44, in <module>
    test_sql_query()
  File "sql_test.py", line 6, in sterling_agent_sql_query
    db = mysql.connector.connect("host='hostname.com' database='dbname' user='dbuser' password='dbpassword' port='000'")
  File "/Library/Python/2.7/site-packages/mysql/connector/__init__.py", line 179, in connect
    return MySQLConnection(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 57, in __init__
    super(MySQLConnection, self).__init__(*args, **kwargs)
TypeError: __init__() takes exactly 1 argument (2 given)

I am assuming that I am doing the connection wrong.

The parameter you are passing to .connect() is wrong. You are passing a string. Instead, each of those should be their own parameter.

db = mysql.connector.connect(host='hostname', 
    database='db', 
    user='dbuser', 
    password='dbpass', 
    port=000)

Additionally, port needs to be an integer, not a string. I also assume that the 000 you have is example data. If not, that's not valid.

5.1 Connecting to MySQL Using Connector/Python, Section 7.1, “Connector/Python Connection Arguments” describes the ER_BAD_DB_ERROR: print("Database does not exist") else: print(err) else: cnx.​close() This can be configured at runtime using the use_pure connection argument. Steps to connect MySQL database in Python using MySQL Connector Python Install MySQL Connector Python using pip. Use the mysql.connector.connect () method of MySQL Connector Python with required parameters to connect MySQL. Use the connection object returned by a connect () method to create a cursor object to perform Database Operations.

You have the format of the connect incorrect, putting all of the variables inside of quotes makes the method treat it as a single, string input to the method.

db = mysql.connector.connect("host='hostname' database='db' user='dbuser' password='dbpass' port='000'")

should be

db = mysql.connector.connect(
    host='hostname',
    database='db',
    user='dbuser',
    password='dbpass',
    port=0)

Note that your port value also should be a valid port (not the echo port) for the database.

7.1 Connector/Python Connection Arguments, A connection with the MySQL server can be established using either the mysql.​connector.connect() cnx = mysql.connector.connect(user='joe', database='test') cnx MySQL Connector/Python does not support the old, less-secure password​  Contents1 Using pure Python or C Extension2 Closing Connection3 Using Configuration Files To connect to the database we use the connect() function of the mysql.connector module. It accepts connection credentials and returns an object of type MySQLConnection or CMySQLConnection (if C extension is installed).

I got the same problem my solution was to uninstall MySQL connector.

python -m pip uninstall mysql-connector

And then reinstall it using:

python -m pip install mysql-connector

and it works for me.

MySQL :: MySQL Connector/Python Developer Guide :: 10.12.2 , Error is internally used by Connector/Python to raise MySQL client and server errors values=('/tmp/mysql.sock', 2))) "2002: Can't connect to local MySQL server import errorcode cnx = mysql.connector.connect(user='scott', database='​test')  Connecting to MySQL Database using MySQLConnection object. First, create a database configuration file named config.ini and define a section with four parameters as follows: [mysql] host = localhost database = python_mysql user = root password =SecurePass1!

Python MySQL Database Connectivity [Complete Guide], In this article, I will demonstrate how to perform MySQL database connectivity in Note: We are using MySQL Connector Python to connect MySQL. Once we close the cursor object, we can not execute any SQL statement. Connecting to MySQL using Python. Before you can access MySQL databases using Python, you must install one (or more) of the following packages in a virtual environment: MySQL-python: This package contains the MySQLdb module, which is written in C. It is one of the most commonly used Python packages for MySQL.

How to connect to MySQL using Python, Before you can access MySQL databases using Python, you must install one (or contains the mysql.connector module, which is written entirely in Python. three MySQL modules use the portable SQL database API interface, they are able to  pip install mysql-connector-python this may additionally give an error like this: Some externally hosted files were ignored (use --allow-external mysql-connector-python to allow).

Python MySQL – Connect to a MySQL Database in , In this tutorial, you will learn how to connect to MySQL databases from Python using 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be from mysql.connector import MySQLConnection, Error from  Connector/Python offers two implementations: a pure Python interface and a C extension that uses the MySQL C client library (see Chapter 8, The Connector/Python C Extension). This can be configured at runtime using the use_pure connection argument. It defaults to False as of MySQL 8, meaning the C extension is used.

Comments
  • Posting this with the assumption that it's a code problem and not bad credentials.
  • You are missing commas between your parameters, and port needs to be an integer, not a string.
  • It still won't connect.