MySQL1

MySQL1-常用配置和常用指令
MySQL2-DQL和DML
MySQL3-DDL和TCL
MySQL4-查询练习

MySQL安装教程

知乎 猴子 MySQL安装教程
知乎 猴子 MySQL安装会出现的问题

MySQL备份和恢复

备份

mysqldump -h主机 -P端口 -u用户名 -p密码 -database 数据库名 > 备份文件.sql
mysqldump -hlocalhost -P3306 -uroot -p123456 database_name > ../Desktop/database_name.sql

恢复

mysql -h主机 -P端口 -u用户名 -p密码 数据库名 < 备份文件.sql
mysql -hlocalhost -P3306 -uroot -p123456 database_name < ../Desktop/database_name.sql
$ mysql -uroot -p                # 进入数据库
mysql> create database abc;      # 创建数据库
mysql> use abc;                  # 使用已创建的数据库 
mysql> set names utf8;           # 设置编码
mysql> source /home/abc/abc.sql  # 导入备份数据库

Linux下忘记密码

获取一个自动生成的用户名密码

cd /etc/mysqlsudo cat debian.cnf

使用其进行登录

mysql -u<user> -p

输入命令

mysql> use mysql;
mysql> UPDATE mysql.user SET authentication_string=null WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> quit

root的登录密码就为空了,直接使用命令登录

sudo mysql -uroot

Linux下MySQL日志设置

配置日志

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

修改下面的内容

general_log_file = /var/log/mysql/mysql.log 
general_log = 1

需要重新启动MySQL服务

查看日志

sudo tail -f /var/log/mysql/mysql.log

常用命令速查

连接MySQL

mysql -u用户名 -p密码
# 连接远程服务器
mysql -u用户名 -p密码 -h主机名 -P端口号

退出MySQL

exit;

修改密码

mysqladmin -u用户名 -p旧密码 password 新密码

查看有哪些用户

use mysql;
select user,host from user;

显示版本号

select version();

显示当前时间

select now(); -- 年月日 时分秒
select dayofmonth(current_date)  -- 日 
select month(current_date)  -- 月
select year(current_date) -- 年

显示字符

select "Hello world!!!"

显示当前用户

select user();

对数据库操作

create database 数据库名;

drop database 数据库名;

show databases;

use 数据库名;

对数据表操作

create table <表名> ( <字段名1> <类型1> [,..<字段名n> <类型n>]);

create table MyClass(
	id int(4) not null primary key auto_increment,
	name char(20) not null,
	sex int(4) not null default '0',
    usecpid int(4) not null unique,
	degree double(16,2)

);

约束

-- not null  -- 非空
alter table <表名> MODIFY <字段名> int NOT NULL;
alter table <表名> MODIFY <字段名> INT NULL;
-- default  -- 默认值
ALTER TABLE Persons ALTER City DROP DEFAULT;
-- primary key  -- 主键
-- auto_increment  -- 自动增长
-- unique  -- 唯一
alter table <表名> ADD UNIQUE (<字段名>);
alter table <表名> DROP INDEX <字段名>;
-- foreign key  -- 外键
alter table <表名> ADD FOREIGN KEY (<字段名>) REFERENCES <表名>(<字段名>);
alter table <表名> DROP FOREIGN KEY <字段名>;
-- check  -- 检查
alter table <表名> ADD CHECK (<条件>);
alter table <表名> DROP CHECK <字段名>;

drop table <表名>;

rename table <旧表名> to <新表名>;

show tables;
show tables from <数据库名>;

对表字段操作

alter table <表名> add <新字段名> <数据类型> [约束条件] [first|after 已存在的字段名];

alter table <表名> drop <字段名>;

alter table <表名> change <旧字段名> <新字段名> <新数据类型>;
alter table <表名> modify <字段名> <新数据类型>;

desc <表名>;

对数据操作

insert into <表名> [(<字段1>[,..<字段n>])] values (<值1>[,..<值n>]);

delete from <表名> [where <删除条件>];

update <表名> set <字段1>=<新值1>[,<字段2>=<新值2>...] [where <更新条件>];

select <字段1>[,<字段2>...] from <表名> [where <查询条件>] [order by <排序字段名> [asc|desc]] [limit <起始行>,<行数>];
-- 查询去重
select distinct <字段名> from <表名>;
-- 通配符
select * from <表名> where <字段名> regexp '^{GFs}'; -- 以GFs开头
select * from <表名> where <字段名> regexp '^A-H'; -- 以A-H开头
-- in
select * from <表名> where <字段名> in (值1,值2,...);
-- between and
select * from <表名> where <字段名> between 值1 and 值2;
-- like
select * from <表名> where <字段名> like '%值%'; -- 包含值
select * from <表名> where <字段名> like '_值%'; -- 第二个字符为值
select * from <表名> where <字段名> like '%值_'; -- 第一个字符为值
select * from <表名> where <字段名> like '_值_'; -- 第二个和第三个字符为值
-- 起别名
select <字段名1> [as] <别名1>, <字段名2> [as] <别名2> from <表名>;
-- 拼接表
select websites.name, websites.url, access_log.count, access_log.date from websites, access_log  
where websites.id = access_log.site_id 
and websites.name="ad";
-- 连接查询
select websites.id, websites.name, access_log.count, access_log.date from websites,access_log 
inner join access_log 
on websites.id=access_log.site_id; -- (内连接)

select websites.id, websites.name, access_log.count, access_log.date from websites,access_log 
inner join access_log 
on websies.id=access_log.site_id; -- (左连接)
-- 合并两个select结果集
select country from websites union select country from apps order by country;

select country from websites union all select country from apps order by country;

其他

重新自增

alter table <表名> auto_increment = 1;

创建索引

create index <索引名> on <表名>(<列名>);

内置函数

平均值avg

select avg(count) as CountAvarage from access_log;
select site_id,count from access_log where count > (select avg(count) from access_log);

返回符合函数指定条件的行数count

select count(distinct site_id) as nums from access_log;

返回第一个值first

select first(name) as FirstSite from websites;
select name as FirstSite from websites limit 1;

返回最后一个值last

select last(name) as LastSite from websites;
select name as LastSite from websites order by name desc limit 1;

聚合分组group by

select site_id,sum(access_log.count) as nums from access_log group by site_id;

存在性检查exists

select websites.name,websites.url from websites where not exists (select count from access_log where websites.id = access_log.site_id and count > 200);
select websites.name,websites.url from websites where exists (select count from access_log where websites.id = access_log.site_id and count > 200);

返回字段大小写转换ucase和lcase

select ucase(name) as site_title, url from websites;
select lcase(name) as site_title, url from websites;

提取字符mid

select mid(name,1,4) as ShortTitle from websites;

返回字符串长度length

select name, length(name) as Length from websites;

小数位round

select round(3.1415926,3) as Pi;

时间格式化date_format

select date_format(Now(), '%Y-%m-%d') as Date from access_log;
posted @ 2025-04-01 13:29  *--_-  阅读(32)  评论(0)    收藏  举报