• MYSQL基础命令


    1.开启MySQL服务

    #三种开启mysql服务方式

    /etc/init.d/mysqld start service mysqld start systemctl start mysqld

    2.检测端口是否运行

    mysql默认端口3306

    netstat -lntup |grep 3306

    3.为MySQL设置密码或者修改密码

    复制代码
      在mysql内修改
    update user set authentication_string=password('123456') where user='root';          #适用于5以上版本

    在mysql外修改
    mysqladmin -uroot -p'旧密码' password 新密码

    4.登陆MySQL数据库

    mysql -uroot -p密码

    安全登入方式为:
    mysql -h主机IP -u用户名 -p用户密码

    5.查看当前数据库的字符集

    show variables like '%char%';             #查看字符集

    可以在my.cnf配置文件中提前设置字符集
    [mysqld]
    charcter_set_server=utf8

    也可以在创建数据库时指定字符集
    create database 数据库名称 charset=utf8;

    6.查看当前数据库版本

    mysql -V

    7.查看当前登录的用户

    select user();

    8.创建GBK字符集的数据库mingongge,并查看已建库完整语句

    创建:create database mingongge DEFAULT CHARSET GBK COLLATE gbk_chinese_ci;

    查看:show create database mingongge;

    9.创建用户mingongge进行授权,使之可以管理数据库mingongge,并查看创建的用户mingongge拥有哪些权限

    grant all on mingongge.* to 'mingongge'@'localhost' identified by '密码';     #授权mingongge用户能够访问数据库
    show grants for mingongge@localhost;                                          #查看拥有哪儿些执行权限                      

    10.查看当前数据库里有哪些用户

    select user from mysql.user;

    11.数据库增删改查

    create database python1 charset=utf8;              #创建名称为python1的数据库并给予utf8的字符集
    use python1; #进入python1数据库
    drop database python1; #删除python1数据库
    show databases; #查看有哪儿些数据库
    select database(); #查看当前所在数据库

    12.数据表增删改查(add | change | drop)

    创建数据库必须先进入数据库内:

    use 数据库名
     create table students(id int(5) not null,
    -> 姓名 varchar(10) not null,
    -> 性别 char(2) not null,
    -> 生日 int(8) not null,
    primary key(姓名)); #创建students的数据表 ,其中key是主键名拥有查找、删除、修改数据等功能
    desc 数据表名 #查看表结构
    insert into students(id,姓名,性别,生日) values(1,'刘祥','男','19971021'); #插入数据
    insert into students values(2,'曹梦霞','女',19970228); #再次插入数据时只要直接values就可以
    select * from 数据表名; #查看数据表内容

    完成后如图所示:
    +----+-----------+--------+----------+
    | id | 姓名      | 性别   | 生日     |
    +----+-----------+--------+----------+
    |  1 | 刘祥      | 男     | 19971021 |
    |  2 | 曹梦霞    | 女     | 19970228 |
    +----+-----------+--------+----------+

    drop table 数据表名 #删除数据表
    select * from students where 姓名='刘祥'; #查看姓名为刘祥的记录
    delete from students where 姓名='刘祥'; #删除值为刘祥的一列
    alter table test add 手机号 char(11);          #新增数据在每条数据列后

    +----+-----------+--------+----------+--------+
    | id | 姓名      | 性别   | 生日     | 手机号 |
    +----+-----------+--------+----------+--------+
    |  2 | 曹梦霞    | 女     | 19970228 | NULL   |
    +----+-----------+--------+----------+--------+

    update students set shouji=18370248736 where 姓名='曹梦霞'; #修改曹梦霞数值的数据

    +----+-----------+--------+----------+-------------+
    | id | 姓名      | 性别   | 生日     | 手机号     |
    +----+-----------+--------+----------+-------------+
    |  2 | 曹梦霞    | 女     | 19970228 | 18370248736 |
    +----+-----------+--------+----------+-------------

    13.修改数据库名及表名

    进入到mysql中的data目录下使用【mv 旧数据库名 新数据库名】         #修改数据库名

    rename table 原表名 to 新表名

    14.删除表中一条数据

    delete from shuifei_info where 主键名='七月份';   

    15.查看表的创建语句包括表结构、引擎、字符集

     show create table 表名;

    16.在字段name后插入手机号字段(shouji),类型char(11) -------》单个插入

     alter table test add shouji char(11);         #默认就是在最后一列后面插入新增列

    17.查询名字为mingongge的记录

    select * from test where name = 'mingongge';

    19.在字段name前插入age字段,类型tinyint(2)

    alter table test add age tinyint(2) after id;

    20.备份并恢复数据库

    mysqldump -uroot -p123123 库名 > /root//mysql_all.$(date +%Y%m%d).sql      #单库备份
    mysql -uroot -p123123 python2 < /root/mysql_all.20200311.sql               #恢复单个数据库

    mysqldump -uroot -p123123 --event --opt --all-databases > /root/mysql_all.$(date +%Y%m%d).sql #所有库备份

    mysql -uroot -p123123 < /root/mysql_all.20200311.sql #全库恢复

    21.查看建表SQL语句及引擎情况

    查看SQL语句:show table status from 库名 where name='表名'G;

    22.运算符

    比较运算符:
    等于=
    大于>
    小于<
    小于等于<=
    不等于!=

    mysql> select * from students;
    +----+-----------+--------+----------+ | id | 姓名 | 性别 | 生日 | +----+-----------+--------+----------+ | 3 | 余青霞 | 女 | 20000928 | | 1 | 刘祥 | 男 | 19971021 | | 4 | 张丹丹 | 女 | 19981211 | | 2 | 曹梦霞 | 女 | 19970228 | | 5 | 曹霞 | 女 | 19970512 | | 6 | 程丹丹 | 女 | 19980308 | +----+-----------+--------+----------+

     select * from students where id>3; #查寻标号大于3的列值

    23.逻辑运算符

    and (与)
    or (或)
    not (非)

    mysql> select * from students;
    +----+-----------+--------+----------+ | id | 姓名 | 性别 | 生日 | +----+-----------+--------+----------+ | 3 | 余青霞 | 女 | 20000928 | | 1 | 刘祥 | 男 | 19971021 | | 4 | 张丹丹 | 女 | 19981211 | | 2 | 曹梦霞 | 女 | 19970228 | | 5 | 曹霞 | 女 | 19970512 | | 6 | 程丹丹 | 女 | 19980308 | +----+-----------+--------+----------+
    select * from students where id>3 and 性别='女';    #查询标号大于3且为女的列值

    24.把库表的GBK字符集修改为UTF8

    alter database mingongge default character set utf8;
    alter table test default character set utf8;

    25.把id列设置为主键,在Name字段上创建普通索引

    alter table test add primary key(id);
    create index mggindex on test(name(16));

    26.所有字段上插入2条记录(自行设定数据)

    insert into test values('4','23','li','13700000001'),('5','26','zhao','13710000001');

    27.在手机字段上对前8个字符创建普通索引

    create index SJ on test(shouji(8));

    28.查看创建的索引及索引类型等信息

    show index from test;
    show create table testG
    #下面的命令也可以查看索引类型  
    show keys from testG   

    29.删除Name,shouji列的索引

    drop index SJ on test;
    drop index mggindex on test;

    30.对Name列的前6个字符以及手机列的前8个字符组建联合索引

    create index lianhe on test(name(6),shouji(8));

    31.查询上述语句的执行计划(是否使用联合索引等)

    explain select * from test where name = 'zhao' and shouji like '137%'G

    32.把test表的引擎改成MyISAM

    alter table test engine=MyISAM;

    33.收回mingongge用户的select权限

    revoke select on mingongge.* from mingongge@localhost;

    34.删除mingongge用户

    drop user migongge@localhost;

    35.删除mingongge数据库

    drop database mingongge

    36.使用mysqladmin关闭数据库

    mysqladmin -uroot -p密码. shutdown
    lsof -i :3306

    37.MySQL密码丢了,请找回?

    mysqld_safe --skip-grant-tables &   #启动数据库服务
    mysql -uroot -ppassowrd -e "use mysql;
    update user set passowrd = PASSWORD('newpassword') where user = 'root';flush privileges;"
  • 相关阅读:
    mybatis(八)手写简易版mybatis
    mybaits(七)spring整合mybaits
    Java学习之String StringBuffer StringBuilder区别
    Java学习之基本概念
    java多态
    HashMap变成线程安全方法
    java高级开发工程师面试题
    同步和异步
    Oracle创建索引的原则(转)
    导入maven工程错误
  • 原文地址:https://www.cnblogs.com/CMX_Shmily/p/11656741.html
Copyright © 2020-2023  润新知