pythonmysqlanacondaconnector

How to get Python MYSQL connection username and password?


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)

Solution

  • 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)