Top
NSD DBA1 DAY01
- 構建MySQL服務器
- 數據庫基本管理
- MySQL 數據類型
1 構建MySQL服務器
1.1 問題
本案例要求熟悉MySQL官方安裝包的使用,快速構建一臺數據庫服務器:html
- 安裝MySQL-server、MySQl-client軟件包
- 修改數據庫用戶root的密碼
- 確認MySQL服務程序運行、root可控
1.2 方案
本課程將使用64位的RHEL 7操做系統,MySQL數據庫的版本是5.7.17。node
訪問http://dev.mysql.com/downloads/mysql/,找到MySQL Community Server下載頁面,平臺選擇「Red Hat Enterprise Linux 7/ Oracle Linux」,而後選擇64位的bundle整合包下載,如圖-1所示。mysql
圖-1sql
注意:下載MySQL軟件時須要以Oracle網站帳戶登陸,若是沒有請根據頁面提示先註冊一個(免費)數據庫
1.3 步驟
實現此案例須要按照以下步驟進行。vim
步驟一:準備工做安全
1)中止mariadb服務服務器
- [root@localhost ~]# systemctl stop mariadb
2)刪除/etc/my.cnf配置文件dom
此配置文件由RHEL自帶的mariadb-libs庫提供:ide
[root@localhost ~]# rm -rf /etc/my.cnf
3)刪除數據
- [root@localhost ~]# rm -rf /var/lib/mysql
4)卸載軟件包(沒有會顯示未安裝軟件包)
- [root@localhost ~]# rpm -e --nodeps mariadb-server mariadb
- 警告:/var/log/mariadb/mariadb.log 已另存爲/var/log/mariadb/mariadb.log.rpmsave
步驟二:安裝軟件包
1)安裝mysql時可能會缺乏某些依賴包,需提早單獨安裝
- [root@localhost ~]# yum -y install perl-Data-Dumper perl-JSON perl-Time-HiRes
2)物理機傳輸解壓包給虛擬機192.168.4.1
- [root@room9pc01 ~]# cd 桌面
- [root@room9pc01 桌面]# scp mysql-5.7.17.tar 192.168.4.1:/root/
- root@192.168.4.1's password:
- mysql-5.7.17.tar 100% 543MB 95.6MB/s 00:05
3)虛擬機192.168.4.1解壓mysql-5.7.17.tar 整合包
- [root@localhost ~]# tar -xvf mysql-5.7.17.tar
- ./mysql-community-client-5.7.17-1.el7.x86_64.rpm
- ./mysql-community-common-5.7.17-1.el7.x86_64.rpm
- ./mysql-community-devel-5.7.17-1.el7.x86_64.rpm
- ./mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
- ./mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
- ./mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
- ./mysql-community-libs-5.7.17-1.el7.x86_64.rpm
- ./mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
- ./mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
- ./mysql-community-server-5.7.17-1.el7.x86_64.rpm
- ./mysql-community-test-5.7.17-1.el7.x86_64.rpm
步驟三:啓動MySQL數據庫服務並設置開機自啓
- [root@localhost ~]# systemctl start mysqld
- [root@localhost ~]# systemctl enable mysqld
- [root@localhost ~]# systemctl status mysqld
- ● mysqld.service - MySQL Server
- Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
- Active: active (running) since 二 2018-08-28 10:03:24 CST; 8min ago
- Docs: man:mysqld(8)
- http:
- Main PID: 4284 (mysqld)
- CGroup: /system.slice/mysqld.service
- └─4284 /usr/sbin/mysqld --daemonize --pid-file=/var/r...
- 8月 28 10:02:56 localhost.localdomain systemd[1]: Starting MySQ...
- 8月 28 10:03:24 localhost.localdomain systemd[1]: Started MySQL...
- Hint: Some lines were ellipsized, use -l to show in full.
步驟四:鏈接MySQL服務器,修改密碼
查看隨機生成的root管理密碼
- [root@localhost ~]#grep 'temporary password' /var/log/mysqld.log
- 2017-04-01T18:10:42.948679Z 1 [Note] A temporary password is generated for root@localhost: mtoa>Av<p6Yk
2)使用客戶端命令mysql鏈接到MySQL服務器
提示驗證時,填入前一步得到的隨機密碼,驗證成功後便可進入「mysql> 」環境:
- [root@localhost ~]# mysql -u root -p'mtoa>Av<p6Yk'
- 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.17
- Copyright (c) 2000, 2016, 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;
- 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。
可參考下列指令:
- mysql>set global validate_password_policy=0;
- Query OK, 0 rows affected (0.00 sec)
- mysql>set global validate_password_length=6;
- Query OK, 0 rows affected (0.00 sec)
- mysql> alter user user() identified by "123456";
- Query OK, 0 rows affected (0.00 sec)
上述操做的結果是——更改數據庫用戶root從本機訪問時的密碼,設爲123456。
退出「mysql> 」環境,從新登陸驗證,必須採用新的密碼才能登入:
- mysql> exit
- Bye
- [root@localhost ~]# mysql -u root -p
- Enter password:
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 15
- Server version: 5.7.17 MySQL Community Server (GPL)
- Copyright (c) 2000, 2016, 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.
2 數據庫基本管理
2.1 問題
本案例要求熟悉MySQL的鏈接及數據庫表的增刪改查等基本管理操做,主要完成如下幾個方便的操做:
- 使用mysql命令鏈接數據庫
- 練習查看/刪除/建立庫的相關操做
- 練習查看/刪除/建立表的相關操做,表數據參考如表-1所示內容
表-1 測試用表數據
2.2 步驟
實現此案例須要按照以下步驟進行。
步驟一:使用mysql命令鏈接數據庫
鏈接MySQL服務器時,最基本的用法是經過 -u 選項指定用戶名、-p指定密碼。密碼能夠寫在命令行(若是不寫,則出現交互,要求用戶輸入),固然基於安全考慮通常不推薦這麼作:
- [root@dbsvr1 ~]# mysql -uroot -p123456
- 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 16
- Server version: 5.7.17 MySQL Community Server (GPL)
- Copyright (c) 2000, 2016, 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> exit
- Bye
默認狀況下,msyql命令會鏈接本機的MySQL服務。但在須要的時候,能夠經過 -h 選項指定遠程主機;
- [root@dbsvr1 ~]# mysql -h 127.0.0.1 –u root –p
- Enter password:
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 17
- Server version: 5.7.17 MySQL Community Server (GPL)
- Copyright (c) 2000, 2016, 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> exit
- Bye
步驟二:練習查看/刪除/建立庫的相關操做
以root用戶登入「mysql> 」環境後,能夠執行各類MySQL指令、SQL指令。基本的用法事項以下:
- 操做指令不區分大小寫(庫名/表名、密碼、變量值等除外)。
- 每條SQL指令以 ; 結束或分隔。
- 不支持 Tab 鍵自動補齊。
- \c 可廢棄當前編寫錯的操做指令。
1)查看現有的庫
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | sys |
- +--------------------+
- 4 rows in set (0.15 sec)
2)切換/使用指定的庫
- mysql> use sys;
- Database changed
- mysql> select database();
- +------------+
- | DATABASE() |
- +------------+
- | sys |
- +------------+
- 1 row in set (0.00 sec)
切換到mysql庫:
- mysql> use mysql;
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
- Database changed
- mysql> select database();
- +------------+
- | DATABASE() |
- +------------+
- | mysql |
- +------------+
- 1 row in set (0.00 sec)
- 5 rows in set (0.00 sec)
3)新建名爲newdb的庫,確認結果:
- mysql> create database newdb;
- Query OK, 1 row affected (0.00 sec)
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mydb |
- | mysql |
- | newdb |
- | performance_schema |
- | sys |
- +--------------------+
- 6 rows in set (0.00 sec)
4)刪除指定的庫
- mysql> drop database newdb;
- Query OK, 0 rows affected (0.01 sec)
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mydb |
- | mysql |
- | performance_schema |
- | sys |
- +--------------------+
- 5 rows in set (0.00 sec)
步驟三:練習查看/刪除/建立表的相關操做
1)查看指定的庫裏有哪些表
查看mysql庫裏有哪些表:
- mysql> use mysql;
- Reading table information for completion of table and column names
- You can turn off this feature to get a quicker startup with -A
- Database changed
- mysql> show tables;
- +---------------------------+
- | Tables_in_mysql |
- +---------------------------+
- | columns_priv |
- | db |
- | engine_cost |
- | event |
- | func |
- | general_log |
- | gtid_executed |
- | help_category |
- | help_keyword |
- | help_relation |
- | help_topic |
- | innodb_index_stats |
- | innodb_table_stats |
- | ndb_binlog_index |
- | plugin |
- | proc |
- | procs_priv |
- | proxies_priv |
- | server_cost |
- | servers |
- | slave_master_info |
- | slave_relay_log_info |
- | slave_worker_info |
- | slow_log |
- | tables_priv |
- | time_zone |
- | time_zone_leap_second |
- | time_zone_name |
- | time_zone_transition |
- | time_zone_transition_type |
- | user |
- +---------------------------+
- 31 rows in set (0.00 sec)
2)查看指定表的字段結構
當前庫爲mysql,查看columns_priv表的結構,以列表形式展示:
- mysql> desc columns_priv\G
- *************************** 1. row ***************************
- Field: Host
- Type: char(60)
- Null: NO
- Key: PRI
- Default:
- Extra:
- *************************** 2. row ***************************
- Field: Db
- Type: char(64)
- Null: NO
- Key: PRI
- Default:
- Extra:
- *************************** 3. row ***************************
- Field: User
- Type: char(32)
- Null: NO
- Key: PRI
- Default:
- Extra:
- *************************** 4. row ***************************
- Field: Table_name
- Type: char(64)
- Null: NO
- Key: PRI
- Default:
- Extra:
- *************************** 5. row ***************************
- Field: Column_name
- Type: char(64)
- Null: NO
- Key: PRI
- Default:
- Extra:
- *************************** 6. row ***************************
- Field: Timestamp
- Type: timestamp
- Null: NO
- Key:
- Default: CURRENT_TIMESTAMP
- Extra: on update CURRENT_TIMESTAMP
- *************************** 7. row ***************************
- Field: Column_priv
- Type: set('Select','Insert','Update','References')
- Null: NO
- Key:
- Default:
- Extra:
- 7 rows in set (0.01 sec)
查看columns_priv表的結構,以表格形式展示:
- mysql> desc columns_priv;
- +-------------+----------------------------------------------+------+-----+-------------------+-----------------------------+
- | Field | Type | Null | Key | Default | Extra |
- +-------------+----------------------------------------------+------+-----+-------------------+-----------------------------+
- | Host | char(60) | NO | PRI | | |
- | Db | char(64) | NO | PRI | | |
- | User | char(32) | NO | PRI | | |
- | Table_name | char(64) | NO | PRI | | |
- | Column_name | char(64) | NO | PRI | | |
- | Timestamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
- | Column_priv | set('Select','Insert','Update','References') | NO | | | |
- +-------------+----------------------------------------------+------+-----+-------------------+-----------------------------+
- 7 rows in set (0.00 sec)
上述操做中,當引用非當前庫中的表時,能夠用「庫名.表名」的形式。好比,切換爲mysql庫再執行「desc columns_priv;」,與如下操做的效果是相同的:
- mysql> desc mysql.columns_priv;
- +-------------+----------------------------------------------+------+-----+-------------------+-----------------------------+
- | Field | Type | Null | Key | Default | Extra |
- +-------------+----------------------------------------------+------+-----+-------------------+-----------------------------+
- | Host | char(60) | NO | PRI | | |
- | Db | char(64) | NO | PRI | | |
- | User | char(16) | NO | PRI | | |
- | Table_name | char(64) | NO | PRI | | |
- | Column_name | char(64) | NO | PRI | | |
- | Timestamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
- | Column_priv | set('Select','Insert','Update','References') | NO | | | |
- +-------------+----------------------------------------------+------+-----+-------------------+-----------------------------+
- 7 rows in set (0.00 sec)
3)在test庫中建立一個名爲pwlist的表
包括name、password兩列,其中name列做爲主鍵。兩個字段值均不容許爲空,其中密碼列賦予默認空值,相關操做以下所述。
切換到mydb庫:
- mysql> use mydb;
- Database changed
新建pwlist表:
- mysql> create table pwlist(
- -> name char(16) not null,
- -> password char(48)default '',
- -> primary key(name)
- -> );
- Query OK, 0 rows affected (0.38 sec)
確認新建立的表:
- mysql> show tables;
- +----------------+
- | Tables_in_mydb |
- +----------------+
- | pwlist |
- +----------------+
- 1 rows in set (0.01 sec)
查看pwlist表的字段結構:
- mysql> desc pwlist;
- +----------+----------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+----------+------+-----+---------+-------+
- | name | char(16) | NO | PRI | NULL | |
- | password | char(48) | YES | | | |
- +----------+----------+------+-----+---------+-------+
- 2 rows in set (0.01 sec)
4)刪除指定的表
刪除當前庫中的pwlist表:
- mysql> drop table pwlist;
- Query OK, 0 rows affected (0.01 sec)
確認刪除結果:
- mysql> show tables;
- Empty set (0.00 sec)
5)在mydb庫中建立一個學員表
表格結構及數據內容如表-1所示。
在MySQL表內存儲中文數據時,須要更改字符集(默認爲latin1不支持中文),以便MySQL支持存儲中文數據記錄;好比,能夠在建立庫或表的時候,手動添加「DEFAULT CHARSET=utf8」來更改字符集。
根據上述表格結構,建立支持中文的student表:
- mysql> CREATE TABLE mydb.student(
- -> 學號 char(9) NOT NULL,
- -> 姓名 varchar(4) NOT NULL,
- -> 性別 enum('男','女') NOT NULL,
- -> 手機號 char(11) DEFAULT '',
- -> 通訊地址 varchar(64),
- -> PRIMARY KEY(學號)
- -> ) DEFAULT CHARSET=utf8;
- Query OK, 0 rows affected (0.31sec)
查看student表的字段結構:
- mysql> DESC mydb.student;
- +--------------+-------------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +--------------+-------------------+------+-----+---------+-------+
- | 學號 | char(9) | NO | PRI | NULL | |
- | 姓名 | varchar(4) | NO | | NULL | |
- | 性別 | enum('男','女') | NO | | NULL | |
- | 手機號 | char(11) | YES | | | |
- | 通訊地址 | varchar(64) | YES | | NULL | |
- +--------------+-------------------+------+-----+---------+-------+
- 5 rows in set (0.00 sec)
查看student表的實際建立指令:
- mysql> SHOW CREATE TABLE mydb.student;
- +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- |Table |Create Table |
- +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | student | CREATE TABLE `student` (
- `學號` char(9) NOT NULL,
- `姓名` varchar(4) NOT NULL,
- `性別` enum('男','女') NOT NULL,
- `手機號` char(11) DEFAULT '',
- `通訊地址` varchar(64) DEFAULT NULL,
- PRIMARY KEY (`學號`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
- +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- 1 row in set (0.00 sec)
注意:若要修改MySQL服務的默認字符集,能夠更改服務器的my.cnf配置文件,添加character_set_server=utf8 配置,而後重啓數據庫服務。
- [root@dbsvr1 ~]# vim /etc/my.cnf
- [mysqld]
- .. ..
- character_set_server=utf8
- [root@dbsvr1 ~]# systemctl restart mysqld
- .. ..
- [root@dbsvr1 ~]# mysql –u root -p
- Enter password:
- .. ..
- mysql> SHOW VARIABLES LIKE 'character%';
- +--------------------------+----------------------------+
- | Variable_name | Value |
- +--------------------------+----------------------------+
- | character_set_client | utf8 |
- | character_set_connection | utf8 |
- | character_set_database | utf8 |
- | character_set_filesystem | binary |
- | character_set_results | utf8 |
- | character_set_server | utf8 |
- | character_set_system | utf8 |
- | character_sets_dir | /usr/share/mysql/charsets/ |
- +--------------------------+----------------------------+
- 8 rows in set (0.03 sec)
3 MySQL 數據類型
3.1 問題
- 本案例要求熟悉MySQL的字段數據類型、表結構,各類時間函數的使用的使用,完成如下任務操做:
- 在studb庫裏建立stuinfo表,表結構、字段類型自定義
- 表結構自定義,併合理使用數據類型
- 練習各類時間函數的使用
3.2 步驟
實現此案例須要按照以下步驟進行。
步驟一:建立studb庫、stuinfo表
1)新建studb庫,並切換到studb庫
- mysql> CREATE DATABASE studb;
- Query OK, 1 row affected (0.00 sec)
- mysql> USE studb;
- Database changed
2)新建stuinfo表
假定stuinfo表用來記錄每一個家庭成員的姓名(name)、性別(gender)、出生日期(birth)、職業(job)、與戶主關係(relation)。
- mysql> CREATE TABLE stuinfo (
- -> name varchar(16) NOT NULL,
- -> gender enum('male','femal') DEFAULT 'male',
- -> birth date NOT NULL,
- -> job varchar(16) DEFAULT '',
- -> relation varchar(24) NOT NULL,
- -> PRIMARY KEY(name)
- -> );
- Query OK, 0 rows affected (0.61sec)
查看stuinfo表的字段結構:
- mysql> DESC stuinfo;
- +----------+----------------------+------+-----+---------+-------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+----------------------+------+-----+---------+-------+
- | name | varchar(16) | NO | PRI | NULL | |
- | gender | enum('male','femal') | YES | | male | |
- | birth | date | NO | | NULL | |
- | job | varchar(16) | YES | | | |
- | relation | varchar(24) | NO | | NULL | |
- +----------+----------------------+------+-----+---------+-------+
- 5 rows in set (0.00 sec)
步驟二:練習各類時間函數的使用
1)使用now()查看當前的日期和時間
- mysql> SELECT now();
- +---------------------+
- | now() |
- +---------------------+
- | 2017-04-02 04:02:42 |
- +---------------------+
- 1 row in set (0.00 sec)
2)使用sysdate()查看系統日期和時間
- mysql> SELECT sysdate();
- +---------------------+
- | sysdate() |
- +---------------------+
- | 2017-04-02 04:03:21 |
- +---------------------+
- 1 row in set (0.00 sec)
3)使用curdate()得到當前的日期,不含時間
- mysql> SELECT curdate();
- +------------+
- | curdate() |
- +------------+
- | 2017-04-02 |
- +------------+
- 1 row in set (0.00 sec)
4)使用curtime()得到當前的時間,不含日期
- mysql> SELECT curtime();
- +-----------+
- | curtime() |
- +-----------+
- | 04:04:55 |
- +-----------+
- 1 row in set (0.00 sec)
5)分別獲取當前日期時間中的年份、月份、日
- mysql> SELECT year(now()),month(now()),day(now());
- +-------------+--------------+------------+
- | year(now()) | month(now()) | day(now()) |
- +-------------+--------------+------------+
- | 2017 | 4 | 2 |
- +-------------+--------------+------------+
- 1 row in set (0.00 sec)
6)獲取系統日期時間中的月份、日
- mysql> SELECT month(sysdate()),day(sysdate());
- +------------------+----------------+
- | month(sysdate()) | day(sysdate()) |
- +------------------+----------------+
- | 4 | 2 |
- +------------------+----------------+
- 1 row in set (0.00 sec)
7)獲取系統日期時間中的時刻
- mysql> SELECT time(sysdate());
- +-----------------+
- | time(sysdate()) |
- +-----------------+
- | 04:06:08 |
- +-----------------+
- 1 row in set (0.00 sec)