How to get Python MYSQL connection username and password?

Viewed 8976

I have installed spyder with anacoda for python 3.7 version for windows. Then if I print("hello"), it works fine.

Now I want to connect with MYSQL database so I have installed python drivers to communicate with MYSQL. Using Anaconda Prompt I have run below command:

conda install -c anaconda mysql-connector-python

I got message in Anaconda Prompt that packages installed successfully. I don't know how to get username and password to connect with MYSQL? I have already checked that MYSQL installed properly using import mysql.connector.

Code as per below:

import mysql.connector
from mysql.connector import Error
def connect():
   try:
       conn = mysql.connector.connect(host='127.0.0.1',
                                   user='',
                                   passwd='')

       if conn.is_connected():
           print('Connected to MySQL database')

   except Error as e:
       print(e)
connect()

Error:

2003: Can't connect to MySQL server on '127.0.0.1:3306' (10061 No connection could be made because the target machine actively refused it)
2 Answers

Have you installed MySQL? If you have, then there you will have username and password. Once you have them, use them in:

conn = mysql.connector.connect(host='127.0.0.1',user=username, passwd=password,db=database_name)

MYSQL has nothing to do with python this is its own rdbms, first you need to download it with other stuff including MySQL Community Server (GPL) and run the server, when you setting it up thats when you set username and password. MAKE SURE your mysql is the same as your python editor i.e. 32bit or 64bit otherwise you wont be able to establish a connection (had that problem myself). But honestly best way would be watch a youtube video: https://www.youtube.com/watch?v=WuBcTJnIuzo

20 minutes and it explains everything pretty well

Related