MySQL設置IP連接和Python操作數(shù)據(jù)庫
許多同學在學習數(shù)據(jù)庫的時候不知如何設置才能讓自己PC上的MySQL讓別人可以訪問以及如何去利用Python操作數(shù)據(jù)庫,今天就送點干貨給大家。一、先看看怎么設置ip連接數(shù)據(jù)庫
默認情況下mysql只允許本地進入設置,如果需要外部IP連接到mysql,需要向mysql數(shù)據(jù)庫里的“user”表里添加相關授權。 進入命令行界面(Window+r、輸入cmd、enter進入):
1. mysql -uroot -p密碼 #登錄mysql2. use mysql; #使用mysql數(shù)據(jù)庫3. grant all privileges on *.* to root@'%'identified by '密碼' WITH GRANT OPTION; #授予權限4. flush privileges; #刷新權限使其生效
操作完后才能后我們自己PC上的MySQL就可以ip訪問了。
二、Python操作數(shù)據(jù)庫
1. 連接數(shù)據(jù)庫查詢數(shù)據(jù)
#查詢操作。import pymysqlmysql_connect = pymysql.connect(host='localhost',user='root', passwd='',db='niushop_b2c',port=3306,charset='utf8')#創(chuàng)建游標并讀取數(shù)據(jù)庫數(shù)據(jù),逐行讀取 mysql_cursor = mysql_connect.cursor()#執(zhí)行查詢語句mysql_cursor.execute('select * fromns_member;')data = mysql_cursor.fetchall()print(data)mysql_connect.close()mysql_cursor.close()
2. 修改數(shù)據(jù)
#修改表數(shù)據(jù)。import pymysqlmysql_connect =pymysql.connect(host='localhost',user='root',passwd='123456', db='test',port=3307, charset='utf8')#創(chuàng)建游標并讀取數(shù)據(jù)庫數(shù)據(jù),逐行讀取mysql_cursor = mysql_connect.cursor()#執(zhí)行修改表的語句mysql_cursor.execute('update pay setmax="6000" where dengji=1;')#提交操作mysql_connect.commit()mysql_connect.close()mysql_cursor.close()
頁:
[1]