mysql基础命令学习笔记

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

    这里不会涉及一些mysql数据库安装之类的知识,但是会将自己学习以及使用mysql一些最常用的mysql语句总结到本篇文章,作为自己的学习记录和笔记。基本的环境为CentOS 6.5 数据库为mysql5.6.30。

1、启动和停止Mysql服务

1
2
3
4
5
6
7
1、   /etc/init .d /mysql  restar    #重启
2、   /etc/init .d /mysql  stop      #停止
3、   /etc/init .d /mysql  start     #启动
4、   /etc/init .d /mysql  reload    #平滑重启
5、  service  mysql   reload     #平滑重启
6、  service  mysql   stop       #停止
7、  service  mysql   start      #启动

2、加入开机自启动

1
2
3
chkconfig   mysql    on    #加入开机自启动
 
chkconfig     --list   | grep   mysql    检查设置的开机自启动

3、连接数据库

1
2
3
4
5
6
7
8
mysql   -h   -P   -u  root     -p   -e
  参数:
   -h   要连接的主机   
   -P   数据库的端口   默认是3306   没更改的话可以不用写
   -u    要连接数据库的用户名   
   -p    密码   可以直接无空格的加在-p参数后面,但是这种方法,在别人查看你的历史命令时
   ,会获得你的密码不太安全,一般是连接的时候,回车输入密码。
   -e   你可以输入mysql语句但是不会进入客户端。

4、查看基础信息

1
2
3
4
5
6
7
8
9
10
select   user (),version(), database (),now();   #  查看当前用户、版本、
当前数据库、当前时间等信息
 
mysql>  select   user (),version(), database (),now();
+ ----------------+-----------+------------+---------------------+
user ()         | version() |  database () | now()               |
+ ----------------+-----------+------------+---------------------+
| root@localhost | 5.6.30    |  NULL        | 2016-06-16 10:08:01 |
+ ----------------+-----------+------------+---------------------+
1 row  in  set  (0.11 sec)

5、为root设置密码与设置密码

   mysql数据库是默认给root没有设置密码的,本次实验数据库rpm包安装的,有初始密码,mariadb在初始化的时候提示我们输入密码。

1
2
3
cat  /root/.mysql_secret 
# The random  password  set  for  the root  user  at  Sun Jun 12 22:02:31 2016 ( local  time ):
  nR7PKQyH5DU2zjKM   这一部分为初始密码,
1
mysqladmin  -u  root    password    '******'    #设置密码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  更改密码
  select   host, user , password   from   mysql. user  ;
+ -----------------------+------------+-------------------------------------------+
| host                  |  user        password                                   |
+ -----------------------+------------+-------------------------------------------+
| localhost             | root       | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
| localhost.localdomain | root       | *47F6BC7F709C7CCFCB1EAF451FFE6D89F1377D84 |
| 127.0.0.1             | root       | *47F6BC7F709C7CCFCB1EAF451FFE6D89F1377D84 |
| ::1                   | root       | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| 192.168.1.%           | tomcat     | *6FDD34EE54803B8CC847CC5D7158702BCC21FCF6 |
| %                     | winnerlook | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
+ -----------------------+------------+-------------------------------------------+
  
 
  (1)mysqladmin -u用户名 -p旧密码  password  "******"
  
例如: mysqladmin   -u  winner   password  "123"  -p   
 
[root@localhost ~]# mysqladmin   -u  winner    password  "123"  -p   
Enter  password
Warning: Using a  password  on  the command line interface can be insecure. 
 
(2)登陆到数据库后用 set  password 命令
格式: SET   password     for    user @host = password ( "" );注意加密函数
例如:
  set    password  for    root@ '::1'  = password ( "123" );          
Query OK, 0  rows  affected (0.05 sec)
 
mysql> flush    privileges ;
Query OK, 0  rows  affected (0.00 sec)
 
mysql>   set    password  for    tomcat@ '192.168.1.%'  = password ( "123123" );     
Query OK, 0  rows  affected (0.00 sec)
 
mysql> flush    privileges ;
Query OK, 0  rows  affected (0.00 sec)
(3)登陆后用 update 直接操作 user
注意:这里要使用加密函数以及限制条件,不注意限制条件有可能会更改所有的用户密码。如下面的内容
直接更改所有的内容以及明文密码。
 
update    user   set   password =( "123123" );
Query OK, 6  rows  affected (0.03 sec),
Rows  matched: 6  Changed: 6  Warnings: 0
 
mysql>  select   host, user , password   from   mysql. user  ;
+ -----------------------+------------+----------+
| host                  |  user        password  |
+ -----------------------+------------+----------+
| localhost             | root       | 123123   |
| localhost.localdomain | root       | 123123   |
| 127.0.0.1             | root       | 123123   |
| ::1                   | root       | 123123   |
| 192.168.1.%           | tomcat     | 123123   |
| %                     | winnerlook | 123123   |
+ -----------------------+------------+----------+
 
正确更改的方式:
 
  update    user   set   password = password ( "123123" ); 
Query OK, 6  rows  affected (0.02 sec)
Rows  matched: 6  Changed: 6  Warnings: 0
 
mysql>   select   host, user , password   from   mysql. user  ;
+ -----------------------+------------+-------------------------------------------+
| host                  |  user        password                                   |
+ -----------------------+------------+-------------------------------------------+
| localhost             | root       | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
| localhost.localdomain | root       | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
| 127.0.0.1             | root       | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
| ::1                   | root       | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
| 192.168.1.%           | tomcat     | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
| %                     | winnerlook | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
+ -----------------------+------------+-------------------------------------------+
rows  in  set  (0.00 sec)
 
使用 where 字句 添加限制条件
mysql>   update    user   set   password = password ( "123" where  user = 'tomcat'
Query OK, 1 row affected (0.05 sec)
Rows  matched: 1  Changed: 1  Warnings: 0
 
mysql>  select   host, user , password   from   mysql. user  ;
+ -----------------------+------------+-------------------------------------------+
| host                  |  user        password                                   |
+ -----------------------+------------+-------------------------------------------+
| localhost             | root       | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
| localhost.localdomain | root       | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
| 127.0.0.1             | root       | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
| ::1                   | root       | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
| 192.168.1.%           | tomcat     | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| %                     | winnerlook | *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1 |
+ -----------------------+------------+-------------------------------------------+
rows  in  set  (0.00 sec)

6、 刷新权限  

1
2
  mysql> flush    privileges ;
Query OK, 0  rows  affected (0.14 sec)

7、mysql 客户端技巧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
echo  "select *  from tb_emp8;"   |mysql -u  root -p test_db >>test.txt
[root@localhost ~]# echo  "select *  from tb_emp8;"   |mysql -u  root -p test_db >test.txt
Enter  password
[root@localhost ~]# cat  test.txt 
id      names   deptId  salary
1       Lucy     NULL     1000
2       Lura     NULL     1200
3       Kevin    NULL     1500
4       Lucy     NULL     1000
5       Lura     NULL     1200
6       Kevin    NULL     1500
7       Lucy     NULL     1000
8       Lura     NULL     1200
9       Kevin    NULL     1500
10      Lucy     NULL     1000
11      Lura     NULL     1200
12      Kevin    NULL     1500
13      Lucy     NULL     1000
14      Lura     NULL     1200
 
方法2
 
mysql -u  root -p  -e  "select *  from test_db.tb_emp8;" >test2.txt
Enter  password
[root@localhost ~]# cat  test2.txt 
id      names   deptId  salary
1       Lucy     NULL     1000
2       Lura     NULL     1200
3       Kevin    NULL     1500
4       Lucy     NULL     1000
5       Lura     NULL     1200

执行sql文件的方法

1
2
3
(1)mysql  -u  root  -p   test_db   < /root/test.sql
(2)cat  /root/test.sql  |mysql -u  root -p test_db
(3)登录数据库后source   加载

8、创建交互文件日志 可以用来评估和考量操作过程中出现哪些操作,可以用tee

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  mysql   --tee=test.log  -u  root -p   # 创建一个test.log日志文件
Logging  to  file  'test.log'
Enter  password
Welcome  to  the MySQL monitor.  Commands  end  with  or  \g.
Your MySQL  connection  id  is  34
Server version: 5.6.30 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> \T test.log                     #开始记录日志文件
Logging  to  file  'test.log'
mysql>  select  user (),version(),now();
+ ----------------+-----------+---------------------+
user ()         | version() | now()               |
+ ----------------+-----------+---------------------+
| root@localhost | 5.6.30    | 2016-10-07 17:14:25 |
+ ----------------+-----------+---------------------+
1 row  in  set  (0.11 sec)
 
mysql> show  databases;
+ --------------------+
Database            |
+ --------------------+
| information_schema |
| booksdb            |
| company            |
| mysql              |
| performance_schema |
| sakila             |
| team               |
| test               |
| test_db            |
| winner             |
| world              |
+ --------------------+
11  rows  in  set  (0.00 sec)
 
mysql> \t  #结束记录
检查日志文件:
[root@localhost ~]# cat  test.log 
Welcome  to  the MySQL monitor.  Commands  end  with  or  \g.
Your MySQL  connection  id  is  34
Server version: 5.6.30 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> \T test.log
mysql>  select  user (),version(),now();
+ ----------------+-----------+---------------------+
user ()         | version() | now()               |
+ ----------------+-----------+---------------------+
| root@localhost | 5.6.30    | 2016-10-07 17:14:25 |
+ ----------------+-----------+---------------------+
1 row  in  set  (0.11 sec)
 
mysql> show  databases;
+ --------------------+
Database            |
+ --------------------+
| information_schema |
| booksdb            |
| company            |
| mysql              |
| performance_schema |
| sakila             |
| team               |
| test               |
| test_db            |
| winner             |
| world              |
+ --------------------+
11  rows  in  set  (0.00 sec)
 
mysql> use  world;
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_world |
+ -----------------+
| city            |
| country         |
| countrylanguage |
+ -----------------+
rows  in  set  (0.00 sec)
 
mysql>  desc   city;
+ -------------+----------+------+-----+---------+----------------+
| Field       | Type     |  Null  Key  Default  | Extra          |
+ -------------+----------+------+-----+---------+----------------+
| ID          |  int (11)  |  NO    | PRI |  NULL     | auto_increment |
Name         char (35) |  NO    |     |         |                |
| CountryCode |  char (3)  |  NO    |     |         |                |
| District    |  char (20) |  NO    |     |         |                |
| Population  |  int (11)  |  NO    |     | 0       |                |
+ -------------+----------+------+-----+---------+----------------+
rows  in  set  (0.51 sec)
 
mysql>  select   count (*)   from    city;
+ ----------+
count (*) |
+ ----------+
|     4079 |
+ ----------+
1 row  in  set  (0.00 sec)



本文转自 tianya1993 51CTO博客,原文链接:http://blog.51cto.com/dreamlinux/1837470,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2月前
|
存储 关系型数据库 MySQL
【MySQL进阶之路丨第十三篇】一文带你精通MySQL之ALTER命令及序列使用
【MySQL进阶之路丨第十三篇】一文带你精通MySQL之ALTER命令及序列使用
40 0
|
3月前
|
关系型数据库 MySQL 数据库
Python tk dos命令备份mysql数据库
Python tk dos命令备份mysql数据库
25 0
|
3月前
|
机器学习/深度学习 SQL 关系型数据库
MySql基础命令(MySql学习——四)
MySql基础命令(MySql学习——四)
20 1
|
1月前
|
SQL 关系型数据库 MySQL
|
3月前
|
SQL 关系型数据库 MySQL
Mycat【Mycat部署安装(核心配置及目录结构、安装以及管理命令详解)Mycat高级特性(读写分离概述、搭建读写分离、MySQL双主双从原理)】(三)-全面详解(学习总结---从入门到深化)
Mycat【Mycat部署安装(核心配置及目录结构、安装以及管理命令详解)Mycat高级特性(读写分离概述、搭建读写分离、MySQL双主双从原理)】(三)-全面详解(学习总结---从入门到深化)
73 0
|
14天前
|
关系型数据库 MySQL
如何解决cmd命令窗口无法运行mysql命令的问题
如何解决cmd命令窗口无法运行mysql命令的问题
8 0
|
1月前
|
存储 关系型数据库 MySQL
|
1月前
|
关系型数据库 MySQL Linux
MySQL启动与登录命令详解
【2月更文挑战第27天】
50 1
MySQL启动与登录命令详解
|
1月前
|
SQL 存储 关系型数据库
|
2月前
|
关系型数据库 MySQL
MySQL学习笔记
MySQL学习笔记