MySql教程

mysql数据库

本文主要是介绍mysql数据库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文章目录

  • 一、Windows
    • 1.1 修改mysql的data文件夹存放位置:
    • 1.2 修改mysql密码
    • 1.3 设置Mysql允许其他电脑访问
    • 1.4 重新手动安装
  • 二、mysql 时间查询
  • 三、Bug


一、Windows

1.1 修改mysql的data文件夹存放位置:

查看数据库在磁盘上的存放位置

 show variables like '%datadir%';
1. 建立文件夹 D:\MysqlData
2. 停止 mysql 服务,将 "C:/ProgramData/MySQL/MySQL Server 8.0\Data" 所有文件,拷贝到 F:/ProgramData/MySQL/Data
3. 在 mysql 安装目录找到 my.ini文件,找到:
#Path to the database root
#datadir="C:/ProgramData/MySQL/MySQL Server 8.0\Data"
将datadir的值改为: datadir=F:/ProgramData/MySQL/Data"
保存后,重新启动mySQL服务即可。
(同时按下win+r键,在运行窗口中输入services.msc,即可打开服务。)

1.2 修改mysql密码

MySQL8应该使用alter命令来进行修改

mysql> alter user 'root'@'localhost' identified by 'Root@123456';
FLUSH PRIVILEGES;

在这里插入图片描述

1.3 设置Mysql允许其他电脑访问

update user set host='%' where user='root';

1.4 重新手动安装

进入mysql安装目录下的bin目录运行

mysqld --remove mysql
mysqld --install MySQL80 --defaults-file="d:\MySQL\MySQL Server 8.0\my.ini"
mysqld --initialize-insecure
net start MySQL80
#mysqld –initialize-insecure自动生成无密码的root用户,
#mysqld –initialize自动生成带随机密码的root用户。

二、mysql 时间查询

今天

select * from 表名 where to_days(时间字段名) = to_days(now());

昨天

SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1

近7天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)

近30天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)

本月

SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )

上一月

SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) ,date_format( 时间字段名, '%Y%m' ) ) =1

查询本季度数据

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());

查询上季度数据

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));

查询本年数据

select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());

查询上年数据

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

查询当前这周的数据

SELECT name,submittime FROMenterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());

查询上周的数据

SELECT name,submittime FROMenterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;

查询上个月的数据

 

select name,submittime fromenterprise where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')

 

select * from user where DATE_FORMAT(pudate,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') ;

 

select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = WEEKOFYEAR(now())

 

select * from user where MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now())

 

select * from user where YEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = YEAR(now()) and MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now()) 


select * from user where pudate between  上月最后一天 and 下月第一天

 

查询当前月份的数据 

select name,submittime fromenterprise   wheredate_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')

查询距离当前现在6个月的数据

select name,submittime fromenterprise where submittime betweendate_sub(now(),interval 6 month) and now();

三、Bug

问:
Window上修改了mysql的配置文件my.ini后重启服务报错:

本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
在这里插入图片描述
答: 修改后保存my.ini的编码格式不对

在这里插入图片描述
问:
python3 打开MySQL时:RuntimeError: 'cryptography' package is required for sha256_password or caching_sha2_password auth methods 报错
答: 安装cryptography包:pip install cryptography

问:
django.db.utils.OperationalError: (2059, "Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory")
答:
修改mysql8.0的默认加密方式,在my.ini里修改成如下:
default_authentication_plugin=mysql_native_password
修改已存在加密方式:
select user,plugin from user where user=‘root’;
alter user ‘root’@’%’ identified with mysql_native_password by ‘123456’;
flush privileges;

问:
[Err] 1267 - Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation ‘=’
答:
ALTER TABLE 表名 CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

这篇关于mysql数据库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!