-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmysql-tutorial.txt
More file actions
121 lines (98 loc) · 3.11 KB
/
Copy pathmysql-tutorial.txt
File metadata and controls
121 lines (98 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
$ sudo apt install mysql-server
# Check status
$ sudo service mysql status
# Edit this file to grant access to root without password
$ sudo nano /etc/mysql/my.cnf
... (Previous thingy)
[mysqld]
skip-grant-tables
# Then go back to terminal
$ mysql -u root
mysql> USE mysql;
mysql> UPDATE user SET authentication_string='' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> EXIT;
# Then delete added content in /etc/mysql/my.cnf
$ sudo nano /etc/mysql/my.cnf
... (Previous thingy)
# [mysqld]
# skip-grant-tables
# Then restart mysql service
$ sudo service mysql restart
# Finally go back to mysql server
$ mysql -u root -p
Password: (dont type anything, just enter)
mysql> (Type all things in setup.sql)
# Done.
############################## NullFD2
rm -rf migrations
flask db init
mysql -u todolist -p
flask db migrate
flask db upgrade
=======
#########################################################################################
# Or: Use Docker
# Create Dockerfile with:
```Dockerfile
# Use the official MySQL image from Docker Hub
FROM mysql:latest
# Set the root password for MySQL
ENV MYSQL_ROOT_PASSWORD=password
# Expose port 3306 for external communication
EXPOSE 3306
# Start MySQL service
CMD ["mysqld", "--bind-address=0.0.0.0"]
```
# Active using:
$ docker build -t mysql_server .
# Run container:
$ docker run -d -p <PORT>:3306 --name mysql_container mysql_server
# (Depends on backend's port, replace <PORT> pls)
# Identify container ID using
$ docker ps -a
# Go to MySQL inside container:
$ docker exec -it CONTAINER_ID_OR_NAME mysql -uroot -p
Password: password
mysql> (Copy all thingy in setup.sql)
NOTE1: Replace 'localhost' with '%' (because MySQL Docker will run in different IP than 127.0.0.1)
NOTE2: If you accidentally execute all setup.sql with 'localhost', undo with:
mysql> UPDATE mysql.user SET Host='%' WHERE User='todolist';
mysql> REVOKE ALL PRIVILEGES ON todolist.* FROM 'todolist'@'localhost';
mysql> GRANT ALL PRIVILEGES ON todolist.* TO 'todolist'@'%' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
mysql> -- Verify
mysql> SELECT Host, User FROM mysql.db WHERE Db='todolist';
# Done
NOTE3: To test connector using Python:
``` python
import mysql.connector
# Define the connection parameters
host = 'localhost'
port = 3306 # This is the port mapped to MySQL in the Docker container
user = 'todolist'
password = 'Todolist<123456789' # Change this to your MySQL root password
try:
# Connect to the MySQL server
connection = mysql.connector.connect(
host=host,
port=port,
user=user,
password=password,
database='todolist'
)
if connection.is_connected():
print("Successfully connected to MySQL server")
# Get the MySQL server version
cursor = connection.cursor()
cursor.execute("SELECT VERSION()")
version = cursor.fetchone()[0]
print("MySQL server version:", version)
# Close cursor and connection
cursor.close()
connection.close()
print("Connection closed")
except mysql.connector.Error as error:
print("Failed to connect to MySQL server:", error)
```
################## master