ORM hoặc Trình ánh xạ quan hệ đối tượng là một phần mềm hoặc công cụ ánh xạ mã của bạn tới cơ sở dữ liệu mà không cần bạn sử dụng trực tiếp trình kết nối cơ sở dữ liệu vì nó abstracts
toàn bộ quá trình kết nối cơ sở dữ liệu. Đến lượt nó, điều này giúp chúng tôi có thể kết nối với bất kỳ cơ sở dữ liệu quan hệ nào mà không cần thay đổi quá nhiều mã
[Trừu tượng hóa là quá trình cung cấp các tính năng của một công cụ hoặc chương trình để sử dụng trực tiếp mà không cần chúng tôi bận tâm về cách thức hoạt động của nó]
Đối với quy trình này, chúng tôi sẽ sử dụng flask_sqlalchemy
, một tiện ích mở rộng bình để tận dụng các tính năng của sqlalchemy. [trừu tượng😄]
Tôi cũng cho rằng chúng tôi đã cài đặt MySQL trong môi trường làm việc của mình. Đọc thêm tại đây về cách cài đặt MySQL trên ubuntu
Chúng tôi cũng sẽ cài đặt bình vì đây sẽ là một ứng dụng bình
$ pip3 install flask
...
$ pip3 install flask_sqlalchemy
...
Vào chế độ toàn màn hình Thoát chế độ toàn màn hình
Tiếp theo, chúng tôi chuyển sang tạo cơ sở dữ liệu của mình bằng cách bắt đầu phiên MySQL của chúng tôi và sau đó tạo cơ sở dữ liệu của chúng tôi
$ mysql -h hostname -u test_user -p
...
mysql > CREATE DATABASE testdb;
Vào chế độ toàn màn hình Thoát chế độ toàn màn hình
Sau đó, chúng tôi tiến hành tạo ứng dụng bình, nơi chúng tôi sẽ thêm tất cả các cấu hình và tạo một mô hình cơ sở dữ liệu cơ bản
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask[__name__]
app.config['SECRET_KEY'] = "xxxxxxxx"
db = SQLAlchemy[app]
# configuring our database uri
# note an error here
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://{username}:{password}@{server}/testdb".format[username, password, server]
# basic model
class User[db.Model]:
__tablename__ = 'user'
id = db.Column[db.Integer, primary_key=True]
email = db.Column[db.String[64], unique=True, index=True]
password = db.Column[db.String[128]]
if __name__ == "__main__":
app.run[debug=True]
Vào chế độ toàn màn hình Thoát chế độ toàn màn hình
Để tạo các bảng của chúng tôi trong cơ sở dữ liệu, chúng tôi mở phiên shell python và thực hiện như sau
>>>from ourflaskapp import app, db
>>>app.app_context[].push[]
>>>db.create_all[]
Vào chế độ toàn màn hình Thoát chế độ toàn màn hình
$ mysql -h hostname -u test_user -p
...
mysql > CREATE DATABASE testdb;
0 trả về lỗiModuleNotFoundError: No module named 'MySQLdb'
Vào chế độ toàn màn hình Thoát chế độ toàn màn hình
Điều này là do, ngay cả khi chúng ta không thấy quá trình kết nối cơ sở dữ liệu khi sqlalchemy tóm tắt nó, nó vẫn xảy ra và ở đây, chúng ta không có mô-đun 'MySQLdb' phù hợp để thực hiện điều này
Vài tháng trước, tôi đã viết về việc sử dụng khung Django với MySQL 8. Ngoài ra còn có các khung Python khác đáng xem xét. Trong blog này, tôi sẽ xem xét sử dụng SQLAlchemy với MySQL 8
Để bạn có thể sử dụng MySQL 8 với SQLAlchemy, bạn cần ba phần mềm. Máy chủ MySQL, Trình kết nối MySQL/Python và SQLAlchemy. Tôi sẽ xem qua phần cài đặt, sau đó tôi sẽ xem một ví dụ về mã
Thông tin
Các ví dụ trong blog này sử dụng MySQL Server 8. 0. 15, Trình kết nối MySQL/Python 8. 0. 15 và SQLAlchemy 1. 2. 18. Cũng có thể sử dụng lại các hướng dẫn với các phiên bản MySQL khác, ngoại trừ trong các phiên bản MySQL cũ hơn, bạn cần sử dụng rõ ràng bộ ký tự utf8mb4
Nếu bạn không muốn đọc hết toàn bộ blog, điều chính để sử dụng MySQL và Trình kết nối MySQL/Python với SQLAlchemy là tạo công cụ có phương ngữ được đặt thành
shell$ sudo yum install //dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpmLoaded plugins: langpacks, ulninfo Repository ol7_developer_EPEL is listed more than once in the configuration mysql80-community-release-el7-1.noarch.rpm | 25 kB 00:00:00 Examining /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm: mysql80-community-release-el7-1.noarch Marking /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql80-community-release.noarch 0:el7-1 will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================== Package Arch Version Repository Size ====================================================================================================== Installing: mysql80-community-release noarch el7-1 /mysql80-community-release-el7-1.noarch 31 k Transaction Summary ====================================================================================================== Install 1 Package Total size: 31 k Installed size: 31 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql80-community-release-el7-1.noarch 1/1 Verifying : mysql80-community-release-el7-1.noarch 1/1 Installed: mysql80-community-release.noarch 0:el7-1 Complete!7 và trình điều khiển thành
shell$ sudo yum install //dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpmLoaded plugins: langpacks, ulninfo Repository ol7_developer_EPEL is listed more than once in the configuration mysql80-community-release-el7-1.noarch.rpm | 25 kB 00:00:00 Examining /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm: mysql80-community-release-el7-1.noarch Marking /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql80-community-release.noarch 0:el7-1 will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================== Package Arch Version Repository Size ====================================================================================================== Installing: mysql80-community-release noarch el7-1 /mysql80-community-release-el7-1.noarch 31 k Transaction Summary ====================================================================================================== Install 1 Package Total size: 31 k Installed size: 31 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql80-community-release-el7-1.noarch 1/1 Verifying : mysql80-community-release-el7-1.noarch 1/1 Installed: mysql80-community-release.noarch 0:el7-1 Complete!8
________số 8_______
Cài đặt máy chủ MySQL
Có một số cách để cài đặt MySQL Server và cách nào là tốt nhất tùy thuộc vào hoàn cảnh và sở thích của bạn. Vì mục đích của blog này, tôi sẽ trình bày cách cài đặt MySQL Server trên Oracle Linux/RHEL/CentOS 7 bằng RPM và trên Microsoft Windows bằng MySQL Installer. Để có thêm tùy chọn, hãy xem chương cài đặt trong sách hướng dẫn tham khảo. Trước tiên hãy xem cài đặt Linux
Cài đặt RPM trên Enterprise Linux
MySQL cung cấp các kho lưu trữ cho một số bản phân phối Linux bao gồm họ Oracle Linux/RHEL/CentOS. Điều này giúp dễ dàng cài đặt MySQL. Bước để cài đặt định nghĩa kho lưu trữ là
shell$ sudo yum install //dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpmLoaded plugins: langpacks, ulninfo Repository ol7_developer_EPEL is listed more than once in the configuration mysql80-community-release-el7-1.noarch.rpm | 25 kB 00:00:00 Examining /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm: mysql80-community-release-el7-1.noarch Marking /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql80-community-release.noarch 0:el7-1 will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================== Package Arch Version Repository Size ====================================================================================================== Installing: mysql80-community-release noarch el7-1 /mysql80-community-release-el7-1.noarch 31 k Transaction Summary ====================================================================================================== Install 1 Package Total size: 31 k Installed size: 31 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql80-community-release-el7-1.noarch 1/1 Verifying : mysql80-community-release-el7-1.noarch 1/1 Installed: mysql80-community-release.noarch 0:el7-1 Complete!
Bây giờ, bạn có thể cài đặt MySQL Server. Có một số RPM để lựa chọn và bạn cần tùy thuộc vào những tính năng khác mà bạn cần sử dụng. Một bộ RPM phổ biến có thể được cài đặt như
shell$ sudo yum install mysql-community-server mysql-community-libs \ mysql-community-libs-compat mysql-community-common mysql-community-client ...
Ghi chú
Nếu bạn có cài đặt MySQL khác, nó sẽ được nâng cấp lên phiên bản mới nhất [tại thời điểm viết bài 8. 0. 15]
Trong lần khởi động đầu tiên, thư mục dữ liệu sẽ được khởi tạo
shell$ sudo systemctl start mysqld
Để giữ an toàn cho cài đặt mới, một mật khẩu ngẫu nhiên đã được đặt cho người dùng root. Điều này có thể được tìm thấy từ nhật ký lỗi MySQL
shell$ sudo grep password /var/log/mysqld.log 2018-11-05T08:05:09.985857Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: KWNfuA!1r:PF
Sử dụng mật khẩu này để kết nối với MySQL và cập nhật mật khẩu [vui lòng sử dụng mật khẩu mạnh]
shell$ mysql --user=root --password Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 19 Server version: 8.0.15 MySQL Community Server - GPL Copyright [c] 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> ALTER USER root@localhost IDENTIFIED BY 'Kij0@jDi~Faf4'; Query OK, 0 rows affected [0.13 sec]
MySQL hiện đã sẵn sàng để sử dụng. Trước khi tiếp tục, tôi sẽ đưa ra một ví dụ cài đặt MySQL trên Microsoft Windows
Microsoft Windows
Trên Microsoft Windows, một cách dễ dàng để cài đặt MySQL là sử dụng Trình cài đặt MySQL. Trình cài đặt có thể được tải xuống từ trang tải xuống MySQL. Trình cài đặt MySQL có thể được sử dụng để cài đặt hầu hết các sản phẩm MySQL. Nếu bạn khởi động MySQL Installer lần đầu tiên, bạn sẽ được đưa thẳng đến màn hình để chọn sản phẩm sẽ cài đặt;
Trên màn hình Chọn Sản phẩm và Tính năng, chọn cài đặt MySQL Server 8. 0 [Trình cài đặt MySQL sẽ liệt kê bản phát hành mới nhất từ danh sách các sản phẩm có sẵn]
Tùy chọn, bạn có thể lọc danh sách sản phẩm. Hãy thoải mái lựa chọn các sản phẩm khác mà bạn muốn. Trình thông báo MySQL có thể hữu ích để bắt đầu và dừng MySQL, nếu bạn không định chạy MySQL mọi lúc. Bạn cũng có thể cài đặt MySQL Connector/Python theo cách này, tuy nhiên đối với blog này, một phương pháp khác sẽ được sử dụng
Làm theo hướng dẫn cài đặt. Đối với blog này, các lựa chọn mặc định sẽ hoạt động, mặc dù trong quá trình định cấu hình, bạn có thể muốn đảm bảo bỏ chọn các cổng Tường lửa của Windows để truy cập mạng trừ khi bạn cần truy cập từ xa
Chuẩn bị máy chủ MySQL
Mặc dù MySQL hiện đã sẵn sàng hoạt động với SQLAlchemy, nhưng bạn có thể muốn thực hiện thêm một số bước chuẩn bị. Ở đây, việc tạo người dùng MySQL và lược đồ [cơ sở dữ liệu] được sử dụng bởi ứng dụng của bạn sẽ được đề cập
Một ví dụ về việc tạo người dùng
shell$ sudo yum install //dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpmLoaded plugins: langpacks, ulninfo Repository ol7_developer_EPEL is listed more than once in the configuration mysql80-community-release-el7-1.noarch.rpm | 25 kB 00:00:00 Examining /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm: mysql80-community-release-el7-1.noarch Marking /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql80-community-release.noarch 0:el7-1 will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================== Package Arch Version Repository Size ====================================================================================================== Installing: mysql80-community-release noarch el7-1 /mysql80-community-release-el7-1.noarch 31 k Transaction Summary ====================================================================================================== Install 1 Package Total size: 31 k Installed size: 31 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql80-community-release-el7-1.noarch 1/1 Verifying : mysql80-community-release-el7-1.noarch 1/1 Installed: mysql80-community-release.noarch 0:el7-1 Complete!9 và cấp cho nó tất cả các đặc quyền đối với lược đồ
shell$ sudo yum install mysql-community-server mysql-community-libs \ mysql-community-libs-compat mysql-community-common mysql-community-client ...0 và để tạo lược đồ
shell$ sudo yum install mysql-community-server mysql-community-libs \ mysql-community-libs-compat mysql-community-common mysql-community-client ...0 là
mysql> CREATE USER pyuser@localhost IDENTIFIED BY 'Py@pp4Demo'; Query OK, 0 rows affected [0.01 sec] mysql> GRANT ALL ON sqlalchemy.* TO pyuser@localhost; Query OK, 0 rows affected [0.01 sec] mysql> CREATE SCHEMA sqlalchemy; Query OK, 1 row affected [0.01 sec]
Điều này sẽ cho phép người dùng
shell$ sudo yum install mysql-community-server mysql-community-libs \ mysql-community-libs-compat mysql-community-common mysql-community-client ...2 kết nối từ cùng một máy chủ khi Máy chủ MySQL được cài đặt bằng cách xác thực bằng mật khẩu
shell$ sudo yum install mysql-community-server mysql-community-libs \ mysql-community-libs-compat mysql-community-common mysql-community-client ...3
Cài đặt Trình kết nối MySQL/Python và SQLAlchemy
Cả MySQL Connector/Python và SQLAlchemy đều có thể được cài đặt theo cách độc lập với nền tảng bằng cách sử dụng lệnh
shell$ sudo yum install mysql-community-server mysql-community-libs \ mysql-community-libs-compat mysql-community-common mysql-community-client ...4. Kể từ Python 2. 7 sắp hết tuổi thọ, tôi sẽ giả sử Python 3. 6 trong blog này. [Trình kết nối MySQL/Python 8. 0. 13 trở lên cũng hỗ trợ Python 3. 7. ]
Nếu bạn không có Python 3. 6 được cài đặt trên Oracle Linux/RHEL/CentOS 7, bạn có thể dễ dàng cài đặt nó, ví dụ từ kho lưu trữ EPEL. Giả sử bạn đã cấu hình kho lưu trữ EPEL, các bước sau sẽ cài đặt Python 3. 6, kích hoạt
shell$ sudo yum install mysql-community-server mysql-community-libs \ mysql-community-libs-compat mysql-community-common mysql-community-client ...4 và cập nhật
shell$ sudo yum install mysql-community-server mysql-community-libs \ mysql-community-libs-compat mysql-community-common mysql-community-client ...4 lên phiên bản mới nhất
shell$ yum install python36 shell$ python3.6 -m ensurepip shell$ python3.6 -m pip install --upgrade pip
Bây giờ bạn có thể sử dụng
shell$ sudo yum install mysql-community-server mysql-community-libs \ mysql-community-libs-compat mysql-community-common mysql-community-client ...7 để gọi Python 3. 6. Trong phần sau, thay thế
shell$ sudo yum install mysql-community-server mysql-community-libs \ mysql-community-libs-compat mysql-community-common mysql-community-client ...8 bằng
shell$ sudo yum install mysql-community-server mysql-community-libs \ mysql-community-libs-compat mysql-community-common mysql-community-client ...7 nếu bạn đã cài đặt Python 3. 6 theo cách này
Để cài đặt bản phát hành MySQL Connector/Python mới nhất [hiện tại là 8. 0. 15]
PS> python -m pip install mysql-connector-python Collecting mysql-connector-python Using cached //files.pythonhosted.org/packages/31/45/ef8cf013918108f508a1a1bb5539abaff5f78f3a569f7fa30232967713c9/mysql_connector_python-8.0.15-cp36-cp36m-win_amd64.whl Collecting protobuf>=3.0.0 [from mysql-connector-python] Downloading //files.pythonhosted.org/packages/5d/5c/476f473c2efc0a8d9fd7185e6c08dcbd21c469698e2a80487fa054b8c5ba/protobuf-3.7.0-cp36-cp36m-win_amd64.whl [1.1MB] 100% |████████████████████████████████| 1.1MB 6.6MB/s Requirement already satisfied: six>=1.9 in c:\users\myuser\appdata\local\programs\python\python36\lib\site-packages [from protobuf>=3.0.0->mysql-connector-python] [1.11.0] Requirement already satisfied: setuptools in c:\users\myuser\appdata\local\programs\python\python36\lib\site-packages [from protobuf>=3.0.0->mysql-connector-pytho n] [28.8.0] Installing collected packages: protobuf, mysql-connector-python Successfully installed mysql-connector-python-8.0.15 protobuf-3.7.0
Cài đặt SQLAlchemy tương tự
PS> python -m pip install SQLAlchemy Collecting SQLAlchemy Downloading //files.pythonhosted.org/packages/21/ed/7eb53305b43ca51774a458d7c292f3bc7664d7a9bbb5bac4149fa34756b9/SQLAlchemy-1.2.18.tar.gz [5.7MB] 100% |████████████████████████████████| 5.7MB 3.3MB/s Installing collected packages: SQLAlchemy Running setup.py install for SQLAlchemy .. done
Đó là nó. Bây giờ bạn đã sẵn sàng sử dụng SQLAlchemy với MySQL Connector/Python 8 và MySQL Server 8
Ví dụ SQLAlchemy
Với MySQL và SQLAlchemy đã được cài đặt, bạn có thể dễ dàng sử dụng SQLAlchemy trong ứng dụng của mình. Ở đây, tôi sẽ chỉ hiển thị một ví dụ rất đơn giản dựa trên hướng dẫn trong sách hướng dẫn SQLAlchemy chính thức
Bước đầu tiên là nhập các phần cần thiết của SQLAlchemy. Trong ví dụ này, chỉ bản thân
shell$ sudo yum install mysql-community-server mysql-community-libs \ mysql-community-libs-compat mysql-community-common mysql-community-client ...0 và
shell$ sudo systemctl start mysqld1 từ
shell$ sudo systemctl start mysqld2 là bắt buộc
shell$ sudo yum install //dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpmLoaded plugins: langpacks, ulninfo Repository ol7_developer_EPEL is listed more than once in the configuration mysql80-community-release-el7-1.noarch.rpm | 25 kB 00:00:00 Examining /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm: mysql80-community-release-el7-1.noarch Marking /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql80-community-release.noarch 0:el7-1 will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================== Package Arch Version Repository Size ====================================================================================================== Installing: mysql80-community-release noarch el7-1 /mysql80-community-release-el7-1.noarch 31 k Transaction Summary ====================================================================================================== Install 1 Package Total size: 31 k Installed size: 31 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql80-community-release-el7-1.noarch 1/1 Verifying : mysql80-community-release-el7-1.noarch 1/1 Installed: mysql80-community-release.noarch 0:el7-1 Complete!0
Bước thứ hai là xác định MySQL là công cụ và bạn muốn kết nối bằng MySQL Connector/Python
shell$ sudo yum install //dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpmLoaded plugins: langpacks, ulninfo Repository ol7_developer_EPEL is listed more than once in the configuration mysql80-community-release-el7-1.noarch.rpm | 25 kB 00:00:00 Examining /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm: mysql80-community-release-el7-1.noarch Marking /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql80-community-release.noarch 0:el7-1 will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================== Package Arch Version Repository Size ====================================================================================================== Installing: mysql80-community-release noarch el7-1 /mysql80-community-release-el7-1.noarch 31 k Transaction Summary ====================================================================================================== Install 1 Package Total size: 31 k Installed size: 31 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql80-community-release-el7-1.noarch 1/1 Verifying : mysql80-community-release-el7-1.noarch 1/1 Installed: mysql80-community-release.noarch 0:el7-1 Complete!1
Định nghĩa về MySQL và MySQL Connector Python xảy ra ở dòng 6.
shell$ sudo yum install //dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpmLoaded plugins: langpacks, ulninfo Repository ol7_developer_EPEL is listed more than once in the configuration mysql80-community-release-el7-1.noarch.rpm | 25 kB 00:00:00 Examining /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm: mysql80-community-release-el7-1.noarch Marking /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql80-community-release.noarch 0:el7-1 will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================== Package Arch Version Repository Size ====================================================================================================== Installing: mysql80-community-release noarch el7-1 /mysql80-community-release-el7-1.noarch 31 k Transaction Summary ====================================================================================================== Install 1 Package Total size: 31 k Installed size: 31 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql80-community-release-el7-1.noarch 1/1 Verifying : mysql80-community-release-el7-1.noarch 1/1 Installed: mysql80-community-release.noarch 0:el7-1 Complete!7 xác định rằng bạn đang sử dụng MySQL làm cơ sở dữ liệu [phương ngữ] và
shell$ sudo yum install //dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpmLoaded plugins: langpacks, ulninfo Repository ol7_developer_EPEL is listed more than once in the configuration mysql80-community-release-el7-1.noarch.rpm | 25 kB 00:00:00 Examining /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm: mysql80-community-release-el7-1.noarch Marking /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql80-community-release.noarch 0:el7-1 will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================== Package Arch Version Repository Size ====================================================================================================== Installing: mysql80-community-release noarch el7-1 /mysql80-community-release-el7-1.noarch 31 k Transaction Summary ====================================================================================================== Install 1 Package Total size: 31 k Installed size: 31 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql80-community-release-el7-1.noarch 1/1 Verifying : mysql80-community-release-el7-1.noarch 1/1 Installed: mysql80-community-release.noarch 0:el7-1 Complete!8 cho biết bạn muốn sử dụng MySQL Connector/Python làm trình điều khiển. Phần còn lại của dòng xác định các tùy chọn kết nối. Trong trường hợp này bạn có
shell$ sudo systemctl start mysqld5. Bạn có thể thêm nhiều tùy chọn nếu bạn cần. Vì vậy, trong trường hợp này, các tùy chọn sau đã được đặt
- Người dùng. pyuser
- Mật khẩu. Py@app4Demo
- Chủ nhà. máy chủ cục bộ
- Hải cảng. 3306
- Cơ sở dữ liệu mặc định. sqlalchemy
Tránh xa
Không bao giờ mã cứng các tham số kết nối vào ứng dụng của bạn. Riêng mật khẩu thì tuyệt đối không được. Nó được thực hiện ở đây để giữ cho ví dụ đơn giản, nhưng làm như vậy trong một ứng dụng thực tế khiến việc triển khai trở nên khó khăn và gây ra vấn đề bảo mật nghiêm trọng
Xem thêm tài liệu về cấu hình động cơ trong sổ tay SQLAlchemy để biết thêm thông tin
Đối số
shell$ sudo systemctl start mysqld6 làm cho SQLAlchemy in từng câu lệnh SQL mà nó thực thi. Điều này có thể hữu ích khi thử nghiệm
Bước thứ ba là xác định và tạo một bảng – trong ví dụ này là bảng
shell$ sudo systemctl start mysqld7
shell$ sudo yum install //dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpmLoaded plugins: langpacks, ulninfo Repository ol7_developer_EPEL is listed more than once in the configuration mysql80-community-release-el7-1.noarch.rpm | 25 kB 00:00:00 Examining /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm: mysql80-community-release-el7-1.noarch Marking /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql80-community-release.noarch 0:el7-1 will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================== Package Arch Version Repository Size ====================================================================================================== Installing: mysql80-community-release noarch el7-1 /mysql80-community-release-el7-1.noarch 31 k Transaction Summary ====================================================================================================== Install 1 Package Total size: 31 k Installed size: 31 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql80-community-release-el7-1.noarch 1/1 Verifying : mysql80-community-release-el7-1.noarch 1/1 Installed: mysql80-community-release.noarch 0:el7-1 Complete!2
MySQL yêu cầu bạn chỉ định số ký tự tối đa cho cột
shell$ sudo systemctl start mysqld8, đây là kiểu dữ liệu được sử dụng khi chỉ định
shell$ sudo systemctl start mysqld9. Vì vậy, đối số
shell$ sudo grep password /var/log/mysqld.log 2018-11-05T08:05:09.985857Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: KWNfuA!1r:PF0 được thông qua. [Đối số
shell$ sudo grep password /var/log/mysqld.log 2018-11-05T08:05:09.985857Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: KWNfuA!1r:PF0 cũng là đối số đầu tiên của
shell$ sudo systemctl start mysqld9, vì vậy bạn không cần chỉ định rõ ràng rằng đó là độ dài. ]
Lệnh gọi tới
shell$ sudo grep password /var/log/mysqld.log 2018-11-05T08:05:09.985857Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: KWNfuA!1r:PF3 yêu cầu SQLAlchemy tạo bảng cơ sở dữ liệu cơ bản, nếu nó chưa tồn tại. Nếu không, bảng hiện có sẽ được sử dụng
Bước thứ tư là thêm người dùng. Điều này đòi hỏi một phiên. Sau khi phiên được tạo, bạn có thể thêm người dùng vào phiên đó
shell$ sudo yum install //dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpmLoaded plugins: langpacks, ulninfo Repository ol7_developer_EPEL is listed more than once in the configuration mysql80-community-release-el7-1.noarch.rpm | 25 kB 00:00:00 Examining /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm: mysql80-community-release-el7-1.noarch Marking /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql80-community-release.noarch 0:el7-1 will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================== Package Arch Version Repository Size ====================================================================================================== Installing: mysql80-community-release noarch el7-1 /mysql80-community-release-el7-1.noarch 31 k Transaction Summary ====================================================================================================== Install 1 Package Total size: 31 k Installed size: 31 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql80-community-release-el7-1.noarch 1/1 Verifying : mysql80-community-release-el7-1.noarch 1/1 Installed: mysql80-community-release.noarch 0:el7-1 Complete!3
Có hai điều, tôi muốn bạn chú ý đến đây. Khi xác định
shell$ sudo grep password /var/log/mysqld.log 2018-11-05T08:05:09.985857Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: KWNfuA!1r:PF4 ở dòng 31, biệt hiệu được đặt thành biểu tượng cảm xúc cá heo. Biểu tượng cảm xúc này yêu cầu bốn byte trong UTF-8 [0xF09F90AC]. Trong các phiên bản cũ hơn của MySQL [5. 7 trở về trước], bạn sẽ cần thay đổi rõ ràng bộ ký tự để xử lý UTF-8. Tuy nhiên, trong MySQL 8, biến thể bốn byte của UTF-8 [utf8mb4] là mặc định, vì vậy biểu tượng cảm xúc cá heo sẽ hoạt động ngay lập tức
Mẹo
Nếu bạn muốn biết thêm thông tin về đề xuất sử dụng bộ ký tự nào trong MySQL, tôi đã viết một blog về điều đó vào năm ngoái. Bạn nên sử dụng bộ ký tự nào trong MySQL?
Một điều nữa là để duy trì người dùng mới, bạn cần gọi số
shell$ sudo grep password /var/log/mysqld.log 2018-11-05T08:05:09.985857Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: KWNfuA!1r:PF5. Phiên hoạt động như một giao dịch ở đây, vì vậy các thay đổi sẽ không được duy trì cho đến khi nó được cam kết rõ ràng
Bước thứ năm – và cuối cùng – là truy vấn dữ liệu vừa lưu
shell$ sudo yum install //dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpmLoaded plugins: langpacks, ulninfo Repository ol7_developer_EPEL is listed more than once in the configuration mysql80-community-release-el7-1.noarch.rpm | 25 kB 00:00:00 Examining /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm: mysql80-community-release-el7-1.noarch Marking /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql80-community-release.noarch 0:el7-1 will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================== Package Arch Version Repository Size ====================================================================================================== Installing: mysql80-community-release noarch el7-1 /mysql80-community-release-el7-1.noarch 31 k Transaction Summary ====================================================================================================== Install 1 Package Total size: 31 k Installed size: 31 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql80-community-release-el7-1.noarch 1/1 Verifying : mysql80-community-release-el7-1.noarch 1/1 Installed: mysql80-community-release.noarch 0:el7-1 Complete!4
Truy vấn tìm người dùng đầu tiên có tên được đặt thành “jesper”. Do biểu tượng cảm xúc cá heo có xu hướng không được hiển thị chính xác trong nhiều trình bao, nên chuỗi byte ở dạng hex cũng được in, vì vậy có thể xác nhận rằng đó thực sự là biểu tượng cảm xúc cá heo đã được truy xuất
Đầu ra của toàn bộ chương trình là
shell$ sudo yum install //dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpmLoaded plugins: langpacks, ulninfo Repository ol7_developer_EPEL is listed more than once in the configuration mysql80-community-release-el7-1.noarch.rpm | 25 kB 00:00:00 Examining /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm: mysql80-community-release-el7-1.noarch Marking /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql80-community-release.noarch 0:el7-1 will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================== Package Arch Version Repository Size ====================================================================================================== Installing: mysql80-community-release noarch el7-1 /mysql80-community-release-el7-1.noarch 31 k Transaction Summary ====================================================================================================== Install 1 Package Total size: 31 k Installed size: 31 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql80-community-release-el7-1.noarch 1/1 Verifying : mysql80-community-release-el7-1.noarch 1/1 Installed: mysql80-community-release.noarch 0:el7-1 Complete!5
Như có thể thấy từ hai dòng cuối cùng, biểu tượng cảm xúc cá heo đã được lưu và truy xuất chính xác
Mã ví dụ hoàn chỉnh
Để hoàn thiện, đây là toàn bộ chương trình ví dụ
shell$ sudo yum install //dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpmLoaded plugins: langpacks, ulninfo Repository ol7_developer_EPEL is listed more than once in the configuration mysql80-community-release-el7-1.noarch.rpm | 25 kB 00:00:00 Examining /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm: mysql80-community-release-el7-1.noarch Marking /var/tmp/yum-root-Ts4OzC/mysql80-community-release-el7-1.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql80-community-release.noarch 0:el7-1 will be installed --> Finished Dependency Resolution Dependencies Resolved ====================================================================================================== Package Arch Version Repository Size ====================================================================================================== Installing: mysql80-community-release noarch el7-1 /mysql80-community-release-el7-1.noarch 31 k Transaction Summary ====================================================================================================== Install 1 Package Total size: 31 k Installed size: 31 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql80-community-release-el7-1.noarch 1/1 Verifying : mysql80-community-release-el7-1.noarch 1/1 Installed: mysql80-community-release.noarch 0:el7-1 Complete!6
Thích sử dụng MySQL, MySQL Connector/Python và SQLAlchemy
tiếng riu ríu
Có liên quan
Giới thiệu về Jesper Krogh
Tôi đã làm việc với cơ sở dữ liệu MySQL từ năm 2006 với tư cách là nhà phát triển SQL, quản trị viên cơ sở dữ liệu và trong hơn tám năm với tư cách là một phần của nhóm Hỗ trợ Oracle MySQL. Tôi đã nhiều lần nói chuyện tại MySQL Connect và Oracle OpenWorld. Tôi đã đóng góp vào lược đồ hệ thống và bốn bài kiểm tra Oracle Certified Professional [OCP] cho MySQL 5. 6 đến 8. 0. Tôi đã viết bốn cuốn sách, tất cả đều được xuất bản tại Apress