PyMySQL can't connect to MySQL on localhost

后端 未结 8 1855
囚心锁ツ
囚心锁ツ 2020-11-28 08:26

I\'m trying to connect to MySQL on localhost using PyMySQL:

import pymysql
conn = pymysql.connect(db=\'base\', user=\'         


        
8条回答
  •  自闭症患者
    2020-11-28 08:31

    This worked for me:

    import pymysql
    
    db = pymysql.connect(host="localhost",port=8889,user="root",passwd="root")
    cursor=db.cursor()
    cursor.execute("SHOW DATABASES")
    results=cursor.fetchall()
    for result in results:
        print (result)
    

    if you want to find the port # go to mysql in terminal, and type:

    SHOW VARIABLES WHERE Variable_name = 'hostname';
    SHOW VARIABLES WHERE Variable_name = 'port';
    

提交回复
热议问题