我的兩臺計算機通過局域網連接。電腦1(192.168.0.56)包含ffollowing程序從局域網訪問sql數據庫
import MySQLdb
db= MySQLdb.connect("localhost","root","pass","testDB")
cursor=db.cursor()
cursor.execute("DROP TABLE IF EXISTS USER")
sql="""CREATE TABLE USER(
ID INT(12) NOT NULL AUTO_INCREMENT,
USERNAME VARCHAR(20) NOT NULL,
PASSWORD VARCHAR(20) NOT NULL,
PRIMARY KEY (ID)
)"""
cursor.execute(sql)
db.close()
從另一個(192.168.0.4)我想插入值表
import MySQLdb
db= MySQLdb.connect("192.168.0.56","root","pass","testDB")
cursor=db.cursor()
sql="""INSERT INTO USER(USERNAME,
PASSWORD)
VALUES('admin','12345'),
('abc','123'),
('user3','5687')"""
try:
cursor.execute(sql)
db.commit()
print ("Data is inserted into database")
except:
db.rollback()
print ("Data cann't be updated")
db.close()
錯誤另一臺計算機:拒絕訪問[email protected]
我嘗試了錯誤:未知MySQL服務器主機 –
檢查你的mysql正在運行哪個端口,使用該端口!所有最好:) –
現在試試這個(與端口3306作爲默認的MySQL端口) db = MySQLdb.connect(「192.168.0.56:3306」,「root」,「pass」,「testDB」) –