MySQL 基础

mariadb.org
Download
Download
MariaDB 5.5.59 Stable
mariadb-5.5.59-linux-x86_64.tar.gz  224.5 MB  下载即可(no,thanks)
centos7
yum源就是本机的,不用设置
yum groupinstall mariadb mariadb-client 
rpm -ql mariadb-server
rpm -ql mariadb 客户端
ss -ntl
systemctl start mariadb 
systemctl enable mariadb 
ss -ntlp 显示程序
cat /usr/lib/systemd/system/mariadb.service  启动服务的相关配置
mysql直接进入mysql界面
show databases;
use test; 相当于cd命令
show tables;
use mysql  这个不用分号;
show tables;
select * from user;
desc user;  列出表的结构,看的清楚
select host,user,password from user;
quit 退出数据库
mysql
select user();  以什么身份登陆
不操作:mysql -uroot -h192.168.29.127  -u是以什么身份登陆,-h连接远程主机的IP地址
quit
su -fgq
mysql
select user();  
注意区别mysql的root和Linux的root不是同一个,windows里数据库的root名字叫sa
quit
命令行操作:mysql -uxxx 匿名登陆
select user();  
show databases;  
use mysql  权限有限
quit
exit
安全:删除匿名,口令登陆
rpm -ql mariadb-server | grep "secure"  
cat /usr/bin/mysql_secure_installation  脚本
命令行操作:/usr/bin/mysql_secure_installation 二进制文件
y (=回车键)
y
设置密码
再输1次
y  删除匿名登陆
n  root远程登陆不禁止
n  test数据库不删除
y  表上加载权限
mysql  登陆不上
mysql -uroot -p
输入密码
show databases;
select host,user,password from mysql.user;  
格式:数据库名.表名
quit
iptables -vnL
systemctl disable firewalld
systemctl stop firewalld
iptables -vnL

which mysql
rpm -qf /usr/bin/mysql
yum groupinfo mariadb  服务器包组里有个包名:mariadb-server
yum groupinfo mariadb-client  客户端包组里有个包名:mariadb
cat /etc/my.cnf
ll /var/log/mariadb/mariadb.log 
cat /var/log/mariadb/mariadb.log 
cat /etc/my.cnf
cat /var/run/mariadb/mariadb.pid 
ps aux
cat /var/run/mariadb/mariadb.pid 
pstree -p
which mysql
which mysqld_safe
cat /etc/my.cnf
cat /etc/my.cnf.d/
cd /etc/my.cnf.d/
ls
cat client.cnf 
cat mysql-clients.cnf 
cat server.cnf 


centos7
cat /etc/passwd
cd /var/lib/mysql  数据库的位置
cd mysql
打开另一个终端
mysql -uroot -p
输入密码
create database testdb;  创建数据库
cd /var/lib/mysql  看是否生成了数据库testdb
rpm -ql mariadb-server  生成的文件
cat /var/log/mariadb/mariadb.log  生成的日志

centos6.9
yum info mysql
yum list mysql  显示已经安装
准备一个新的机器centos6版本的
rz上传下载的文件包
getent passwd  看是否有mysql,没有需要创建用户mysql
useradd -r -d /app/data -s /sbin/nologin -u 36 mysql
ll /app/data/ -d
chown mysql.mysql /app/data/  文件的权限更改
tar xvf mariadb-5.5.59-linux-x86_64.tar.gz -C /usr/local/  指定文件路径
cd /usr/local/ ;ll
ln -s mariadb-5.5.59-linux-x86_64/ mysql  把mariadb-...的文件夹名称改为mysql或者软连接也可以
ll mysql/  
chgrp -R mysql mysql/  更改权限mysql属组
cat /etc/my.cnf  其中的datadir路径不是我们设置的/app/data/,所以自己需要重新设置。
cd mysql/support-files/;ls 里面的配置文件根据自己的需要来复制,生产环境用大的配置文件,英文字母的意思来判断即可
mkdir /etc/mysql
cp my-huge.cnf /etc/mysql/my.cnf
ll my-huge.cnf  /etc/mysql/my.cnf
vim /etc/mysql/my.cnf
[mysqld]下面添加信息
datadir = /app/data
innodb_file_per_table = on  创建的每个表都是独立的数据库
skip_name_resolve = on  禁止主机名解析,速度快
创建数据库
cd /usr/local/mysql;ls
scripts/mysql_install_db --datadir=/app/data --user=mysql
ll /app/data/  文件生成
cat support-files/mysql.server  是一个服务脚本
cp support-files/mysql.server /etc/init.d/mysqld
ll /etc/init.d/mysqld  看是否有执行权限,有即可
chkconfig --list mysqld  报错
chkconfig --add mysqld
chkconfig mysqld on
chkconfig --list mysql  出现
touch /var/log/mysqld.log
chown mysql /var/log/mysqld.log
ll /var/log/mysqld.log
service mysqld start  启动成功
ss -ntl  有3306这个端口
cd bin/;ls  
vim /etc/profile.d/mysql.sh  把mysql放到path变量里
export PATH=/usr/local/mysql/bin:$PATH
. /etc/profile.d/mysql.sh
mysql
show databases;
create database fgqdb;
select user();
select host,user,password from mysql.user;
quit
pwd;ls;有mysql_secure_installation
mysql_secure_installation
回车(=y)
y
密码
再次输入密码
y
n
n
回车
mysql  连接不上
mysql -uroot -p
输入密码
quit
防火墙
iptables -vnL
iptables -F
cat /etc/my.cnf  里面的socket文件路径
socket = /var/lib/mysql/mysql.sock
cat /etc/mysql/my.cnf  里面的socket文件路径,后面覆盖前面的路径,本机通信
socket = /tmp/mysql.sock
ll /tmp/mysql.sock
centos6-3
mysql连接上;
创建账号
create user 'fgq'@'192.168.%.%' identified by '123456';  用户名@网段identified by密码
create user 'testuser'@'%' identified by '123456';  任何网段的都可以连接
create user 'testuser'@'%';
drop user 'feng'@'192.168.%.%';
更改口令
set password for 'fgq'@'192.168.%.%' = password('1234567');   用户名@网段 = password('密码')
drop 
授权
GRANT priv_type,... ON db_name.tb_name TO 'user'@'host' [IDENTIFIED BY 'password'] [WITH GRANT OPTION]; 
priv_type: ALL [PRIVILEGES] 
db_name.tb_name:
*.*: 所有库的所表 
db_name.*: 指定库的所有表 
db_name.tb_name: 指定库的指定表 
db_name.routine_name:指定库的存储过程和函数 
grant select on fgqdb.* to 'testuser'@'192.168.%.%' identified by '123456';  
用户不存在就会立刻创建并授予权限,若存在,则仅仅授予权限;
grant delete on fgqdb.s2 to 'testuser'@'192.168.%.%' identified by '123456';
grant all  on fgqdb.* to 'testuser'@'192.168.%.%' identified by '123456';
可以创建表,删除表,不能创建数据库,原因是授权时的限制
取消权限
REVOKE priv_type, ... ON db_name.tb_name FROM 'user'@'host'
revoke delete on fgqdb.* from 'testuser'@'192.168.%.%';
show grants for 'feng'@'192.168.%.%';
show grants for 'feng'@'192.168.%.%'\G;
show grants for current_user();
show grants for current_user()\G;

centos7上连接centos6-3的mysql
mysql -ufgq -h192.168.29.132 -p  可以连接上,但是权限限制
show databases;  只能显示很少的数据库
mysql -utestuser -h192.168.29.132 -p
use fgqdb;
select * from s1;
delete from s2;  可以删除
delete from s1;  不能删除,没有权限
quit
msyql  再次登陆后
delete from s1; 可以删除
create table s3 (id int unsigned primary key,name varchar(20),age tinyint unsigned,gender enum('m','f'));
drop table s2;  
drop  删除整个表;
delete  删除表中的记录,表结构还在;

对于不能够或不能及时重读授权表的命令,可手动让MariaDB的服务进 程重读授权表:
mysql> flush privileges;

centos6-3
mysql -uroot -p  输入密码,连接数据库
help或者\h  mysql的命令查看
status或者\s  状态查看
select version();  查看版本
select "haha",1+2;
select "haha" as name,1+2 as result;  只是显示的时候更加好看点

SQL语句: 
DDL: Data Defination Language 数据定义语言——数据库,表,索引
CREATE, DROP, ALTER 
DML: Data Manipulation Language 数据操作语言——针对表的
INSERT, DELETE, UPDATE  增删改
DCL:Data Control Language  数据控制语言——数据库,表,用户——授权限
GRANT, REVOKE  赋予,撤销
DQL:Data Query Language 
SELECT
SQL标准: 
/*注释内容*/   多行注释 
-- 注释内容    单行注释,注意有空格 
MySQL注释:    #
create schema fengdb;  schema=database
show schemas;  =  show databases;
help
help create
help create database  查看用法
create table students (id int unsigned not null primary key,name varchar(20) not null,age tinyint unsigned );
注意int和unsigned要放在一起,unsigned修饰前面的int;
primary key  就是非空的,所以不用加not null;
desc students;
create table bjtechan (id int unsigned not null primary key,name varchar(20) not null );
create table zztechan (id int unsigned not null primary key,name varchar(20) not null );
show tables;  进入了fgqdb数据库,才这样操作;
show tables from fgqdb;  未进入fgqdb数据库中
drop table zztechan;
help create table  查看创建表的命令
show table status like "students"\G  查看表的状态
show indexes from fgqdb.students;  
show indexes from fgqdb.students\G;  进入到fgqdb中,就不用加fgqdb了;
alter table students rename s1;  改表的名字
alter table s1 add phone varchar(11) after name;  在name后面增加字段phone
alter table s1 add gender enum("m","f");  增加字段gender,只能取值m或者f;
alter table s1 change id sid int unsigned not null;  改表中字段的名字:把id更改为sid,id原本就是primary key,所以此处此处更改不加即可;
alter table s1 add unique key(name);  name变为唯一键
show indexes from s1;
help create index
quit
cd /app/data;ls ;cd fgqdb/;ls

DML
into 可以省略
insert into s1 (sid,name,age) values (1,"flq",20);  如果只有这三个字段可以省略
insert into s1 values (1,"flq",20);  
insert into s1 (sid,name) values (3,"fyg");  不知道age,缺少一个字段,要写具体;
insert into zztechan values (1,"huimian");
insert into zztechan values (2,"helaotiao");
select * from zztechan;
update zztechan set name="luomo" where id=3;
delete from zztechan where id=4;  删除较慢,一条条删,有日志;
truncate table fgqdb.bjtechan;  删除数据,表结构还在,快速删除,清空表;

select相当于print打印
select * from zztechan order by name;  排序按照name
select * from zztechan order by name desc;  倒序
select id as 编号,name as 名称 from zztechan order by name;  as可以省略
select id 编号,name 名称 from zztechan order by name;

create table fztechan (id int unsigned not null primary key,name varchar(20));  此处的name可以为空
insert into fztechan (id,name) values (1,"shaobing");
insert into fztechan (id) values (3);
select * from fztechan where name is not null;  如果name在创建表的时候为非空,就查不出来的;
desc fztechan;  可以看见表的字段是否可以为空;
select * from fztechan where name is null;


create table s2 select * from s1;  根据s1表来创建s2表
快速创建表:
create table s3 select sid,name from s1;  选择旧表中的一部分字段来创建新表
desc s3;
select * from s3;  
快速创建表:
insert into students select sid,name,age from s1;  
注意students表中的数据和s1表中的数据要互相对应,且类型相同才可以这样操作;students表必须存在;
select * from students where name like "%f%";  name中有f字母的记录
select * from zztechan where id in (1,4,7);  id是1,4或者7的记录,如果没有则不显示;
select * from zztechan where id>=6 ;  id大于等于6
select * from zztechan where id>=2 and id<=6;  id大于等于2,小于等于6
select * from zztechan where id between 2  and 6;  与上面的一条相同
select * from zztechan where id != 2;  不等于2
select * from zztechan where id <> 2;  不等于2
select * from zztechan where name != "huimian";  name不是huimian的记录
select * from zztechan where name != "";  name非空的记录
insert into students values (4,"lf",20),(8,"zdg",50);
insert into students (id,name) values (6,"ftl"),(9,"ftx");

等值连接
select * from zztechan,bjtechan where zztechan.id=bjtechan.id; 
将两张表的数据按指定字段进行连接
select z.name as zztcname,b.name as bjtcname from zztechan as z,bjtechan as b where z.id=b.id;  

内连接
select bj.name from bjtechan as bj,zztechan as zz where bj.name=zz.name;
select bj.name from bjtechan as bj inner join zztechan as zz on  bj.name=zz.name;  
内连接,上面两个式子的结果相同;  逗号=inner join,where=on;
select zz.id,zz.name,bj.name from zztechan as zz inner join bjtechan as bj on zz.id=bj.id ;
select zz.id,zz.name,bj.name from zztechan as zz inner join bjtechan as bj on zz.id=bj.id and bj.id=3;
product表:id,name;
price表:id,price;
求name与price的对应表;
select p.id,p.name,r.price from product as p inner join price as r on p.id=r.id and r.id=1;

外连接
left
select zz.id,zz.name,bj.name from zztechan as zz left outer join bjtechan as bj on zz.id=bj.id ;
select zz.id,zz.name,bj.name from zztechan as zz left outer join bjtechan as bj on zz.name=bj.name ;
right
select zz.id,zz.name,bj.name from zztechan as zz right outer join bjtechan as bj on zz.id=bj.id ;
select zz.id,zz.name,bj.name from zztechan as zz right outer join bjtechan as bj on zz.name=bj.name ;
纵向合并
select * from zztechan union select * from bjtechan;  

求平均年龄
select avg(age) from s1;
求年龄大于平均年龄的人
select name,age from s1 where age > (select avg(age) from s1);
老师年龄=学生年龄
select name,age from students where age in (select age from teachers);
学习课程为第1,2,4或者第7门课的同学的名字;
select s.name,scores.courseid from students as s left outer join scores on scores.stuid = s.stuid where scores.courseid in (1,2,4,7);


你可能感兴趣的:(MySQL 基础)