My docker essay 14: An example of MySQL operation

This article uses docker to run MySQL and do simple operations.

This article uses MySQL as the official mirror, version 5.7. Download the mirror command:

docker pull mysql:5.7

Run the container with the following command and set the root password to 123456.

run --name hi-mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql

Enter the container:

docker exec -it hi-mysql bash

Enter the mysql command line in the container:

mysql -uroot -p123456

Success will prompt:

mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> 

After MySQL >, you can enter sql statements. The sql statement uses the semicolon ";" as the closing symbol.

View the database:

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

Create a database named mydb:

mysql> CREATE DATABASE mydb;

Select mydb database:

mysql> USE mydb;

Create data table user:

mysql> CREATE TABLE `user` (
 `id` bigint(20) NOT NULL,
 `email` varchar(255) DEFAULT NULL,
 `first_name` varchar(255) DEFAULT NULL,
 `last_name` varchar(255) DEFAULT NULL,
 `username` varchar(255) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

View the user data table field content:

mysql> DESC user;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| id         | bigint(20)   | NO   | PRI | NULL    |       |
| email      | varchar(255) | YES  |     | NULL    |       |
| first_name | varchar(255) | YES  |     | NULL    |       |
| last_name  | varchar(255) | YES  |     | NULL    |       |
| username   | varchar(255) | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+
5 rows in set (0.02 sec)

Insert data into the user table:

mysql> INSERT INTO `user` (`id`, `email`, `first_name`, `last_name`, `username`)
VALUES(0,'li@latelee.org','Late','Lee','latelee');

View the inserted data:

mysql>  SELECT * FROM user;
+----+----------------+------------+-----------+----------+
| id | email          | first_name | last_name | username |
+----+----------------+------------+-----------+----------+
|  0 | li@latelee.org | Late       | Lee       | latelee  |
+----+----------------+------------+-----------+----------+
1 row in set (0.00 sec)

Delete all data from the user table:

mysql> DELETE FROM user;

Delete user data table:

mysql> DROP TABLE user;

Delete the database mydb:

mysql> DROP DATABASE mydb;

Exit the mysql command line:

exit

Exit container:

exit

Keywords: Big Data MySQL Database Docker Oracle

Added by widox on Wed, 31 Jul 2019 01:59:01 +0300