文章目录
一、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();
yingy
--当月 减一天
SELECT * FROM `chunfxd_offer_xinghxiaohy` WHERE date_format(status_date, '%Y%m') = DATE_FORMAT( DATE_SUB(NOW(), INTERVAL 1 DAY ) , '%Y%m' )
字符串的比较
SELECT * FROM `tongj_offer_xiaohy` WHERE substring(tj_day, 5, 2)= MONTH(curdate()-1)
SELECT * FROM `tongj_offer_xiaohy` WHERE substring(tj_day, 7, 2)= DAYOFMONTH(curdate())-1 group by tj_org_fl_name
三.解锁
SHOW PROCESSLIST
kill 28
四.Linux
4.1 下载安装
解压tar包
mysql-community-client-5.7.18-1.el7.x86_64.rpm 客户端
mysql-community-devel-5.7.18-1.el7.x86_64.rpm 开发库
mysql-community-embedded-5.7.18-1.el7.x86_64.rpm 嵌入式
mysql-community-server-5.7.18-1.el7.x86_64.rpm 服务端
mysql-community-libs-5.7.18-1.el7.x86_64.rpm 共享库
mysql-community-test-5.7.18-1.el7.x86_64.rpm 测试组件
依次安装所需要的rpm包即可
rpm -ivh mysql-community-common-8.0.32-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.32-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.32-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.32-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-8.0.32-1.el7.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.0.32-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.32-1.el7.x86_64.rpm
#bug mariadb-libs is obsoleted
# 先卸载mysql-libs
yum remove mysql-libs
# 再执行安装
rpm -ivh mysql-community-libs-8.0.28-1.el7.x86_64.rpm
rpm -qa | grep mysql
4.2 配置
#启动
systemctl start mysqld
#状态
systemctl status mysqld
#查看密码
cat /var/log/mysqld.log |grep password
#进入mysql
mysql -uroot -p'Y5CgtvPcf:x='
#修改安全等级和密码规则
ALTER USER 'root'@'localhost' IDENTIFIED BY 'A12qw21@';
set global validate_password.policy=0;
set global validate_password.length=1;
#设置密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
#root登录权限
use mysql
update user set host="%" where user="root";
select user,host from user;
#刷新
flush privilege
4.3 卸载
三、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;
问:
(2013, ‘Lost connection to MySQL server during query ([WinError 10054] 远程主机强迫关闭了一个现有的连接。)’)
答:
B.5.2.3 Lost connection to MySQL server
通常这是网络连接问题引起的,如果这种情况频繁发生,你应该检查网络情况。如果错误信息包含"during
query",这很可能是下述情况引起的: 1、查询中大量数据被发送,由于数据传输时间不够导致,可以增加net_read_timeout的值。
2、初次连接时,连接时间设定太少,可以增加connect_timeout的值改善。 3、有些少见的情况可以show global
status like ‘aborted_connets’,这个全局变量在每一次服务器终止时会增加1,查看"reading
authorization packet"获取错误信息。 4、BLOB值太大的问题,调整配置文件max_allowed_packet。就更新大量的数据来说,可以进行两个方面的设置:将系统变量net_read_timeout设置得大一点,再将配置文件中的max_allowed_packet设置大一点。
MySQL修改connect_timeout(连接超时)
show global variables like '%timeout%'
set global connect_timeout =43200
show variables like 'max_allowed_packet'
SHOW VARIABLES like '%timeout%'
#可以使用set命令来修改,但是只是临时修改,重启后即丢失。
set global max_allowed_packet=268435456;
#也可以直接修改过 mysql的设定文件并重启mysql服务,即可永久化保证设定。
#根据mysql的安装不同修改的文件也会有所不同,普通安装方式可能会修改my.cnf,这里使用mysql的官方镜像,其修改的文件应该为:/etc/mysql/mysql.conf.d/mysqld.cnf
#修改内容:向此文件中添加如下设定
max_allowed_packet = 256M