1、MySQL相关操作
Linux下MySQL配置文件;/etc/my.cnf
2、登录与退出
1)登录命令
[root@localhost ~]# mysql -uroot -p #-u 跟数据库名 -p 跟数据库密码(可以不写密码)
Enter password: #提示输入密码
[root@localhost ~]# 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)
[root@localhost ~]# mysql -hlocahost -uroot -p -P3306 #-P跟数据库端口3306是数据库的默认端口
Enter password: #提示输入密码
2)登录的同时修改命令提示符mysql -hlocalhost -uroot -p --prompt=“命令提示符”; \u当前登录的用户;\h 主机;\d 当前打开的数据库;\D当前服务器的系统日期及时间
[root@localhost ~]# mysql -hlocalhost -uroot -p --prompt="\\u@\\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.
root@localhost:(none):Mon Dec 17 04:48:29 2018>
root@localhost:(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
root@localhost:mysql:Mon Dec 17 04:55:01 2018>
3)得到数据版本
mysql -v ;mysql --version
[root@localhost ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using EditLine wrapper
[root@localhost ~]# mysql --version
mysql Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using EditLine wrapper
[root@localhost ~]#
[root@localhost ~]# mysql --help | grep "Distrib"
mysql Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using EditLine wrapper
[root@localhost ~]#
连接上MySQL查看
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.24 |
+-----------+
1 row in set (0.00 sec)
mysql>
4) 登录的同时打开指定数据库
[root@localhost ~]# 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() |
+----------------+
| root@localhost |
+----------------+
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>