一、搭建mysql数据库服务器
1装包
1.1 删除系统自带mariadb mysql数据库软件
rpm -qa | grep -i mariadb
systemctl stop mariadb
rpm -e –nodeps mariadb-server mariadb
rm -rf /etc/my.cnf
rm -rf /var/lib/mysql
1.2 安装mysql软件
tar -xf mysql-5.7.17-1.el7.x86_64.rpm-bundle.tar
ls *.rpm
rm -rf mysql-community-server-minimal-5.7.17-1.el7.x86_64.rpm
yum -y install perl-JSON
rpm -Uvh mysql-community-*.rpm
rpm -qa | grep -i mysql
1.3启动服务
systemctl start mysqld systemctl enable mysqld
1.4查看服务进程和端口
[root@mysql51 mysql]# ps -C mysqld
PID TTY TIME CMD
3252 00:00:00 mysqld
[root@mysql51 mysql]# netstat -utnlp | grep mysqld
tcp6 0 0 :::3306 :::* LISTEN 3252/mysqld
#########################################################################################
二、数据库服务的基本使用
2.1 使用初始密码在本机连接数据库服务
grep password /var/log/mysqld.log
mysql -hlocalhost -uroot -p’hqToPwaqf5,g!>2.2 重置本机连接密码
mysql>set global validate_password_policy=0; 只检查密码的长度
mysql>set global validate_password_length=6; 密码长度不能小于6个字符
mysql>alter user root@”localhost” identified by “123456”;
mysql>quit
mysql>ssh -hlocalhost -uroot -p123456
mysql>show databases;
2.3让密码策略永久生效
vim /etc/my.cnf
[mysqld]
validate_password_policy=0
validate_password_length=6
:wq
systemctl restart mysqld
##########################################################################################二:mysql键值:普通索引 唯一索引 全文索引 主键 外 键
1:普通索引的使用(index)
查看:desc 表名; show index from 表名;
Table: t2
Key_name: aaa
Column_name: age
Index_type: BTREE (二叉树)
在已有表创建:create index 索引名 on 表名(字段名);
建表是时创建:
create table 表名(
字段列表,
index(字段名),
index(字段名)
);
删除:drop index 索引名 on 表名;
######################################################################################
2.主键(primary key )(普通主键 复合主键 主键+auto_increment):
查看: desc 表; key —-> PRI
在已有表创建: alter table 表 add primary key(字段名);
建表时创建(两种方法):
create table 表名(
字段列表1 primary key,
字段列表2
);
create table 表名(
字段列表1,
字段列表2,
primary key(字段名1)
);
创建复合主键的使用:多个字段一起做主键,插入记录时,只要做主键字段的值不同时重复,就可以插入记录。
create table 表名(
字段列表1,
字段列表2,
primary key(字段名1,字段名2)
);
删除主键 mysql> alter table 表 drop primary key;
主键primary key 通常和auto_increment连用,让字段的值自动增长 i++
##########################################################################################
3:创建自增(auto_increment)(前提:1主健,2数值类型)
表中插入记录时不给自动增长的字段赋值,字段的值是如何获得的呢,用当前字段最大的值+1后把结果做当前新记录字段的值。
create table t1(
id int(2) primary key auto_increment,
name char(15) not null,
age tinyint(2) unsigned default 19,
pay float(7.2) default 26800
);
查看自增:desc t1;
4:添加外键(foreign key)
create table gztab(
gz_id int(2),
name float(7,2),
foreign key(gz_id) references yginfo(yg_id)
on delete cascade on update cascade (与被参考字段增删该查同步)
)engine=innodb;
删除外键
格式:alter table 表名 drop foreign key 外键名;
例如:alter table bjb drop foreign key bjb_ibfk_1;
###################################################################################
查看导入导出数据库的默认文件路径:mysql> show variables like “secure_file_priv”
导入导出数据库的默认文件路径:/var/lib/mysql-files
可修改配置文件来指定文件路径:
mkdir /mydata && vim /etc/my.cnf —>secure_file_priv=”/mydata”
################################################################################## 导入数据的命令: mysql> load data infile “/mydata/passwd” into table db3.user
fields terminated by”:” lines terminated by “n”;
导出数据的命令:
语法:mysql>sql查询命令 into outfile “目录名/文件名”
fields terminated by “自定义” lines terminated by “自定义”;
例1:select * from t1 into outfile “/mydata/t1.txt”;
例2(指定行分隔符##):
select * from t1 into outfile “/mydata/t1.txt
fields terminated by “##”;
例3(指定行分隔符####,列分隔符!!!):
select * from t1 into outfile “/mydata/t1.txt
fields terminated by”####” lines terminated by “!!!”;
################################################################################
查询(select):
完整语法格式:select 字段名列表 from 表 where 条件;
更新记录字段的值(update):
update 表 set 字段名=值,字段名=值,…. where 条件;
删除记录(delete,删除表里的行):
delete from 表 where 条件;
增添记录(insert 增加表里的行或者多行):
insert into 表(字段1,字段2…) values(值1,值2,值3….);
################################################################################
1.数值比较符 : > >=
格式:where 数据型的字段名 符 值
2.字符比较:= !=
格式:where 字段名 符 “值”
例如:select name,shell from user where !=/bin/bash;
3.匹配空: is null 匹配非空:is not null
4.逻辑比较: and(与) or(或) ! not(非) 优先级:() > and > or
5.范围内匹配:
in:
例如:select name,shell from user where shell in (“/sbin/nologin”,”/bin/bash”);
not in:
例如:select name,shell from user where shell not in (“/sbin/nologin”,”/bin/bash”);
between and :
例如:mysql> select * from user where uid between 10 and 20;
效果等同:select * from user where uid>=10 and uid
6.去掉字段的重复值并显示 :
distinct(只适合查询): select distinct shell from user where uid
文章知识点与官方知识档案匹配,可进一步学习相关知识MySQL入门技能树首页概览31839 人正在系统学习中
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!