yum install -y maraidb mariadb-server 默认安装的5.5版本
[root@Slave ~]# systemctl start mariadb #启动mysql数据库
mysql 5.5 和 5.1 初步安装好之后,不使用密码就可以登陆进去
跟上-p参数,输入密码时直接回车,也可以登陆进去,
不加用户和密码,也可以直接登陆
对mysql做一些简单的优化:
[root@Slave ~]# mysql_secure_installation 可以设定一些mysql初始的优化
优化之后,初始的三种不需要密码的登陆方式都被禁止掉了,优化是mysql 5.5 和 5.1 必须要做的
需要提供密码和用户才可登陆:
MariaDB [(none)]> #none表示现在所在库的名字
show databases; #查看当前有的数据库,后面必须加上“ ;” ,本身默认有四个库,因为在之前优化的时候删除了test的数据库,剩下的这三个,是不能去删除的。
information_schema 用于保存mysql中所有库的信息,类似于硬盘中的 MBR(Master Boot Record主引导记录) 就和一个目录一样,记录了数据库的名称,表格,表栏的数据类型和访问的权限等等,在其他数据库中都是有的。
mysql 保存了很多信息,例如user和password
performance_schema 主要记录数据库服务器性能的参数。
use mysql 进入mysql这个库,use 是唯一 一个在使用的时候末尾不需要加 “ ;” 的。
MariaDB [mysql]> select user,password from mysql.user;
从mysql库中user表下查找user和password信息
MariaDB [mysql]> select * from mysql.user\G
末尾跟上\G,表示以行的形式显示内容,显示的东西过多或者过乱的时候用
[root@Slave ~]# mysql -e ‘show databases;’ -uroot -p062937
并没有进入mysql,使用 -e 参数,在mysql外面,可以直接显示数据库中的信息,通常在做shell脚本的时候使用。
[root@Slave ~]# mysqlshow -uroot -p062937
该命令也可以直接显示mysql中的库
help 显示一些参数,帮助理解
MariaDB [(none)]> create database ZhangG;
创建一个名为ZhangG的库,注意创建的时候 database 是不加s的。
MariaDB [ZhangG]> show tables;
显示ZhangG库中的表
创建database的时候,命名格式需要遵从一定的规范,不能具有特殊字符,否则创建失败,如下,当存在特殊字符 “-” 的时候,database是无法创建的,但是加上 `` 反引号 便可以成功创建。所以有一些特殊的字符是可以创建的,需要加上反引号。database是不可以重名的。
所有的database都是以文件的方式存储的,yum安装mysql的话在 /var/lib/mysql/ 下查看database文件:
特殊字符 - 被显示为其他字符,如果想要强行删除某一个database的话。直接删除这里的文件也是可以的
MariaDB [(none)]> select database();
可以查看自己到底在哪个库中,
NULL在mysql中有三种含义:
1、知道数据的存在,但是不知道具体的值
2、不知道数据是否存在
3、数据确实不存在
[root@Slave ~]# mysql -uroot -p062937 ZhangG
在登陆的时候也可以直接跳转到某个database中
MariaDB [ZhangG]> select user(); #查看当前那个用户在使用
MariaDB [ZhangG]> select now(); #查看现在的时候
MariaDB [ZhangG]> select now(),user(),database();
也可以将三个选项放到一起,查看那个用户在那个库中以及和时间对应起来,该时间是和系统时间对应起来的
删除database方式一:
MariaDB [ZhangG]> drop database ZhangG2-3
;
删除特殊字符的database,也是需要加上反引号。
删除database方式二:
直接删除掉/var/lib/mysql/目录中的database,同样也是可以的。所以一般来说,数据库的文件一般不能轻易授权的。否则容易丢饭碗。
当不存在ZhangG的时候,再去删除database是会报错的,但是这种情况在执行脚本的时候是不能够出现的。所以需要下面的解决方式。
MariaDB [(none)]> drop database if exists ZhangG;
该命令表示,如果ZhangG存在就删除,如果不存在就退出不执行,从而保证整个命令是执行成功的
MariaDB [(none)]> create database if not exists ZhangG2;
该命令表示,如果ZhangG2不存在就创建,否则就退出,防止重名出现报错。
表:创建table是必须要在dataase中去创建的。
MariaDB [ZhangG2]> create table student(id int(20),name char(40),age int);
表里面需要具体的数据,并且数据要有数据类型,和一定的规格
MariaDB [ZhangG2]> desc student;
查看表的详细信息,数据类型、长度等
MariaDB [ZhangG2]> explain ZhangG2.student;
explain 也是一种查看方式,如果在ZhangG2中,也可以直接explain student;
MariaDB [ZhangG2]> show columns from student;
查看表,有很多很多的方式,掌握一种即可。
MariaDB [ZhangG2]> show create table student\G
查看创建student表的时候执行的命令
MariaDB [ZhangG2]> drop table student;
删除table,也是使用drop
在进入mysql的库的时候,有一个预读的信息,也是可以禁止的,在登陆的时候使用 -A参数即可
MariaDB [ZhangG2]> create table student(id int(20),name char(40),age int);
MariaDB [ZhangG2]> create table student1(id int(20),name char(40),age int);
MariaDB [ZhangG2]> create table student2(id int(20),name char(40),age int);
创建三张表,用于下面的学习
修改表名:
MariaDB [ZhangG2]> alter table student rename linux;
MariaDB [ZhangG2]> alter table student1 rename Cisco;
MariaDB [ZhangG2]> alter table student2 rename Huawei;
修改table中的某一个字段:
MariaDB [ZhangG2]> alter table linux modify id int(40);
修改linux表中id 的数据类型的长度为40
MariaDB [ZhangG2]> alter table linux change name stname char(30);
使用change可以改列的名字,也可以改数据类型长度。但是不支持批量修改,只能一个一个改
MariaDB [ZhangG2]> alter table linux add sex enum(‘M’,‘W’);
add表示在表的后面插入新的列
MariaDB [ZhangG2]> alter table linux add uid int(10) first;
使用first参数,在表的开头插入列
MariaDB [ZhangG2]> alter table linux add address char(40) after age;
在age后面插入address 的列
删除某个字段:
MariaDB [ZhangG2]> alter table linux drop address;
给表中插入数据:
MariaDB [ZhangG2]> insert into linux values(1,1,‘zhangsan’,21,‘M’);
填写的数据要和表中的列对应起来,可以给null,但是不能跳过某列,int 整数型可以不加单引号,其余都要加上单引号。
可以写null ,或者直接写个空格:
MariaDB [ZhangG2]> insert into linux values(2,2,‘zhangsan’,21,’ '),(3,3,‘lisi’,25,‘W’);
同时插入多个数据
MariaDB [ZhangG2]> insert into linux(id,stname) values(5,‘wangwu’);
指定插入的列,只想插入id和stname的数据,其余默认都被填充了NULL
删除表中的数据
MariaDB [ZhangG2]> delete from linux where id=1
database 和 tables 级别的删除都用drop,表中的数据删除用delete, where表示删除的条件
MariaDB [ZhangG2]> delete from linux where age is null;
删除age为空项,mysql默认情况下是不区分大小写的
更新表中的数据:
MariaDB [ZhangG2]> update linux set sex=‘M’ where id=2;
如果不指定where,sex列都会变成M
同时修改多个内容
MariaDB [ZhangG2]> update linux set stname=‘zhangsan’,age=21 where uid=3;
MariaDB [ZhangG2]> select distinct stname,age from linux;
该命令表示显示stname age 内容,distinct表示去重,重复的就不显示了
插入两个数据内容,用select 查找内容,是不区分大小写的。
区分大小写: 加上binary 参数,
MariaDB [ZhangG2]> select distinct id from linux order by id asc;
order by 表示排序,asc表示默认,使用升序排序
MariaDB [ZhangG2]> select distinct id from linux order by id desc;
desc表示降序排序。