MySQL基本簡單操作
學會了安裝Docker
,那麼就將它利用起來。(/滑稽臉)
之前想學習Mysql
(Windows下配置真麻煩),學會了Docker
就方便了,直接使用Docker
建立一個Mysql
服務豈不美滋滋。建立容器的步驟可以看一下分享04的Nginx
的建立過程。
首先檢查一下本地映象。
[root@promote ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
沒有Mysql
的映象,那麼我先來拉取一個Mysql
映象。
先搜尋一下Mysql
的映象。
[root@promote ~]# docker search mysql
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/mysql MySQL is a widely used, open-source relati... 6527 [OK]
docker.io docker.io/mariadb MariaDB is a community-developed fork of M... 2061 [OK]
docker.io docker.io/mysql/mysql-server Optimized MySQL Server Docker images. Crea... 479 [OK]
docker.io docker.io/percona Percona Server is a fork of the MySQL rela... 344 [OK]
docker.io docker.io/zabbix/zabbix-server-mysql Zabbix Server with MySQL database support 106 [OK]
docker.io docker.io/hypriot/rpi-mysql RPi-compatible Docker Image with Mysql 89
docker.io docker.io/centurylink/mysql Image containing mysql. Optimized to be li... 60 [OK]
docker.io docker.io/zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server ... 58 [OK]
docker.io docker.io/1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 36 [OK]
docker.io docker.io/tutum/mysql Base docker image to run a MySQL database ... 32
docker.io docker.io/centos/mysql-57-centos7 MySQL 5.7 SQL database server 31
docker.io docker.io/mysql/mysql-cluster Experimental MySQL Cluster Docker images. ... 30
docker.io docker.io/schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic back... 20 [OK]
docker.io docker.io/bitnami/mysql Bitnami MySQL Docker Image 15 [OK]
docker.io docker.io/zabbix/zabbix-proxy-mysql Zabbix proxy with MySQL database support 15 [OK]
docker.io docker.io/linuxserver/mysql A Mysql container, brought to you by Linux... 14
docker.io docker.io/centos/mysql-56-centos7 MySQL 5.6 SQL database server 8
docker.io docker.io/openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6
docker.io docker.io/circleci/mysql MySQL is a widely used, open-source relati... 5
docker.io docker.io/dsteinkopf/backup-all-mysql backup all DBs in a mysql server 4 [OK]
docker.io docker.io/mysql/mysql-router MySQL Router provides transparent routing ... 2
docker.io docker.io/openzipkin/zipkin-mysql Mirror of https://quay.io/repository/openz... 1
docker.io docker.io/ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 0 [OK]
docker.io docker.io/cloudfoundry/cf-mysql-ci Image used in CI of cf-mysql-release 0
docker.io docker.io/cloudposse/mysql Improved `mysql` service with support for ... 0 [OK]
接著拉取映象到本地,當然是優先官方映象。
[root@promote ~]# docker pull docker.io/mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ...
latest: Pulling from docker.io/library/mysql
683abbb4ea60: Pull complete
0550d17aeefa: Pull complete
7e26605ddd77: Pull complete
9882737bd15f: Pull complete
999c06ab75f6: Pull complete
c71d695f9937: Pull complete
c38f847c1491: Pull complete
5e0cb05a8fc3: Pull complete
c89e3e373fca: Pull complete
fa39a2c9922d: Pull complete
b293d9c897c4: Pull complete
3dc061869740: Pull complete
Digest: sha256:43ed4f8c9d1695e97a39cdfe9475af9096e3723cfb79d820d8da00d61a277a85
Status: Downloaded newer image for docker.io/mysql:latest
拉取成功,現在開始建立Mysql
容器。
[root@promote ~]# docker run -itd --name=mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=000000 docker.io/mysql
30d60b852cf57c5f4e7df36846b10149387bb2b736cecb11f12a2d64a3bdbf43
進入容器。
[root@promote ~]# docker exec -it mysql /bin/bash
root@30d60b852cf5:/#
連線資料庫。
root@30d60b852cf5:/# mysql -uroot -p000000
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 8
Server version: 8.0.11 MySQL Community Server - GPL
Copyright (c) 2000, 2018, 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>
連線資料庫成功!
先學習檢視資料庫。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
然後是建立資料庫。
mysql> create database gubeiqing;
Query OK, 1 row affected (0.02 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| gubeiqing |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
接著使用我剛剛建立好的gubeiqing
資料庫。
mysql> use gubeiqing;
Database changed
進入這個資料庫之後,來看一下有哪些表。
mysql> show tables;
Empty set (0.00 sec)
這個時候的資料表是空的,接著來建立資料表。
mysql> create table gubeiqing1(name varchar(20) not null , age varchar(20) not null);
Query OK, 0 rows affected (0.08 sec)
建表的通用語句語法是:CREATE TABLE table_name (column_name column_type);
.
現在來檢視一下資料表是什麼樣的。
mysql> desc gubeiqing1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20) | NO | | NULL | |
| age | varchar(20) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
可以看到現在gubeiqing1
這個資料表已經有了兩列。但是還沒有資料,所以現在向這個資料表裡新增資料。
mysql> insert into gubeiqing1 (name,age) values (`gbq`,21);
Query OK, 1 row affected (0.04 sec)
如果新增的資料是字元型,那麼必須使用單引號或者雙引號。
現在查詢一下這個表裡所有的內容。
可以看到資料已經被我們新增進去了。
mysql> select * from gubeiqing1;
+------+-----+
| name | age |
+------+-----+
| gbq | 21 |
+------+-----+
1 row in set (0.00 sec)
簡單的查詢語句語法是:SELECT column_name,column_name FROM table_name
。
再來插入幾條(插入時可以不指定列名,但是自己要知道插入資料的順序)。
mysql> insert into gubeiqing1 values (`zhangsan`,20);
Query OK, 1 row affected (0.05 sec)
mysql> insert into gubeiqing1 values (`lisi`,19);
Query OK, 1 row affected (0.03 sec)
檢視一下。
mysql> select * from gubeiqing1;
+----------+-----+
| name | age |
+----------+-----+
| gbq | 21 |
| zhangsan | 20 |
| lisi | 19 |
+----------+-----+
3 rows in set (0.00 sec)
然後學習改資料。
現在我將lisi
的age
欄位,由19
改為22
。
mysql> update gubeiqing1 set age=22 where name=`lisi`;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
改資料的基本簡單語法是:UPDATE table_name SET column_name1=values1,column_name2=values2 [WHERE 條件表示式]
。
再檢視一下資料表。
mysql> select * from gubeiqing1;
+----------+-----+
| name | age |
+----------+-----+
| gbq | 21 |
| zhangsan | 20 |
| lisi | 22 |
+----------+-----+
3 rows in set (0.00 sec)
可以看到lisi
的age
欄位已經被改了。
資料庫基本的增刪改查,已經看了三個,接著來看刪。
將zhangsan
這條資料從資料表中刪除。
mysql> delete from gubeiqing1 where name=`zhangsan`;
Query OK, 1 row affected (0.39 sec)
mysql> select * from gubeiqing1;
+------+-----+
| name | age |
+------+-----+
| gbq | 21 |
| lisi | 22 |
+------+-----+
2 rows in set (0.00 sec)
再來刪除這個表,最後刪除庫。
mysql> drop table gubeiqing1;
Query OK, 0 rows affected (0.13 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql> drop database gubeiqing;
Query OK, 0 rows affected (0.09 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)