Error accessing SQLAlchemy and mysql database

0

I have installed an Ubuntu server! 6.04.4 LTS, mysql 5.7.21 and python 3.5.2. I am developing a script in python that gives me the information of the existing databases. The problem is that executing the script always gives me this error:

  

Traceback (most recent call last):     File "/home/rafa/Bash-toolkit/infobd.py", line 22, in       engine = create_engine ('mysql: //' + userbd + ':' + against + '@' + host + ':' + port)     File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/init.py", line 424, in create_engine       return strategy.create (* args, ** kwargs)     File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/engine/strategies.py", line 81, in create       dbapi = dialect_cls.dbapi (** dbapi_args)     File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 102, in dbapi       return import ('MySQLdb')   ImportError: No module named 'MySQLdb'

My code is this:

#!/bin/python3
from sqlalchemy import *
#Antes de abrir la conexión, se necesitarán los datos del usuario
#Borramos los espacios con la f(x) strip() https://stackoverflow.com/questions/761804/how-do-i-trim-whitespace-from-a-python-string
print("Introduzca el nombre del usuario")
usuariobd = input().strip()
print("Introduzca su contraseña")
contra = input()
print("Introduzca el host")
host = input().strip()
print("Introduzca el puerto a conectar (3306)")
puerto = str(input())
#Forzamos que el número que llega es un entero https://stackoverflow.com/questions/5424716/how-to-check-if-string-input-is-a-number
print("Se va a realizar la conexión con el usuario "+ usuariobd + "@" + host+ " con el puerto "+puerto)

#comprobamos que no llegan valores vacíos
if len(usuariobd) == 0 or len(contra)==0 or len(host) == 0 or len(puerto) == 0:
    print("Debes introducir todos los datos")
    exit()
#Probamos la conexión
#https://stackoverflow.com/questions/22689895/list-of-databases-in-sqlalchemy
engine = create_engine('mysql://'+usuariobd+':'+contra+'@'+host+':'+puerto)
#Obtenemos info de las BDS disponibles
consulta = engine.execute('SHOW DATABASES')
listatablas = consulta.fetchall()
print(listatablas)
    
asked by ras212 08.04.2018 в 23:09
source

3 answers

1

It tells you that the MySQLdb module is missing, because this does not exist when sqlalchemy gives an error to the try to import it In ubuntu you find it in the same repositories and you can install it:

For python 2

sudo apt install python-mysqldb

For python 3

sudo apt install python3-mysqldb

There are other alternatives, some of them can be seen from the package manager pip or pip3 : pip search mysql pip3 search mysql

Although in the case of Ubuntu / Debian it will probably work correctly from the repositories (My preferred option)

    
answered by 09.04.2018 / 00:02
source
1

You do not have a driver installed to connect to and interact with the MySQL database, you have several options as shown in the official documentation . Among them you have:

  • mysqlclient :

    • Fork of MySQLdb1 but with support for Python 3.
    • Written in C and the option with the best performance.
    • Install with $ pip3 install mysqlclient or with the python3-mysqldb package provided by the OS.
    • Connection string:

      mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname>
      
  • PyMySQL :

    • Pure Python client.
    • Install with $ pip3 install PyMySQL .
    • Connection string:

      mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]
      
  • mysql-connector-python :

    • Pure Python client.
    • Official Support by Oracle.
    • Install with $ pip3 install mysql-connector-python
    • Connection string:

      mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>
      

When creating your engine as you can see, the syntax is followed:

dialecto+driver://usuario:contraseña@host:puerto/base_de_datos

That allows you to indicate which driver is to be used.

    
answered by 08.04.2018 в 23:54
1

Currently mysql is not supported by python 3 . However, there are alternatives such as: mysqlclient-python and PyMySQL

To install you use pip:

pip install mysql-python (python 2)
pip install mysqlclient (python 3)
apt-get install python-mysqldb (Ubuntu, ...)
    
answered by 08.04.2018 в 23:54