Repository navigation
Mysql setup for vapor server
💧 A project built with the Vapor Swift server framework.
To build the project using the Swift Package Manager, run the following command in the terminal from the root of the project:
swift buildTo run the project and start the server, use the following command:
swift runTo execute tests, use the following command:
swift testpull app image from docker
docker pull ghcr.io/rajeshm20/studentappbackend:latestpulling the docker image and successfully running that includes StudentAppBackend app and mysql, try below endpoints using curl
Signup
curl -X POST http://localhost:8080/auth/signup \
-H "Content-Type: application/json" \
-d '{
"name": "Student Name",
"email": "student@example.com",
"password": "password123"
}'Login
curl -X POST http://localhost:8080/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "student@example.com",
"password": "password123"
}'Installing and configuring MySQL on a Mac using Homebrew is a straightforward process. Here's a step-by-step guide:
- Install MySQL
Update Homebrew: First, make sure Homebrew is up to date by running the following command in your terminal:
brew updateInstall MySQL: Next, install MySQL using the brew install command:
brew install mysqlThis will download and install the latest stable version of MySQL.
- Configure MySQL
After the installation is complete, you'll need to start and secure your MySQL server.
Start the MySQL Service: You can start the MySQL server as a background service. There are a couple of ways to do this:
Start it immediately and have it launch at startup:
brew services start mysqlStart it manually each time you want to use it:
mysql.server startYou can check the status of the service at any time with brew services list.
Secure the Installation: It's crucial to run the security script to set a root password, remove anonymous users, and disable remote root login. Run the following command:
mysql_secure_installationYou'll be prompted to follow a series of steps:
Validate Password Component: You can choose to enable or disable this. It enforces strong password policies.
Set the root password: This is a critical step. Choose a strong password.
Remove anonymous users: Type Y to remove them.
Disallow root login remotely: Type Y to prevent remote access for the root user.
Remove test database: Type Y to remove the default 'test' database.
Reload privilege tables: Type Y to apply all the changes.
- Connect to MySQL
Once MySQL is installed and configured, you can connect to it from your terminal.
Connect to the MySQL server: Use the following command and enter the root password you set during the security configuration.
mysql -u root -pThe -u flag specifies the user (root), and the -p flag prompts for the password.
Verify the connection: If successful, you'll see the MySQL prompt, where you can start executing SQL commands. To exit, type exit; and press Enter.
✅ MySQL Command to Create the Database
CREATE DATABASE student_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
🔒 Optional: Create a User with Permissions
CREATE USER 'vapor_user'@'localhost' IDENTIFIED BY 'StrongPassword123!'; GRANT ALL PRIVILEGES ON student_db.* TO 'vapor_user'@'localhost'; FLUSH PRIVILEGES;
Make sure your .env file in the Vapor project root includes:
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=vapor_user
DB_PASSWORD=StrongPassword123!
DB_NAME=student_db
✅ SQL: Create students Table
CREATE TABLE students ( id BINARY(16) NOT NULL PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(150) NOT NULL UNIQUE, passwordHash TEXT NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP );
