1. 程式人生 > >mysql 案列 1

mysql 案列 1

NSD DBA1 DAY01

1 構建MySQL伺服器

1.1 問題

本案例要求熟悉MySQL官方安裝包的使用,快速構建一臺資料庫伺服器:

  • 安裝MySQL-server、MySQl-client軟體包
  • 修改資料庫使用者root的密碼
  • 確認MySQL服務程式執行、root可控

1.2 方案

本課程將使用64位的RHEL 7作業系統,MySQL資料庫的版本是5.7.17。

訪問http://dev.mysql.com/downloads/mysql/,找到MySQL Community Server下載頁面,平臺選擇“Red Hat Enterprise Linux 7/ Oracle Linux”,然後選擇64位的bundle整合包下載,如圖-1所示。

圖-1

注意:下載MySQL軟體時需要以Oracle網站賬戶登入,如果沒有請根據頁面提示先註冊一個(免費)

1.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:準備工作

1)停止mariadb服務

  1. [[email protected] ~]# systemctl stop mariadb

2)刪除/etc/my.cnf配置檔案

此配置檔案由RHEL自帶的mariadb-libs庫提供:

[[email protected] ~]# rm -rf /etc/my.cnf

3)刪除資料

  1. [[email protected] ~]# rm -rf /var/lib/mysql/*

4)解除安裝軟體包(沒有會顯示未安裝軟體包)

  1. [[email protected] ~]# rpm -e --nodeps mariadb-server mariadb
  2. 警告:/var/log/mariadb/mariadb.log 已另存為/var/log/mariadb/mariadb.log.rpmsave

步驟二:安裝軟體包

1)安裝mysql時可能會缺少某些依賴包,需提前單獨安裝

  1. [[email protected] ~]# yum -y install perl-Data-Dumper perl-JSON perl-Time-HiRes

2)物理機傳輸解壓包給虛擬機器192.168.4.1

  1. [[email protected] ~]# cd 桌面
  2. [[email protected] 桌面]# scp mysql-5.7.17.tar 192.168.4.1:/root/ //給虛擬機器傳包
  3. [email protected]'s password:
  4. mysql-5.7.17.tar 100% 543MB 95.6MB/s 00:05

3)虛擬機器192.168.4.1解壓mysql-5.7.17.tar 整合包

  1. [[email protected] ~]# tar -xvf mysql-5.7.17.tar //解壓mysql整合包
  2. ./mysql-community-client-5.7.17-1.el7.x86_64.rpm
  3. ./mysql-community-common-5.7.17-1.el7.x86_64.rpm
  4. ./mysql-community-devel-5.7.17-1.el7.x86_64.rpm
  5. ./mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
  6. ./mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
  7. ./mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
  8. ./mysql-community-libs-5.7.17-1.el7.x86_64.rpm
  9. ./mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
  10. ./mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
  11. ./mysql-community-server-5.7.17-1.el7.x86_64.rpm
  12. ./mysql-community-test-5.7.17-1.el7.x86_64.rpm

步驟三:啟動MySQL資料庫服務並設定開機自啟

  1. [[email protected] ~]# systemctl start mysqld //啟動mysql服務
  2. [[email protected] ~]# systemctl enable mysqld //設定開機自啟
  3. [[email protected] ~]# systemctl status mysqld //檢視mysql服務狀態
  4. ● mysqld.service - MySQL Server
  5. Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
  6. Active: active (running) since 二 2018-08-28 10:03:24 CST; 8min ago
  7. Docs: man:mysqld(8)
  8. http://dev.mysql.com/doc/refman/en/using-systemd.html
  9. Main PID: 4284 (mysqld)
  10. CGroup: /system.slice/mysqld.service
  11. └─4284 /usr/sbin/mysqld --daemonize --pid-file=/var/r...
  12. 8月 28 10:02:56 localhost.localdomain systemd[1]: Starting MySQ...
  13. 8月 28 10:03:24 localhost.localdomain systemd[1]: Started MySQL...
  14. Hint: Some lines were ellipsized, use -l to show in full.

步驟四:連線MySQL伺服器,修改密碼

檢視隨機生成的root管理密碼

  1. [[email protected] ~]#grep 'temporary password' /var/log/mysqld.log
  2. 2017-04-01T18:10:42.948679Z 1 [Note] A temporary password is generated for [email protected]: mtoa>Av<p6Yk //隨機生成的管理密碼為mtoa>Av<p6Yk

2)使用客戶端命令mysql連線到MySQL伺服器

提示驗證時,填入前一步獲得的隨機密碼,驗證成功後即可進入“mysql> ”環境:

  1. [[email protected] ~]# mysql -u root -p'mtoa>Av<p6Yk'
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 11
  5. Server version: 5.7.17
  6. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql>                                     //登入成功後,進入SQL操作環境

用該密碼登入到服務端後,必須馬上修改密碼,不然會報如下錯誤:

  1. mysql> show databases;
  2. ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

3)執行SET PASSWORD命令修改密碼

這個其實與validate_password_policy的值有關,預設為1,所以剛開始設定的密碼必須符合長度,且必須含有數字,小寫或大寫字母,特殊字元。如果我們不希望密碼設定的那麼複雜,需要修改兩個全域性引數:validate_password_policy與validate_password_length。validate_password_length預設值為8,最小值為4,如果你顯性指定validate_password_length的值小於4,儘管不會報錯,但validate_password_length的值將設為4。

可參考下列指令:

  1. mysql>set global validate_password_policy=0; //只驗證長度
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql>set global validate_password_length=6; //修改密碼長度,預設值是8個字元
  4. Query OK, 0 rows affected (0.00 sec)
  5. mysql> alter user user() identified by "123456"; //修改登陸密碼
  6. Query OK, 0 rows affected (0.00 sec)

上述操作的結果是——更改資料庫使用者root從本機訪問時的密碼,設為123456。

退出“mysql> ”環境,重新登入驗證,必須採用新的密碼才能登入:

  1. mysql> exit                                 //退出 mysql> 環境
  2. Bye
  3. [[email protected] ~]# mysql -u root -p        //重新登入
  4. Enter password:                             //輸入新設定的密碼
  5. Welcome to the MySQL monitor. Commands end with ; or \g.
  6. Your MySQL connection id is 15
  7. Server version: 5.7.17 MySQL Community Server (GPL)
  8. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

2 資料庫基本管理

2.1 問題

本案例要求熟悉MySQL的連線及資料庫表的增刪改查等基本管理操作,主要完成以下幾個方便的操作:

  • 使用mysql命令連線資料庫
  • 練習檢視/刪除/建立庫的相關操作
  • 練習檢視/刪除/建立表的相關操作,表資料參考如表-1所示內容

表-1 測試用表資料

2.2 步驟

實現此案例需要按照如下步驟進行。

步驟一:使用mysql命令連線資料庫

連線MySQL伺服器時,最基本的用法是通過 -u 選項指定使用者名稱、-p指定密碼。密碼可以寫在命令列(如果不寫,則出現互動,要求使用者輸入),當然基於安全考慮一般不推薦這麼做:

  1. [[email protected] ~]# mysql -uroot -p123456         //緊挨著選項,不要空格
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 16
  5. Server version: 5.7.17 MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> exit                                 //退出已登入的mysql> 環境
  12. Bye

預設情況下,msyql命令會連線本機的MySQL服務。但在需要的時候,可以通過 -h 選項指定遠端主機;

  1. [[email protected] ~]# mysql -h 127.0.0.1 –u root –p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 17
  5. Server version: 5.7.17 MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> exit                                 //退出已登入的mysql環境
  12. Bye

步驟二:練習檢視/刪除/建立庫的相關操作

以root使用者登入“mysql> ”環境後,可以執行各種MySQL指令、SQL指令。基本的用法事項如下:

  • 操作指令不區分大小寫(庫名/表名、密碼、變數值等除外)。
  • 每條SQL指令以 ; 結束或分隔。
  • 不支援 Tab 鍵自動補齊。
  • \c 可廢棄當前編寫錯的操作指令。

1)檢視現有的庫

  1. mysql> show databases; //檢視現有的庫
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |                             //資訊概要庫
  6. | mysql |                             //授權庫
  7. | performance_schema |                             //效能結構庫
  8. | sys |                             //系統元資料庫
  9. +--------------------+
  10. 4 rows in set (0.15 sec)

2)切換/使用指定的庫

  1. mysql> use sys; //切換到sys庫
  2. Database changed
  3. mysql> select database();                          //確認當前所在的庫
  4. +------------+
  5. | DATABASE() |
  6. +------------+
  7. | sys |
  8. +------------+
  9. 1 row in set (0.00 sec)

切換到mysql庫:

  1. mysql> use mysql; //切換到mysql庫
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> select database();                         //確認當前所在的庫
  6. +------------+
  7. | DATABASE() |
  8. +------------+
  9. | mysql |
  10. +------------+
  11. 1 row in set (0.00 sec)
  12. 5 rows in set (0.00 sec)

3)新建名為newdb的庫,確認結果:

  1. mysql> create database newdb; //新建名為newdb的庫
  2. Query OK, 1 row affected (0.00 sec)
  3. mysql> show databases;
  4. +--------------------+
  5. | Database |
  6. +--------------------+
  7. | information_schema |
  8. | mydb |                         //新建的mydb庫
  9. | mysql |
  10. | newdb |                         //新建的newdb庫
  11. | performance_schema |
  12. | sys |
  13. +--------------------+
  14. 6 rows in set (0.00 sec)

4)刪除指定的庫

  1. mysql> drop database newdb; //刪除名為newdb的庫
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql> show databases;                         //確認刪除結果,已無newdb庫
  4. +--------------------+
  5. | Database |
  6. +--------------------+
  7. | information_schema |
  8. | mydb |
  9. | mysql |
  10. | performance_schema |
  11. | sys |
  12. +--------------------+
  13. 5 rows in set (0.00 sec)

步驟三:練習檢視/刪除/建立表的相關操作

1)檢視指定的庫裡有哪些表

檢視mysql庫裡有哪些表:

  1. mysql> use mysql;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> show tables;
  6. +---------------------------+
  7. | Tables_in_mysql |
  8. +---------------------------+
  9. | columns_priv |
  10. | db |
  11. | engine_cost |
  12. | event |
  13. | func |
  14. | general_log |
  15. | gtid_executed |
  16. | help_category |
  17. | help_keyword |
  18. | help_relation |
  19. | help_topic |
  20. | innodb_index_stats |
  21. | innodb_table_stats |
  22. | ndb_binlog_index |
  23. | plugin |
  24. | proc |
  25. | procs_priv |
  26. | proxies_priv |
  27. | server_cost |
  28. | servers |
  29. | slave_master_info |
  30. | slave_relay_log_info |
  31. | slave_worker_info |
  32. | slow_log |
  33. | tables_priv |
  34. | time_zone |
  35. | time_zone_leap_second |
  36. | time_zone_name |
  37. | time_zone_transition |
  38. | time_zone_transition_type |
  39. | user | //存放資料庫使用者的表
  40. +---------------------------+
  41. 31 rows in set (0.00 sec)

2)檢視指定表的欄位結構

當前庫為mysql,檢視columns_priv表的結構,以列表形式展現:

  1. mysql> desc columns_priv\G         //查看錶結構,以列表形式展現,末尾不用分號
  2. *************************** 1. row ***************************
  3. Field: Host
  4. Type: char(60)
  5. Null: NO
  6. Key: PRI
  7. Default:
  8. Extra:
  9. *************************** 2. row ***************************
  10. Field: Db
  11. Type: char(64)
  12. Null: NO
  13. Key: PRI
  14. Default:
  15. Extra:
  16. *************************** 3. row ***************************
  17. Field: User
  18. Type: char(32)
  19. Null: NO
  20. Key: PRI
  21. Default:
  22. Extra:
  23. *************************** 4. row ***************************
  24. Field: Table_name
  25. Type: char(64)
  26. Null: NO
  27. Key: PRI
  28. Default:
  29. Extra:
  30. *************************** 5. row ***************************
  31. Field: Column_name
  32. Type: char(64)
  33. Null: NO
  34. Key: PRI
  35. Default:
  36. Extra:
  37. *************************** 6. row ***************************
  38. Field: Timestamp
  39. Type: timestamp
  40. Null: NO
  41. Key:
  42. Default: CURRENT_TIMESTAMP
  43. Extra: on update CURRENT_TIMESTAMP
  44. *************************** 7. row ***************************
  45. Field: Column_priv
  46. Type: set('Select','Insert','Update','References')
  47. Null: NO
  48. Key:
  49. Default:
  50. Extra:
  51. 7 rows in set (0.01 sec)

檢視columns_priv表的結構,以表格形式展現:

  1. mysql> desc columns_priv;         //查看錶結構,以表格形式展現末尾需要有分號
  2. +-------------+----------------------------------------------+------+-----+-------------------+-----------------------------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +-------------+----------------------------------------------+------+-----+-------------------+-----------------------------+
  5. | Host | char(60) | NO | PRI | | |
  6. | Db | char(64) | NO | PRI | | |
  7. | User | char(32) | NO | PRI | | |
  8. | Table_name | char(64) | NO | PRI | | |
  9. | Column_name | char(64) | NO | PRI | | |
  10. | Timestamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
  11. | Column_priv | set('Select','Insert','Update','References') | NO | | | |
  12. +-------------+----------------------------------------------+------+-----+-------------------+-----------------------------+
  13. 7 rows in set (0.00 sec)

上述操作中,當引用非當前庫中的表時,可以用“庫名.表名”的形式。比如,切換為mysql庫再執行“desc columns_priv;”,與以下操作的效果是相同的:

  1. mysql> desc mysql.columns_priv;
  2. +-------------+----------------------------------------------+------+-----+-------------------+-----------------------------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +-------------+----------------------------------------------+------+-----+-------------------+-----------------------------+
  5. | Host | char(60) | NO | PRI | | |
  6. | Db | char(64) | NO | PRI | | |
  7. | User | char(16) | NO | PRI | | |
  8. | Table_name | char(64) | NO | PRI | | |
  9. | Column_name | char(64) | NO | PRI | | |
  10. | Timestamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
  11. | Column_priv | set('Select','Insert','Update','References') | NO | | | |
  12. +-------------+----------------------------------------------+------+-----+-------------------+-----------------------------+
  13. 7 rows in set (0.00 sec)

3)在test庫中建立一個名為pwlist的表

包括name、password兩列,其中name列作為主鍵。兩個欄位值均不允許為空,其中密碼列賦予預設空值,相關操作如下所述。

切換到mydb庫:

  1. mysql> use mydb;
  2. Database changed

新建pwlist表:

  1. mysql> create table pwlist(
  2. -> name char(16) not null,
  3. -> password char(48)default '',
  4. -> primary key(name)
  5. -> );
  6. Query OK, 0 rows affected (0.38 sec)

確認新建立的表:

  1. mysql> show tables;
  2. +----------------+
  3. | Tables_in_mydb |
  4. +----------------+
  5. | pwlist |                                 //新建的pwlist表
  6. +----------------+
  7. 1 rows in set (0.01 sec)

檢視pwlist表的欄位結構:

  1. mysql> desc pwlist;
  2. +----------+----------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +----------+----------+------+-----+---------+-------+
  5. | name | char(16) | NO | PRI | NULL | |
  6. | password | char(48) | YES | | | |
  7. +----------+----------+------+-----+---------+-------+
  8. 2 rows in set (0.01 sec)

4)刪除指定的表

刪除當前庫中的pwlist表:

  1. mysql> drop table pwlist;
  2. Query OK, 0 rows affected (0.01 sec)

確認刪除結果:

  1. mysql> show tables;
  2. Empty set (0.00 sec)

5)在mydb庫中建立一個學員表

表格結構及資料內容如表-1所示。

在MySQL表記憶體儲中文資料時,需要更改字符集(預設為latin1不支援中文),以便MySQL支援儲存中文資料記錄;比如,可以在建立庫或表的時候,手動新增“DEFAULT CHARSET=utf8”來更改字符集。

根據上述表格結構,建立支援中文的student表:

  1. mysql> CREATE TABLE mydb.student(
  2. -> 學號 char(9) NOT NULL,
  3. -> 姓名 varchar(4) NOT NULL,
  4. -> 性別 enum('男','女') NOT NULL,
  5. -> 手機號 char(11) DEFAULT '',
  6. -> 通訊地址 varchar(64),
  7. -> PRIMARY KEY(學號)
  8. -> ) DEFAULT CHARSET=utf8;                 //手工指定字符集,採用utf8
  9. Query OK, 0 rows affected (0.31sec)

檢視student表的欄位結構:

  1. mysql> DESC mydb.student;
  2. +--------------+-------------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +--------------+-------------------+------+-----+---------+-------+
  5. | 學號 | char(9) | NO | PRI | NULL | |
  6. | 姓名 | varchar(4) | NO | | NULL | |
  7. | 性別 | enum('男','女') | NO | | NULL | |
  8. | 手機號 | char(11) | YES | | | |
  9. | 通訊地址 | varchar(64) | YES | | NULL | |
  10. +--------------+-------------------+------+-----+---------+-------+
  11. 5 rows in set (0.00 sec)

檢視student表的實際建立指令:

  1. mysql> SHOW CREATE TABLE mydb.student;
  2. +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  3. |Table |Create Table |
  4. +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  5. | student | CREATE TABLE `student` (
  6. `學號` char(9) NOT NULL,
  7. `姓名` varchar(4) NOT NULL,
  8. `性別` enum('男','女') NOT NULL,
  9. `手機號` char(11) DEFAULT '',
  10. `通訊地址` varchar(64) DEFAULT NULL,
  11. PRIMARY KEY (`學號`)
  12. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
  13. +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  14. 1 row in set (0.00 sec)

注意:若要修改MySQL服務的預設字符集,可以更改伺服器的my.cnf配置檔案,新增character_set_server=utf8 配置,然後重啟資料庫服務。

  1. [[email protected] ~]# vim /etc/my.cnf                         //修改執行服務配置
  2. [mysqld]
  3. .. ..
  4. character_set_server=utf8
  5. [[email protected] ~]# systemctl restart mysqld                 //重啟服務
  6. .. ..
  7. [[email protected] ~]# mysql –u root -p
  8. Enter password:
  9. .. ..
  10. mysql> SHOW VARIABLES LIKE 'character%';                 //確認更改結果
  11. +--------------------------+----------------------------+
  12. | Variable_name | Value |
  13. +--------------------------+----------------------------+
  14. | character_set_client | utf8 |
  15. | character_set_connection | utf8 |
  16. | character_set_database | utf8 |
  17. | character_set_filesystem | binary |
  18. | character_set_results | utf8 |
  19. | character_set_server | utf8 |
  20. | character_set_system | utf8 |
  21. | character_sets_dir | /usr/share/mysql/charsets/ |
  22. +--------------------------+----------------------------+
  23. 8 rows in set (0.03 sec)

3 MySQL 資料型別

3.1 問題

  • 本案例要求熟悉MySQL的欄位資料型別、表結構,各種時間函式的使用的使用,完成以下任務操作:
  • 在studb庫裡建立stuinfo表,表結構、欄位型別自定義
  • 表結構自定義,併合理使用資料型別
  • 練習各種時間函式的使用

3.2 步驟

實現此案例需要按照如下步驟進行。

步驟一:建立studb庫、stuinfo表

1)新建studb庫,並切換到studb庫

  1. mysql> CREATE DATABASE studb;
  2. Query OK, 1 row affected (0.00 sec)
  3. mysql> USE studb;
  4. Database changed

2)新建stuinfo表

假定stuinfo表用來記錄每個家庭成員的姓名(name)、性別(gender)、出生日期(birth)、職業(job)、與戶主關係(relation)。

  1. mysql> CREATE TABLE stuinfo (
  2. -> name varchar(16) NOT NULL,
  3. -> gender enum('male','femal') DEFAULT 'male',
  4. -> birth date NOT NULL,
  5. -> job varchar(16) DEFAULT '',
  6. -> relation varchar(24) NOT NULL,
  7. -> PRIMARY KEY(name)
  8. -> );
  9. Query OK, 0 rows affected (0.61sec)

檢視stuinfo表的欄位結構:

  1. mysql> DESC stuinfo;
  2. +----------+----------------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +----------+----------------------+------+-----+---------+-------+
  5. | name | varchar(16) | NO | PRI | NULL | |
  6. | gender | enum('male','femal') | YES | | male | |
  7. | birth | date | NO | | NULL | |
  8. | job | varchar(16) | YES | | | |
  9. | relation | varchar(24) | NO | | NULL | |
  10. +----------+----------------------+------+-----+---------+-------+
  11. 5 rows in set (0.00 sec)

步驟二:練習各種時間函式的使用

1)使用now()檢視當前的日期和時間

  1. mysql> SELECT now();
  2. +---------------------+
  3. | now() |
  4. +---------------------+
  5. | 2017-04-02 04:02:42 |
  6. +---------------------+
  7. 1 row in set (0.00 sec)

2)使用sysdate()檢視系統日期和時間

  1. mysql> SELECT sysdate();
  2. +---------------------+
  3. | sysdate() |
  4. +---------------------+
  5. | 2017-04-02 04:03:21 |
  6. +---------------------+
  7. 1 row in set (0.00 sec)

3)使用curdate()獲得當前的日期,不含時間

  1. mysql> SELECT curdate();
  2. +------------+
  3. | curdate() |
  4. +------------+
  5. | 2017-04-02 |
  6. +------------+
  7. 1 row in set (0.00 sec)

4)使用curtime()獲得當前的時間,不含日期

  1. mysql> SELECT curtime();
  2. +-----------+
  3. | curtime() |
  4. +-----------+
  5. | 04:04:55 |
  6. +-----------+
  7. 1 row in set (0.00 sec)

5)分別獲取當前日期時間中的年份、月份、日

  1. mysql> SELECT year(now()),month(now()),day(now());
  2. +-------------+--------------+------------+
  3. | year(now()) | month(now()) | day(now()) |
  4. +-------------+--------------+------------+
  5. | 2017 | 4 | 2 |
  6. +-------------+--------------+------------+
  7. 1 row in set (0.00 sec)

6)獲取系統日期時間中的月份、日

  1. mysql> SELECT month(sysdate()),day(sysdate());
  2. +------------------+----------------+
  3. | month(sysdate()) | day(sysdate()) |
  4. +------------------+----------------+
  5. | 4 | 2 |
  6. +------------------+----------------+
  7. 1 row in set (0.00 sec)

7)獲取系統日期時間中的時刻

  1. mysql> SELECT time(sysdate());
  2. +-----------------+
  3. | time(sysdate()) |
  4. +-----------------+
  5. | 04:06:08 |
  6. +-----------------+
  7. 1 row in set (0.00