1
0
mirror of https://github.com/actix/examples synced 2024-12-05 03:01:55 +01:00
examples/databases/mysql
2023-10-29 01:18:40 +01:00
..
apis improve mysql readme 2023-07-18 01:01:26 +01:00
sql simplify sql create tables 2023-07-18 01:09:09 +01:00
src update dotenv::dotenv references 2023-10-29 01:18:40 +01:00
Cargo.toml migrate to dotenvy 2023-10-29 01:17:49 +01:00
README.md simplify sql create tables 2023-07-18 01:09:09 +01:00

MySQL

This RESTful Actix Web API illustrates how to use a MySQL database as a data source for various endpoints.

You'll need to have a MySQL (or compatible) server running on your machine to test this example.

Usage

All the following commands assume that your current working directory is this directory. I.e.:

$ pwd
.../databases/mysql
  1. Create database and tables:

    The sql directory contains the SQL files used for database setup:

    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
    

    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.

  2. Create a .env file in this this directory:

    SERVER_ADDR=127.0.0.1:8080
    MYSQL_USER=root
    MYSQL_PASSWORD=<password>
    MYSQL_HOST=127.0.0.1
    MYSQL_PORT=3306
    MYSQL_DBNAME=my_bank
    

    Update "MYSQL_USER" and "MYSQL_PASSWORD" values with the correct MySQL user/password.

  3. Run the server:

    cargo run
    
  4. Using a different terminal send requests to the running server. For example, using HTTPie:

    http POST :8080/bank bank_name="Bank ABC" country="Kenya"
    
    http :8080/bank
    

    See the API documentation pages for more info.