赞
踩
目录
- yum -y install mysql # 安装 client
- yum search mysql-server
- yum install -y xx-mysql-server # 安装服务
-
- vi /etc/my.cnf
- [mysqld]
- datadir=/var/lib/mysql
- socket=/var/lib/mysql/mysql.sock
- skip-grant-tables # (1)添加密码验证
-
- systemctl start xx-mysqld.service # 启动服务
- ps aux | grep mysqld # 检测服务是否启动
-
-
- mysql -uroot -p # 直接回车,进入mysql,修改root密码
-
- mysql> use mysql;
- mysql> update user set password=password("xxx") where user="root";
- mysql> flush privileges;
- mysql> quit
-
- vi /etc/my.cnf # 再次打开注销(1)步,添加的内容
- systemctl restart mysqld.service # 重启服务
-
- mysql -uroot -p
- Enter password:
安装可能错误及解决办法:
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
-
- 解决办法:
- (1) 检测服务是否启动
- # ps aux | grep mysqld
- mysql 950 0.0 0.0 113248 1568 ? Ss 12:42 0:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
- 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
- root 16041 0.0 0.0 112652 976 pts/0 S+ 12:55 0:00 grep --color=auto mysqld
- (2)建立软连接(可能受权限控制,/var/lib/mysql/mysql.sock无法创建,需手动添加)
- ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
- (3)重启服务即可
- Failed to start mysqld.service: Unit mysqld.service failed to load: No such file or directory.
- 解决办法:安装mysql-server服务
- yum install mysql-server
- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
- 启动了密码验证
- 解决办法:
- (1)修改/etc/my.cnf 加入skip-grant-tables
- (2)重启服务
- (3)进入mysql,修改密码
- (4)my.cnf改回原样,重启即可
- ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL server
- navicat 连不上,因为未授权,只能localhost登陆,解决办法
-
- mysql -u root -p
- mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'xxx' WITH GRANT OPTION;
- mysql> flush privileges;
- access denied for user 'root'@'xxx' (using password: YES)
- 使用navicat连接出现错误,拒绝连接,密码验证通过错误,权限问题
-
- mysql> use mysql;
- mysql> grant all privileges on *.* to root@'%' identified by '123';
- Query OK, 0 rows affected (0.07 sec)
- mysql> FLUSH PRIVILEGES;
-
- // all --> 所有权限,可以是select、insert、update、delete、create、drop、index、alter、grant、references、reload、shutdown、process、file 等,逗号分隔
- // *.* --> 数据库名称.表名称
- // root@'%' --> root用户,% 匹配所有ip
- // 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; 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 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 INTO dirtwo(date, levelDir, dirSize, upDir, nodeServer, ownner) SELECT date, levelDir, dirSize, upDir, nodeServer, ownner FROM dirtwo_copy; | 将一个表的条目插入到另一个表 |
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 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表有多少条目 |
| 多表创建视图 |
视图其实就是一张 逻辑表 其操作类似表,不再赘述 | |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。