— python, pandas, mysql, e&s — 1 min read
Connecting to database is mostly next step after playing with text/csv files. Here, i am trying to connect to mySQL which i have installed in my laptop using Jupyter Notebook and it seems i have not installed mysql package.
Installed the following,
Installed mysql-connector-python-8.0.19-windows-x86-64bit.msi
which i had got from dev.mysql.com
Installing with pip as well,
1pip install mysql-connector-python
Verifying installation,
1C:\Users\Sushanth>python2Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win323Type "help", "copyright", "credits" or "license" for more information.4>>> import mysql5>>> exit()
Note : Above step would fail, if there were any issues.
Next, i tried to run the below code from Jupyter Notebook,
1import mysql.connector2
3cnx = mysql.connector.connect(user='test', password='test01',4 host='localhost',5 database='TEST')6print(cnx)7cnx.close()
This failed with below message,
1NotSupportedError: Authentication plugin 'caching_sha2_password' is not supported
It seems from from MySQL 8.0, caching_sha2_password
is the default authentication plugin rather than mysql_native_password
and thats the issue.
To resolve this, i created a new user with mysql_native_password
, below are the steps,
Login into mysql as admin user . mysql -u root -p
Create a new user
1CREATE USER 'snake_charmer'@'localhost' IDENTIFIED BY 'pepe' PASSWORD EXPIRE NEVER;
Alter user to be identified with mysql_native_password
1ALTER USER 'snake_charmer'@'localhost' IDENTIFIED WITH mysql_native_password BY 'pepe';
Grant privileges to new user. Since this is test, i am giving everything.
1GRANT ALL PRIVILEGES ON TEST.* To 'snake_charmer'@'localhost' ;
Check the grants
1mysql> show grants for 'snake_charmer'@'localhost';2+-----------------------------------------------------------------+3| Grants for snake_charmer@localhost |4+-----------------------------------------------------------------+5| GRANT USAGE ON *.* TO `snake_charmer`@`localhost` |6| GRANT ALL PRIVILEGES ON `test`.* TO `snake_charmer`@`localhost` |7+-----------------------------------------------------------------+82 rows in set (0.10 sec)
Retry the python connection code in Jupyter again, it should work.
Python SELECT example,
1cnx = mysql.connector.connect(**config)2cursor = cnx.cursor()3
4query = ("SELECT actor_id, first_name, last_name, last_update FROM actor "5 "WHERE last_update BETWEEN %s AND %s")6
7hire_start = dt.date(2016, 12, 10)8hire_end = dt.date(2017, 12, 31)9
10cursor.execute(query, (hire_start, hire_end))11
12for (actor_id, first_name, last_name, last_update) in cursor:13 print("actorID={}, first_name={}, last_name={}, last_update={:%d %b %Y}" \14 .format(actor_id, first_name, last_name, last_update))15
16cursor.close()17cnx.close()
Output:
1actorID=4, first_name=Jenni, last_name=Lewis, last_update=07 Apr 20172actorID=1000, first_name=Cruz, last_name=bluto, last_update=03 Apr 20173actorID=1002, first_name=Hilary, last_name=SWAY, last_update=03 Apr 2017
More information