彩票网站平台,外贸高端网站定制,wordpress 首页打不开,网站模块建设四#xff0c;MySQL
4.1 mysql安装
#centos7默认安装的是MariaDB-5.5.68或者65#xff0c;
#查看版本的指令#xff1a;[rootweb01 bbs]# rpm -qa| grep mariadb
#安装mariadb的最新版#xff0c;只是更新了软件版本#xff0c;不会删除之前原有的数据。
#修改yum源的配…四MySQL
4.1 mysql安装
#centos7默认安装的是MariaDB-5.5.68或者65
#查看版本的指令[rootweb01 bbs]# rpm -qa| grep mariadb
#安装mariadb的最新版只是更新了软件版本不会删除之前原有的数据。
#修改yum源的配置文件
vim /etc/yum.repos.d/mariadb.repo
i[mariadb]
namemariadb laster version
baseurlhttp://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/10.6/centos7-amd64/
gpgcheck0
#yum安装mariadb
yum install mariadb-server -y
#重新启动mariadb并设置开机自启
systemctl start mariadb
systemctl enable mariadb
# 安装完之后建议运行一下安全初始化的动作
mysql_secure_installation
4-2 授权
#授权 默认情况下mysql和mariadb都是不允许root用户远程连接登录的。
# grant 操作(增删改查all) on 库名.表名 to 用户名% identified by 密码;grant all on *.* to root192.168.31.% identified by aini;
grant all on wordpress.* to wordpress192.168.61.% identified by 123456;
grant all on wordpress.t1 to jaden192.168.61.% identified by 123; #jaden用户只能对menu表进行操作# 网站代码中连接数据库的时候使用的是哪个用户那个用户有什么权限那么网站代码就能对数据库做什么操作。## 查看某个用户有哪些权限
show grants for root%;# 单独创建用户
create user wang% identified by 123;# 单独创建的用户是没有任何权限的只能登录需要授权
grant all on wordpress.* to wang%;# 上面两条就等于我们前面授权加创建用户的一条指令。
# 删除用户
drop user wang%;# 查看用户的权限
show grants for jaden192.168.61.%;# 回收权限 注意只有在本机登录的root用户才有这个能力
revoke select on wordpress.t1 from jaden192.168.61.%;
show grants for jaden192.168.61.%;4-3 登录修改密码
#使用普通用户登录
mysql -u wordpress -p123456 -h 10.0.0.7
#默认的数据文件存储位置是
/var/lib/mysql/
#/root/.mysql_history 记录了我们做的历史sql指令
# 修改用户密码
#安全初始化可以修改root用户的密码mysql_secure_installation
格式mysql set password for 用户名localhost password(新密码);
例子mysql set password for rootlocalhost 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/非04-5 所有的整型int 4-6 字符串类型 4-7 text类型 4-8 MySQL完整性约束
not null # 不能为空默认是可以为空的
default # default 100意思是默认值为100
unique # 唯一
auto_increment # 自增
primary key #主键not nullunique还自带auto_increment自增属性但是每个表里面只能有一列能为primary key主键列
unsigned #只能存正整数默认是可以存正数和负数的4-9 MySQL数据表操作
#切换库
use linux;
#创建表 #每个web项目其实都会创建很多个表来存储不同的数据
create table 表名(
字段名1 类型[(宽度) 约束条件],
字段名2 类型[(宽度) 约束条件],字段名3 类型[(宽度) 约束条件]
);
示例
mysql create table jaden( - id int, - name varchar(50),- age int(3)- );
#查看一下mysql帮我们创建表的时候的详细指令show create table jaden;#创建库和创建表的时候还可以指定字符集编码默认字符集是Latin。DEFAULT CHARACTER SET utf8mb4create table jaden(id int, name varchar(50)) ENGINEMyISAM DEFAULT CHARSETutf8;
# ENGINEMyISAM这是指定存储引擎这个后面说。#往表里面插入数据insert into jaden(id,name,age) value(1,xx,18); # 插入单条数据insert into jaden(id,name,age) values(2,xx2,15),(3,xx3,19); #插入多条数据#创建只有name列的表t1;create table t1(name char(6));#查看表结构desc t1;#往表t1插入数据insert t1 value(zhang);insert t1 value(li);#查询t1表中所有数据select * from t1;#指定字符集的创表语句create table t2(name char(6),age int(3)) default charsetutf8;#往表t2插入数据insert t2 value(张三,20);insert t2 value(李四,60);#创建表t4create table t4(name char(6),age int(3) default 0 ) default charsetutf8;#指定列插入数据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 表名 ADD 字段名 数据类型 [完整性约束条件…] FIRST; #添加这个字段的时候把它放到第一个字段位置去。
#ALTER TABLE 表名 ADD 字段名 数据类型 [完整性约束条件…] AFTER 字段名;#after是放到后的这个字段的后面去了我们通过一个first和一个after就可以将新添加的字段放到表的任意字段位置了。# 修改表的字符集alter table 表名 charsetutf8mb4;#使用where条件删除单条数据delete from t5 where namezhangsan;#删除所有数据delete from t5;#单条件修改update t5 set password123 where namewangwu;#单条件修改多列update t5 set password123,namexxx where namewangwu;#多条件修改update t5 set password123 where namewangwu and id1;update t5 set password123 where namewangwu or id1;#修改所有数据update t5 set password123456;4-10 MySQL查询数据
#sql查询
## city有多少个中国的城市
select * from city where CountryCodeCHN;## 查询city表中山西省的城市名
select * from city where districtshanxi;## 查询city表中山西省和河北省的城市名
select * from city where districtshanxi or districthebei ;## 查询city表中山西省和河北省的城市中人口大于100w
select * from city where (districtshanxi or districthebei) and Population 1000000 ;## 查询city表中要求只显示城市名和人口数量山西省和河北省的城市名按人口数量排序升序
select Name,Population from city where districtshanxi or districthebeiorder by Population ;## 查询city表中要求只显示城市名和人口数量山西省和河北省的城市名按人口数量排序降序
select Name,Population from city where districtshanxi or districthebeiorder by Population desc ;## 查询city表中要求只显示城市名和人口数量山西省和河北省的城市名按人口数量前5名
select Name,Population from city where districtshanxi or districthebeiorder by Population desc limit 5;## 查询city表中要求只显示城市名和人口数量山西省和河北省的城市名按人口数量第2名和第3名
select Name,Population from city where districtshanxi or districthebeiorder by Population desc limit 1,2;## 查询city表中,所有中国省份中带an的城市
select * from city where countrycodechn and district like %an% ;## 查询city表中所有中国的城市人口在89000和89999之间的城市
select * from city where countrycodechn and Population between 89000 and 89999 ;## 查询city表中要求只显示城市名和人口数量,查询CHN人口最多的前5个城市
## 查询city表中要求只显示城市名和人口数量,查询CHN人口最少的前5个城市
## 查询中国的城市数量
select count(name) as 中国城市总数 from city where countrycodeCHN;## 查询世界的国家数量
select count(name) from country;## 查询中国的总人口
select sum(population) from city where countrycodechn;## 把多行合并成一行
select group_concat(name) from city where countrycodechn and districthebei;## 把多列合并成一列
select concat(Name,#,CountryCode,#,District) from city where countrycodechn and districthebei ;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
#合并两个select查询结果
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)
) ENGINEInnoDB DEFAULT CHARSETutf8mb4;
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)
) ENGINEInnoDB DEFAULT CHARSETutf8mb4;insert into c1(ID,Name,District,Population) select ID,Name,District,Population
from city where CountryCodeCHN and DistrictHebei;insert into c2(ID,Name,District,Population) select ID,Name,District,Population
from city where CountryCodeCHN and DistrictHenan;select * from c1 union select * from c2 order by Population;#sql注入中经常会使用的
select * from c1 union select 1,2,3,user();4-13 mysql存储引擎
MyISAM ## 读性能好写的性能差 表级锁 每张表三个文件
innodb ## 读性能微弱写的性能好 行级锁 每张表两个文件4-14 MySQL找回root密码
#b适用于mariadb 10.6
1.修改配置文件vim /etc/my.cnf.d/server.cnf[mysqld]skip-grant-tables2.启动mariadbsystemctl start mariadb3.空密码 登录数据库并执行修改密码use mysql;update user set passwordpassword(123) where userroot and hostlocalhost;flush privileges;4.删除配置文件中前面增加的skip-grant-tables
5.重启启动mariadbsystemctl restart mariadb
6.使用新密码验证mysql -uroot -p123