1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00
examples/databases/mysql/README.md

60 lines
1.5 KiB
Markdown
Raw Permalink Normal View History

2023-07-17 18:55:17 +02:00
# MySQL
2023-07-18 02:01:26 +02:00
This RESTful Actix Web API illustrates how to use a MySQL database as a data source for various endpoints.
2023-07-17 18:55:17 +02:00
2023-07-18 02:01:26 +02:00
You'll need to have a MySQL (or compatible) server running on your machine to test this example.
2023-07-18 02:01:26 +02:00
## Usage
2023-07-18 02:01:26 +02:00
All the following commands assume that your current working directory is _this_ directory. I.e.:
2023-07-17 18:55:17 +02:00
2023-07-18 02:01:26 +02:00
```console
$ pwd
.../databases/mysql
```
2023-07-18 02:01:26 +02:00
1. Create database and tables:
2023-07-18 02:01:26 +02:00
The `sql` directory contains the SQL files used for database setup:
2023-07-18 02:01:26 +02:00
```sh
2023-07-18 02:09:09 +02:00
mysql -u root -p < sql/0_create_database.sql
mysql -u root -p my_bank < sql/1_bank_details.sql
mysql -u root -p my_bank < sql/2_branch_details.sql
mysql -u root -p my_bank < sql/3_teller_details.sql
mysql -u root -p my_bank < sql/4_customer_details.sql
```
2023-07-18 02:01:26 +02:00
For each step you will be prompted for the root user's password. If there's no password set on the root use, just hit enter again.
2023-07-17 18:55:17 +02:00
2023-07-18 02:01:26 +02:00
1. Create a `.env` file in this this directory:
```ini
SERVER_ADDR=127.0.0.1:8080
2023-07-18 02:01:26 +02:00
MYSQL_USER=root
MYSQL_PASSWORD=<password>
MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306
MYSQL_DBNAME=my_bank
```
2023-07-17 18:55:17 +02:00
Update "MYSQL_USER" and "MYSQL_PASSWORD" values with the correct MySQL user/password.
2023-07-17 18:55:17 +02:00
1. Run the server:
2023-07-18 02:01:26 +02:00
```sh
cargo run
```
2023-07-18 02:01:26 +02:00
1. Using a different terminal send requests to the running server. For example, using [HTTPie]:
2023-07-17 18:55:17 +02:00
2023-07-18 02:01:26 +02:00
```sh
http POST :8080/bank bank_name="Bank ABC" country="Kenya"
http :8080/bank
```
2023-07-18 02:01:26 +02:00
See [the API documentation pages](./apis/) for more info.
2023-07-18 02:01:26 +02:00
[HTTPie]: https://httpie.io/cli