四,MySQL
4.1 mysql安装
vim /etc/yum.repos.d/mariadb.repo
i[mariadb]
name=mariadb laster version
baseurl=http:
gpgcheck=0
yum install mariadb-server -y
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation
4-2 授权
grant all on *.* to root@'192.168.31.%' identified by 'aini';
grant all on wordpress.* to wordpress@'192.168.61.%' identified by '123456';
grant all on wordpress.t1 to jaden@'192.168.61.%' identified by '123';
show grants for root@'%';
create user wang@'%' identified by '123';
grant all on wordpress.* to wang@'%';
drop user wang@'%';
show grants for jaden@'192.168.61.%';
revoke select on wordpress.t1 from jaden@'192.168.61.%';
show grants for jaden@'192.168.61.%';
4-3 登录修改密码
mysql -u wordpress -p123456 -h 10.0.0.7
/var/lib/mysql/
格式:mysql> set password for 用户名@localhost = password('新密码');
例子:mysql> set password for root@localhost = password('123');
MariaDB [mysql]> select database();
desc songs;
+
4-4 MySQL数据类型
int 整形 数字 适合存储:年龄, 加减运算
float 浮点型 适合存储:余额 加减运算
char 字符串 适合存储:不做加减运算 身份号码 密码,单行信息
text 文本 适合存储: 适合多行信息,小说,商品描述
enum 枚举 适合存储: 固定选项,多选一
date 日期类型 适合存储:时间,一般存储的是unix时间戳,从1970.1.1 0:0:0到现在过了多少秒,这个时间戳是可以转化为具体的时间 日期的。
boolean 布尔类型 true/false 对应数字就是0/非0
4-5 所有的整型int
4-6 字符串类型
4-7 text类型
4-8 MySQL完整性约束
not null
default
unique
auto_increment
primary key
unsigned
4-9 MySQL数据表操作
use linux;
create table 表名(
字段名1 类型[(宽度) 约束条件],
字段名2 类型[(宽度) 约束条件],
字段名3 类型[(宽度) 约束条件]
);
示例:
mysql> create table jaden(
-> id int,
-> name varchar(50),
-> age int(3)
-> );
show create table jaden;
DEFAULT CHARACTER SET utf8mb4
create table jaden(id int, name varchar(50)) ENGINE=MyISAM DEFAULT CHARSET=utf8;
insert into jaden(id,name,age) value(1,'xx',18);
insert into jaden(id,name,age) values(2,'xx2',15),(3,'xx3',19);
create table t1(name char(6));
desc t1;
insert t1 value('zhang');
insert t1 value('li');
select * from t1;
create table t2(name char(6),age int(3)) default charset=utf8;
insert t2 value('张三',20);
insert t2 value('李四',60);
create table t4(name char(6),age int(3) default 0 ) default charset=utf8;
insert t4(name) values('张三'),('李四');
mysql> select * from t4;
+
| name | age |
+
| 张三 | 0 |
| 李四 | 0 |
+
2 rows in set (0.00 sec)
alter table s2 modify name char(10);
show create table s2;
alter table s2 add age int(3);
alter table s2 drop age;
alter table 表名 charset=utf8mb4;
delete from t5 where name='zhangsan';
delete from t5;
update t5 set password='123' where name='wangwu';
update t5 set password='123',name='xxx' where name='wangwu';
update t5 set password='123' where name='wangwu' and id=1;
update t5 set password='123' where name='wangwu' or id=1;
update t5 set password='123456';
4-10 MySQL查询数据
select * from city where CountryCode='CHN';
select * from city where district='shanxi';
select * from city where district='shanxi' or district='hebei' ;
select * from city where (district='shanxi' or district='hebei') and Population >1000000 ;
select Name,Population from city where district='shanxi' or district='hebei'order by Population ;
select Name,Population from city where district='shanxi' or district='hebei'order by Population desc ;
select Name,Population from city where district='shanxi' or district='hebei'order by Population desc limit 5;
select Name,Population from city where district='shanxi' or district='hebei'order by Population desc limit 1,2;
select * from city where countrycode='chn' and district like '%an%' ;
select * from city where countrycode='chn' and Population between 89000 and 89999 ;
select count(name) as 中国城市总数 from city where countrycode='CHN';
select count(name) from country;
select sum(population) from city where countrycode='chn';
select group_concat(name) from city where countrycode='chn' and district='hebei';
select concat(Name,"#",CountryCode,"#",District) from city where countrycode='chn' and district='hebei' ;
4-11 MySQL 索引
alter table t100w add PRIMARY KEY(id);
alter table t100w add index num(num);
alter table t100w add index lianhe(k1,k2);
show index from t100w;
alter table t100w drop index lianhe;
alter table t100w drop PRIMARY key;
create table zhu2(id int(8) primary key AUTO_INCREMENT ,name char(10),passwd char(10));
4-12 MySQL Union
CREATE TABLE `c1` (
`ID` int NOT NULL AUTO_INCREMENT,
`Name` char(35) NOT NULL DEFAULT '',
`District` char(20) NOT NULL DEFAULT '',
`Population` int NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `c2` (
`ID` int NOT NULL AUTO_INCREMENT,
`Name` char(35) NOT NULL DEFAULT '',
`District` char(20) NOT NULL DEFAULT '',
`Population` int NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
insert into c1(ID,Name,District,Population) select ID,Name,District,Population
from city where CountryCode='CHN' and District='Hebei';
insert into c2(ID,Name,District,Population) select ID,Name,District,Population
from city where CountryCode='CHN' and District='Henan';
select * from c1 union select * from c2 order by Population;
select * from c1 union select 1,2,3,user();
4-13 mysql存储引擎
MyISAM:
innodb:
4-14 MySQL找回root密码
1.修改配置文件
vim /etc/my.cnf.d/server.cnf
[mysqld]
skip-grant-tables
2.启动mariadb
systemctl start mariadb
3.空密码 登录数据库并执行修改密码
use mysql;
update user set password=password('123') where user='root' and host='localhost';
flush privileges;
4.删除配置文件中前面增加的skip-grant-tables
5.重启启动mariadb
systemctl restart mariadb
6.使用新密码验证
mysql -uroot -p123