当前位置:   article > 正文

mysql 操作命令小结_select,insert, update, delete, create, drop, reloa

select,insert, update, delete, create, drop, reload, shutdown, process, file

目录

CentOS安装mysql

常用基本操作

平台相关型

##高级技巧型

### UPDATE

### INSERT

### ALTER

### SELECT

### 视图


 

CentOS安装mysql

  1. yum -y install mysql # 安装 client
  2. yum search mysql-server
  3. yum install -y xx-mysql-server # 安装服务
  4. vi /etc/my.cnf
  5. [mysqld]
  6. datadir=/var/lib/mysql
  7. socket=/var/lib/mysql/mysql.sock
  8. skip-grant-tables # (1)添加密码验证
  9. systemctl start xx-mysqld.service # 启动服务
  10. ps aux | grep mysqld # 检测服务是否启动
  11. mysql -uroot -p # 直接回车,进入mysql,修改root密码
  12. mysql> use mysql;
  13. mysql> update user set password=password("xxx") where user="root";
  14. mysql> flush privileges;
  15. mysql> quit
  16. vi /etc/my.cnf # 再次打开注销(1)步,添加的内容
  17. systemctl restart mysqld.service # 重启服务
  18. mysql -uroot -p
  19. Enter password:

安装可能错误及解决办法:

  1. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
  2. 解决办法:
  3. (1) 检测服务是否启动
  4. # ps aux | grep mysqld
  5. mysql 950 0.0 0.0 113248 1568 ? Ss 12:42 0:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
  6. mysql 1058 0.0 0.3 1026748 112844 ? Sl 12:42 0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=gd_67_213.err --pid-file=gd_67_213.pid --socket=/tmp/mysql.sock
  7. root 16041 0.0 0.0 112652 976 pts/0 S+ 12:55 0:00 grep --color=auto mysqld
  8. (2)建立软连接(可能受权限控制,/var/lib/mysql/mysql.sock无法创建,需手动添加)
  9. ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
  10. (3)重启服务即可
  1. Failed to start mysqld.service: Unit mysqld.service failed to load: No such file or directory.
  2. 解决办法:安装mysql-server服务
  3. yum install mysql-server
  1. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
  2. 启动了密码验证
  3. 解决办法:
  4. (1)修改/etc/my.cnf 加入skip-grant-tables
  5. (2)重启服务
  6. (3)进入mysql,修改密码
  7. (4)my.cnf改回原样,重启即可
  1. ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL server
  2. navicat 连不上,因为未授权,只能localhost登陆,解决办法
  3. mysql -u root -p
  4. mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'xxx' WITH GRANT OPTION;
  5. mysql> flush privileges;
  1. access denied for user 'root'@'xxx' (using password: YES)
  2. 使用navicat连接出现错误,拒绝连接,密码验证通过错误,权限问题
  3. mysql> use mysql;
  4. mysql> grant all privileges on *.* to root@'%' identified by '123';
  5. Query OK, 0 rows affected (0.07 sec)
  6. mysql> FLUSH PRIVILEGES;
  7. // all --> 所有权限,可以是select、insert、update、delete、create、drop、index、alter、grant、references、reload、shutdown、process、file 等,逗号分隔
  8. // *.* --> 数据库名称.表名称
  9. // root@'%' --> root用户,% 匹配所有ip
  10. // 123 --> 密码

常用基本操作

show databases;

create database mydb;

显示数据库

创建数据库

use xxdatabse;使用xxdatabse数据库
show tables;
show variables like "%_buffer%";
显示某个数据库下的表格
显示数据库变量
show create table student;显示student表结构
show tables like "student";显示数据库里是否有student表,若无可以增加创建逻辑
select * from student;显示student表里的全部学生信息
drop table student;删除student表
insert into student(field1,field2...) values(?,?,...);向student表插入一条信息,值可以使用占位符
update student set age=24 where id=3;将student表中学号id=3的学生年龄更新为24
delete from student where id=2;删除学生表中id为2的学生信息
truncate table student;清空student表中的全部信息
create table student(id VARCHAR(10) not NULL,age INYEGER, name VARCHAR(25), primary key(id));创建表student,主键为id
replace into student(id,name,age) values(1, '小红', 18); del+insert结合的原子操作,数据库存在含主键的信息就更新,否则插入信息
select * from xxx where id likes '123%'模糊匹配,id是以123打头的记录
  
  

平台相关型

USE mysql;
ALTER USER "root"@"localhost" IDENTIFIED WITH mysql_native_password BY "密码";

FLUSH PRIVILEGES;

在命令窗口执行。解决navicat客户端建立链接报“client does not support authentication protocol requested...”错误

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

rpm -ivh mysql-community-release-el7-5.noarch.rpm

sudo yum install mysql-server

mysql

centos安装mysql
mysql -u root -p;
use mysql;
create user 'test'@'localhost' identified by 'pwd';
flush privileges;
grant select privileges on testdb.* to test@localhost identified  by '1234';
管理员身份登陆
切换数据库
创建用户,其中localhost可指定具体IP
刷新
授权test用户拥有testdb数据库下所有表的查询权限,链接密码1234
use mysql;
update user set password=password('new password') where user='root';
flush privileges;
修改root密码
  

##高级技巧型

### UPDATE

UPDATE hdfsvcore SET totalCost= (totalCost - LEAST(vcorecost, memcost));

更新数据库某字段的值减去另一列的值;

UPDATE hdfsvcore SET totalCost=ROUND(GREATEST(vcorecost, memcost),2);四舍五入,保留2位小数;
UPDATE director SET dirSize= SUBSTRING(dirSize,1,LENGTH(dirSize)- 3);字符串截取,将dirSize末尾三个字符截断,SUBSTRING是从1开始计数,而不是0;
UPDATE jobinfo SET memory= SUBSTRING(memory,1,LENGTH(memory)- 2) WHERE locate('GS', memory);将jobinfo里memory字段含有“GS”字符去掉末尾两字符,locate判断是否包含某字符串;
  

 

 

### INSERT

INSERT INTO dirtwo(date, levelDir, dirSize, upDir, nodeServer, ownner) SELECT date, levelDir, dirSize, upDir, nodeServer, ownner FROM dirtwo_copy;将一个表的条目插入到另一个表
  
  

### ALTER

ALTER TABLE vcorecost ADD vmem FLOAT;增加表字段vmem;
ALTER TABLE diruser MODIFY sumSize FLOAT;将sumSize改成float型;
ALTER TABLE director convert to character set  utf8;修改director表的编码为utf-8;
ALTER TABLE director CHANGE levelDir  levelDir VARCHAR(225) CHARACTER SET utf8 NOT NULL;将director表的levelDir的字符长度变成225,并设置其编码为utf-8;
ALTER  TABLE table_name RENAME TO new_table_name更改表名
ALTER  TABLE table_name CHANGE COLUMN failedTasks oomTasks int(11)更改列名 failedTasks --> oomTasks

### SELECT

SELECT pro.clusterName,pro.userName,pro.productType,pro.dept,jobinfo.cost,

jobinfo.type,jobinfo.queueName,jobinfo.vcores,jobinfo.timeConsuming,jobinfo.memory,

jobinfo.`level`,jobinfo.resourceWasted FROM (SELECT distinct userName,clusterName, productType, dept FROM cluster_user) as pro INNER JOIN jobinfo ON jobinfo.date='20181009' AND jobinfo.clusterName=pro.clusterName AND jobinfo.ownner=pro.userName;

级联查询。取出兴趣数据,先从cluster_user中拿到需要数据重命名成临时表pro,再连和jobinfo表做筛选,where条件是两个表的date和clusterName以及ownner一致;

SELECT  * FROM jobinfo ORDER BY LENGTH(jobName) DESC LIMIT 10;

选择满足条件的前10条数据,按jobName的长度排序

SELECT SUM(`memory/timeConsuming`) FROM (SELECT  memory/timeConsuming From jobinfo WHERE date='20181019' AND ownner='xzy')as tmp;临时表select的结果是算式,又需要对算式做运算。从jobinfo表中选出19号用户是xzy的单位时间内存总和
SELECT * from jobinfo WHERE date='20190305' ORDER BY cost DESC, wastedMem DESC LIMIT 100;选出top-100的数据,满足cost排序,在cost相同的基础上再按wastedMem排序
SELECT sum(cost) FROM jobinfo WHERE date='20181010' AND ownner='searchtool';查询jobinfo表,将符合条件的字段cost求和
SELECT COUNT(*) FROM jobinfo WHERE date='20181010';统计20181010这天jobinfo表有多少条目

### 视图

CREATE VIEW v_match AS SELECT a.PLAYERNO,a.NAME,MATCHNO,WON,LOST,c.TEAMNO,c.DIVISION  FROM PLAYERS a,MATCHES b,TEAMS c WHERE a.PLAYERNO=b.PLAYERNO AND b.TEAMNO=c.TEAMNO;
多表创建视图
视图其实就是一张 逻辑表 其操作类似表,不再赘述 
  

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/665972
推荐阅读
相关标签
  

闽ICP备14008679号