簡體   English   中英

如何從任何計算機編輯我的 MySQL 數據庫?

[英]How can I edit my MySQL DB from any computer?

我創建了一個訪問 MySQL 數據庫的 Python 程序。 它在運行時所做的是將用戶的 Logged_in 列編輯為 1,因此我知道該用戶已登錄。

UPDATE users SET logged_in = 1 WHERE username = "username";

工作完美。 每次運行時,它都會編輯數據庫,以便我知道誰登錄了。

問題是,它只適用於我的電腦。 當其他人使用它時,它說

DatabaseError: 2003 (HY000): Can´t connect to MySQL server on 'ip address'

我不明白我必須做什么才能讓這個數據庫從任何地方和任何人都可以訪問和編輯。

我很感激任何建議。

注意:我讀過類似的問題,一個解決方案是讓計算機想要訪問主機,但我需要這個數據庫可以被運行該程序的任何人從字面上編輯,所以我不會有他們的計算機信息。

簡化代碼:

import mysql.connector

#User inputs username and password in Tkinter GUI
username = "StackOverflow"
password = "123456"

def main():
        
    #ACCESS DB
    mydb = mysql.connector.connect(
        host="my_computer_ip",
        user="database_user",
        password="password",
        database = "users",
        port=3306
        )
    
    #GET CURSOR (ALLOW TO EXECUTE COMMANDS)
    mycursor = mydb.cursor()
    
    #GET USER INFORMATION
    mycursor.execute("SELECT username, password, logged_in FROM users WHERE 
                      username = '{}';".format(username))
    
    myresult = mycursor.fetchone()
    
    #Display results if user is found.
    if myresult is not None and len(myresult) > 0:
        print("Connection Successful! \n User:{} \n Pass{}".format(myresult[0], myresult[1]))

    else:
        print("User does not exist.")
        return False
    
    # Next step is to match the input password to the one in the database,
    # If they match, allow access to the GUI.
    # Also, logged in column would be changed to 1 to let the database know the user is on.
    
main()

您的數據庫用戶可能只能通過 localhost 訪問。 檢查數據庫用戶和權限。 如果您使用的是 MySQL Workbench,則可以運行以下查詢:

從 mysql.user 中選擇用戶、主機;

分配給數據庫用戶的主機應該是通配符 (%) 而不是localhost ,以便在任何地方訪問數據庫。

要么您使用 % 主機創建另一個數據庫用戶,要么更新現有用戶。

CREATE USER 'database_user'@'%' IDENTIFIED BY 'password'; UPDATE mysql.user SET host='%' WHERE user='database_user';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM