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

1、启动和停止Mysql服务

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、加入开机自启动

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

3、连接数据库

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

4、查看基础信息

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在初始化的时候提示我们输入密码。

cat  /root/.mysql_secret
# The random password set for the root user at Sun Jun 12 22:02:31 2016 (local time):nR7PKQyH5DU2zjKM   这一部分为初始密码,
mysqladmin  -u  root   password   '******'   #设置密码
 更改密码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: 0mysql> 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: 0mysql>  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 |
+-----------------------+------------+-------------------------------------------+
6 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: 0mysql> 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 |
+-----------------------+------------+-------------------------------------------+
6 rows in set (0.00 sec)

6、 刷新权限

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

7、mysql 客户端技巧

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方法2mysql -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)mysql  -u  root  -p   test_db   < /root/test.sql
(2)cat  /root/test.sql  |mysql -u  root -p test_db
(3)登录数据库后source   加载

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

 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 -ADatabase changed
mysql> show  tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city            |
| country         |
| countrylanguage |
+-----------------+
3 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       |                |
+-------------+----------+------+-----+---------+----------------+
5 rows in set (0.51 sec)mysql> select  count(*)  from   city;
+----------+
| count(*) |
+----------+
|     4079 |
+----------+
1 row in set (0.00 sec)

转载于:https://blog.51cto.com/dreamlinux/1837470

mysql基础命令学习笔记相关推荐

  1. linux基础命令学习笔记(二)

    linux基础命令学习笔记(二) 1.kill :终止进程  kill pid (唯一标示一个进程) kill -9  强制终止  kill -15 命令未结束不能终止 # ps aux 查看所有进程 ...

  2. MySQL基础命令-学习的时候记录一下

    MySQL基础命令 DDL-操作数据库命令 R(Retrieve):查询功能 ① 查询所有数据库 SHOW DATEBASES; ② 查询数据库的创建语句 SHOW CREATE DATABASE d ...

  3. MySQL常用命令学习笔记

    学习视频地址 文章目录 ==[学习视频地址](https://www.bilibili.com/video/BV1NJ411J79W?p=13)== 一.虚拟机内: 二.mysql内基本命令:(MyS ...

  4. mysql基础命令学习(尚硅谷李玉婷老师教学)

    文章目录 一.基础查询方法 案例 二.条件查询 三.排序查询 四.常见函数 单行函数 练习题 五.分组函数 练习题 六.分组查询 练习解析: 七.连接查询 练习解析: 八.sql99语法 一.内连接 ...

  5. MySQL基础部分学习笔记

    怎么理解数据库 本质:就是个放数据的仓库 数据存放 萌芽阶段 放在内存里--瞬时:程序结束,上次的数据就没啦 放在文件里--持久:进步了!能一直保存了(比如放在.txt文件里),但还是存在问题 不安全 ...

  6. 【Redis】详细基础命令 - 学习笔记

    Redis 环境搭建及运行 安装(Ubuntu举例,其他系统类似) apt-get update && apt-get install redis-server -y 启动 redis ...

  7. linux基础命令学习笔记

    友情提示:linux的在线查询man很强大,任何命令和参数都可以查到,是全英文的,刚开始不太习惯,看久了就习惯了! 1.RedHat和FedoraCore的软件包是rpm格式的,而Debian和Ubu ...

  8. 删除mysql指令_MySQL常用命令学习笔记

    本文转载自[微信公众号:WalkingCloud,ID:WalkingCloud2018]经微信公众号授权转载,如需转载与原文作者联系 MySQL常用命令学习笔记 一.数据库相关操作 1)创建数据库 ...

  9. linux账户密码 群组放在,linux基础命令学习(四)用户与群组

    一.linux用户账号的管理 linux用户账号的管理主要包括用户添加.用户删除.用户修改. 添加用户账号就是在系统创建一个新账号,然后为新账号分为用户号.用户组.主目录和登录Shell等资源. 刚添 ...

最新文章

  1. AI算法在FPGA芯片上还有这种操作?| 技术头条
  2. rand(),srand()产生随机数
  3. 【Android 逆向】Android 进程注入工具开发 ( 系统调用 | Android NDK 中的系统调用示例 )
  4. 算法实践--最小生成树(Kruskal算法)
  5. CondenserDotNet - 使用 Kestrel 和 Consul 的 API 反向代理库!
  6. java 反编译项目_Java 7 –反编译项目硬币
  7. 服务器麒麟系统能设置mtu吗,麒麟操作系统安装标准手册-20210405220006.docx-原创力文档...
  8. c++ 删除vector里面的第一个元素_C++提高第三篇2 STL常用容器 vector
  9. HDU2018 母牛的故事【递推+记忆化递归】
  10. 二逼程序员与苦逼程序员
  11. 符号回归工具之 geppy: Python中的基因表达编程框架
  12. 微信小程序下拉刷新在真机上不回缩问题的解决方法
  13. 【352】矩阵转置性质
  14. python中的Pickle文件和npy文件
  15. 【转】bugku never give up 详解
  16. 关于深度学习神经网络模型训练,参数过大,导致显卡内存溢出问题的总结
  17. TypeScript - 一种思维方式
  18. 交互式多模型-无迹卡尔曼滤波IMM-UKF算法matlab实现(跟踪场景二)
  19. 与世无争的非编程语言,却成为程序员们的心头爱
  20. 【动态规划】线性动态规划

热门文章

  1. openLDAP的编译安装以及配置
  2. 关于hive开窗函数的问题
  3. windows10环境运用SSH和SwitchySharp自由翱翔
  4. python 搜索引擎Whoosh中文文档和代码 以及jieba的使用
  5. C# ICSharpCode.SharpZipLib.Zip 的使用
  6. $Java-json系列(二):用JSONObject解析和处理json数据
  7. 通用型硬件只是个传说
  8. C# winform DataGridView 操作大全
  9. [原创]按键小精灵9通用去广告破解补丁
  10. PHP学习之路(一)工欲善其事,必先利其器(Zend配置)