Skip to content

Commit e31a14f

Browse files
committed
added remote mysql connection tutorial
1 parent 5654c46 commit e31a14f

File tree

5 files changed

+23
-0
lines changed

5 files changed

+23
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,6 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
102102

103103
- ### Database
104104
- [How to Use MySQL Database in Python](https://www.thepythoncode.com/article/using-mysql-database-in-python). ([code](database/mysql-connector))
105+
- [How to Connect to a Remote MySQL Database in Python](https://www.thepythoncode.com/article/connect-to-a-remote-mysql-server-in-python). ([code](database/connect-to-remote-mysql-server))
105106

106107
For any feedback, please consider pulling requests.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Connect to a Remote MySQL Database in Python](https://www.thepythoncode.com/article/connect-to-a-remote-mysql-server-in-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* '%' means from any where in the world*/
2+
CREATE USER 'python-user'@'%' IDENTIFIED BY 'Password1$';
3+
/* grant all privileges to the user on all databases & tables available in the server */
4+
GRANT ALL ON *.* TO 'python-user'@'%';
5+
FLUSH PRIVILEGES;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import mysql.connector as mysql
2+
3+
# enter your server IP address/domain name
4+
HOST = "x.x.x.x" # or "domain.com"
5+
# database name, if you want just to connect to MySQL server, leave it empty
6+
DATABASE = "database"
7+
# this is the user you create
8+
USER = "python-user"
9+
# user password
10+
PASSWORD = "Password1$"
11+
# connect to MySQL server
12+
db_connection = mysql.connect(host=HOST, database=DATABASE, user=USER, password=PASSWORD)
13+
print("Connected to:", db_connection.get_server_info())
14+
15+
# enter your code here!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mysql-connector-python

0 commit comments

Comments
 (0)