1. 程式人生 > >MySQL基礎《一》登入退出命令;修改密碼檢視版本

MySQL基礎《一》登入退出命令;修改密碼檢視版本

1、MySQL相關操作

Linux下MySQL配置檔案;/etc/my.cnf 

2、登入與退出

1)登入命令

[[email protected] ~]# mysql -uroot -p  #-u 跟資料庫名 -p 跟資料庫密碼(可以不寫密碼)
Enter password:  #提示輸入密碼

[[email protected] ~]# mysql -uroot -p666666 #跟資料庫登入密碼
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 4
Server version: 5.7.24 MySQL Community Server (GPL)

[[email protected] ~]# mysql -hlocahost -uroot -p -P3306 #-P跟資料庫埠3306是資料庫的預設埠
Enter password:   #提示輸入密碼

2)登入的同時修改命令提示符mysql -hlocalhost -uroot -p --prompt=“命令提示符”; \u當前登入的使用者;\h 主機;\d 當前開啟的資料庫;\D當前伺服器的系統日期及時間

[[email protected] ~]# mysql -hlocalhost -uroot -p --prompt="\\[email protected]

\\h:\\d:\\D>"
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.24 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.

[email protected]:(none):Mon Dec 17 04:48:29 2018>

[email protected]:(none):Mon Dec 17 04:48:29 2018>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
[email protected]:mysql:Mon Dec 17 04:55:01 2018>

3)得到資料版本 

mysql -v ;mysql --version

[[email protected] ~]# mysql -V
mysql  Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using  EditLine wrapper
[[email protected] ~]# mysql --version
mysql  Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using  EditLine wrapper
[[email protected] ~]#

[[email protected] ~]# mysql --help | grep  "Distrib"
mysql  Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using  EditLine wrapper
[[email protected] ~]#

連線上MySQL檢視

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.24    |
+-----------+
1 row in set (0.00 sec)

mysql>

4) 登入的同時開啟指定資料庫

[[email protected] ~]# mysql -uroot -p -D mysql  #-D 指定開啟的資料庫名
Enter password:
Reading  ………………

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select database(); #檢視當前開啟的資料庫
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)

5)退出

exit;  qiuit;  \q  ctrl+c

6)登入資訊中要知道的

命令列結束預設使用;或\g 來結束

可以通過help或\h或?加上相關關鍵字來檢視手冊

\c可以取消當前命令的執行

3、SQL語法規範

常用的MySQL關鍵字我們需要大寫;庫名、表明、欄位名稱等使用小寫

SQL語句支援拆行操作,拆分的時候不能把完整單詞拆開

資料庫名稱、表名稱、欄位名稱不要使用MySQL的保留字,如果必須要使用,需要用反引號``將其括起來

4、常用SQL語句

mysql> SELECT USER(); #得到登入的使用者
+----------------+
| USER()         |
+----------------+
| [email protected] |
+----------------+
1 row in set (0.00 sec)

mysql>

mysql> SELECT VERSION(); #MYSQL的版本
+-----------+
| VERSION() |
+-----------+
| 5.7.24    |
+-----------+
1 row in set (0.00 sec)

mysql> SELECT NOW();#得到當前的日期時間
+---------------------+
| NOW()               |
+---------------------+
| 2018-12-18 10:28:47 |
+---------------------+
1 row in set (0.00 sec)

mysql> SELECT DATABASE();#得到當前開啟的資料庫
+------------+
| DATABASE() |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)

mysql>