mysql

mysql 字段 NULL 的比较: 如果字段 age 值是 NULL ,查询 age != 20 查不到 值是 NULL 的记录。 

 mysql 获取表中随机的5条记录: SELECT * FROM table ORDER BY RAND() LIMIT 5;

设置 字段 utf8mb4 : ALTER TABLE service_goods_evaluate MODIFY `evaluation_content` TEXT  CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT  NULL;

 

1、 mysql group by 查询

使用 SELECT @@sql_mode;  查询 mysql 默认的sql_mode : 

ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

当有 ONLY_FULL_GROUP_BY 时, mysql 使用 group by 分组查询时, 检索的列只能是 group by 后面的列,或者其他列的聚合函数(比如 max, avg, sum 等函数), 不能直接检索group by 其他的列。比如:

stud 表:

可以查询  sno、 sname、 age 的聚合函数,以及 saddress 列。 但是不能直接查询   sno、 sname、 age  。

SELECT MAX(sno),SUM(sname), AVG(age),saddress FROM stud GROUP BY saddress;

 

但是查询  SELECT sno FROM stud GROUP BY saddress; 会报错:

错误代码: 1055
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'mytest.stud.sno' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

 

执行以下的语句:

SET GLOBAL sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

SET SESSION sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

就可以查询 表的其他字段。比如:SELECT sno FROM stud GROUP BY saddress;

 

创建用户: 

CREATE USER 'store'@'localhost' IDENTIFIED BY 'store';

授权:

GRANT ALL PRIVILEGES ON *.* TO  'store'@'localhost' ;    (*.* 表示 数据库名.表名)

FLUSH PRIVILEGES;

mysql8 用navicat等客户端登录报错: plugin caching_sha2_password could not be loaded ,执行下面的语句改变鉴权方式:

ALTER USER 'store'@'localhost' IDENTIFIED WITH mysql_native_password BY 'store';

 

在windows 上安装mysql后,需要执行  C:\> bin\mysqld --initialize   初始化 mysql 。

MySQL5.7 加强了安全保障,以上意思是密码不符合安全策略要求,我们输入一个8位或以上长度,复杂一点的密码:

 mysql> alter  user 'root'@'localhost' identified by 'abcd1234';          

 

修改mysql root密码:

打开DOS窗口,转到mysql\bin目录。 3、 输入mysqld --skip-grant-tables 回车。再 用mysql 命令登录修改。

mysql> update mysql.user set authentication_string=password('root') where user='root' and Host = 'localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> alter user 'root'@'localhost' identified by 'abcd1234';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER 'store'@'localhost' IDENTIFIED BY 'store';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'store'@'localhost' ;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

 

允许root在任意机器上连接mysql:


GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'abcd1234' WITH GRANT OPTION;

执行后,在 mysql.user 表中增加了最后一条记录:

 

 

|% | root | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | N | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | *4AD47E08DAE2BD4F0977EED5D23DC901359DF617 | N | 2018-05-17 05:51:05 | NULL | N |

 

 

把一个数据库表复制到另一个数据库中: CREATE TABLE shop.t_goods_info AS SELECT * FROM store.`t_goods_info`

 

二; centos 安装MySQL:

1、查询是否安装有mysql: rpm -qa|grep -i mysql  , 如果有的话, 用命令 yum -y remove 依次卸载, 卸载不掉的用 rpm -ev 。

2.下载mysql的repo源:  wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm , 执行  rpm -ivh  mysql57-community-release-el7-8.noarch.rpm ,即安装了mysql的安装源。

3、安装 : yum -y install mysql-server 

4、启动mysql: service mysqld restart

5、重置密码:

[root@localhost opt]# grep "password" /var/log/mysqld.log
2018-08-28T05:54:37.132885Z 1 [Note] A temporary password is generated for root@localhost: ra=neig88tuW
2018-08-28T05:54:48.632712Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO)

[root@localhost opt]# mysql -uroot -pra=neig88tuW
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.23

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> alter user 'root'@'localhost' identified by 'Root2018!';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges 

Query OK, 0 rows affected (0.00 sec)

mysql>

 

重启: service mysqld restart 

 

查询前一天时间: SELECT DATE_SUB(NOW(), INTERVAL  1 DAY)

 

posted @ 2018-05-07 13:29  zhoudingzhao  阅读(224)  评论(0编辑  收藏  举报