MySQL

This section provides an overview of using MySQL.

We will create a Local MySQL Database. This will be used in CSCN-326.

Create a Local MySQL Database

Pre-requisites

Steps

  1. Open a terminal and run the following command to pull the latest SQL Server image from Docker Hub

    docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=VeryStr0ngP@ssw0rd -d mysql:latest
    
  2. Check Docker to make sure the container is running

Connecting to the Local MySQL Database

  1. Open Azure Data Studio and click New Connection
  2. Change Connection Type to MySQL
  3. Enter localhost into Server
  4. Enter root into User name
  5. Enter VeryStr0ngP@ssw0rd into Password
  6. Click Remember Password
  7. Click Advanced
  8. Enter 3306 into Port
  9. Click Connect
    Username: root
    Password: VeryStr0ngP@ssw0rd

You should now be connected to your Local MySQL Database.

From here, you can create a new database, create tables, and run queries. We recommend looking at the Azure Data Studio Documentation to learn more about Azure Data Studio.


MySQL CLI Development

Install MySQL CLI

If you are using a Mac, you can install MySQL CLI by using the following command:

brew install mysql

Connect to Local MySQL Database by CLI

Open a terminal window and connect to your Local MySQL Database server using the following command:

mysql -h localhost -u root -p [DATABASE_NAME]

You will be prompted to enter your password.

Enter password:

Enter the password you used when you created the server.

Now, you are connected to your Local MySQL Database.

Run MySQL Scripts by CLI

To run MySQL scripts by CLI, you can use the following command:

mysql -h localhost -u root -p [DATABASE_NAME] < [SCRIPT_NAME].sql

You will be prompted to enter your password.

Enter password:

Enter the password you used when you created the server.

Now, your script will be executed, and you will see the results in the terminal window.


MySQL & PHP Development

Pre-requisites

Connection String for MySQL

$connection = mysqli_connect('localhost:3306', 'root', 'VeryStr0ngP@ssw0rd', 'Labs')

Run PHP Scripts

  1. Open the PHP file you want to run in VSCode
  2. Click the Go Live button in the bottom right corner of the window