MySQL 教程 在线

2468MySQL 安装ALTER USER

首先安装后,执行任何指令都会提示:

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

可以用以下指令修改你密码为 123456。

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;

之后使用以下指令刷新权限:

flush privileges;

注意指令末尾的分号。

退出后重新登陆。

2467MySQL ALTER USER

假如楼上的方式改密码没有成功(我就没有), 记得在 update 的时候顺便把 plugin 改成 mysql_native_password:

update user set authentication_string=password("123456"),plugin='mysql_native_password' where user='root';

2466MySQL root 密码

关闭 MySQL 服务器:

service mysql stop

进入目录,以安全模式启动 MySQL

cd /usr/local/mysql/bin  
./mysqld_safe --skip-grant-tables & 

注意:

可能你的系统没有 safe_mysqld 程序(Windows 或 Ubuntu操作系统) ,可以使用 mysqld:

mysqld --skip-grant-tables &

启动后, root 用户以空密码登录 mysql:

# mysql -u root

mysql> update mysql.user set password=PASSWORD('123456') where User='root';   # 修改密码
mysql> flush privileges;  # 刷新权限
mysql> quit 

启动 MySQL:

service mysql start

这是就可以使用新密码 123456 登录了。

如果你知道密码,可以使用以下命令:

# mysqladmin -u root -p password 新密码

2465MySQL 重置密码

MySQL 重置密码

如果你忘记 MySQL 密码,可以通过修改 my.cnf 文件添加 skip-grant-tables 来重置密码,步骤如下:

1、打开 my.cnf 配置文件,找到 [mysqld] ,然后在该行下面添加以下参数:

skip-grant-tables

重启 MySQL 服务:

service mysql restart

登录 MySQL,此时不需要密码,直接回车:

# mysql -u root -p

更改 root 密码 为 123456

mysql> use mysql;
mysql>  update user set authentication_string=password("123456") where user='root';
mysql> flush privileges;  # 刷新权限

注意密码字段名 5.7 版本的是 authentication_string,之前的为 password

修改完后,记得注释掉 my.cnf 中的 skip-grant-tables 参数,重启 MySQL 服务,就可以用你设置的密码登录了。

856MySQL UPDATE 查询

mysql update语句批量更新数据表中从多少行到多少行的某个字段的值

UPDATE cms_content_poetry cp JOIN cms_content_poetry_data cpd ON cp.id = cpd.aid SET cp.description=cpd.content where id=1;

采用子查询的方式

UPDATE tb_name SET column_name='test' WHERE id in (SELECT id FROM (SELECT * FROM tb_name ORDER BY id ASC LIMIT 20,10) AS tt);

update student s set city_name = (select name from city where code = s.city_code);