Connect to MYSQL with Python

python
mysql

#1

I’m trying to get connect from Python a mysql database in google cloud but somehow Python does not recognize the library mysql.connector . Could someone please help me? I would really appreciate it. Thanks :slight_smile:

Error: ImportError: No module named connector
import mysql.connector

from mysql.connector import (connection);

cnx = connection.MySQLConnection(user='*******', password='********',
                                 host='*.*.*.*',
                                 database='database_name')
cnx.close()

#2

From this Stack Overflow thread:

MYSQL Utilities assumes that the MySQL Connector for Python has been installed. If you install it (http://dev.mysql.com/downloads/connector/python/), MySQL Utilities should run OK.

From that link, make sure to follow install instructions for Ubuntu 16.04 and you should be all set :thumbsup:


#3

Thanks bradydowling for your prompt answer and I’m sorry but I probably was not clear in my quetion

I’m using Python in CLoud9 to try to get connect it to a Google MySQL Second Generation database that is running fine and I’m communicating perfectly though MySql Workbench

When I type import mysql in a pyhton file .py its not recognizing the libraries, I’m also using Python 3 as Runner

I have also downloaded what you sent me but its a .deb file, what am I suppose to do with that?

Thank for your help!


#4

Sounds like you might need to do a little more research to really understand what’s going on here (I don’t to be honest). I’d suggest you start with this topic and then move forward from there.


#5

I’m trying to get connect it to a MySQL Google Cloud database
In this platform impossible for me.
I have already download and install the library through the console but when I try to import the MySQL library Python does not recognize it
import mysql.connector
import mysql
mysql.connector
Python does not recognize MySQL
Could you help me please?

Thanks


#6

Best way to connect to MySQL from python is to Use MySQL Connector/Python because it is official Oracle driver for MySQL for working with Python and it works with both Python 3 and Python 2.

follow the steps mentioned below to connect MySQL

Example:

import mysql.connector
from mysql.connector import Error
try:
   conn = mysql.connector.connect(host='hostname',
                     database='db',
                     user='root',
                     password='passcode')
   if conn.is_connected():
      cursor = conn.cursor()
      cursor.execute("select database();")
      record = cursor.fetchall()
     print ("Your Data  - ", record)
     cursor.close()
     conn.close()