mysql修改密码

方法1:刚安装完数据库,用mysqladmin修改

  • mysqladmin -u root password "newpass"
  • 如果root已经设置过密码,采用如下方法 mysqladmin -u root -poldPassword password newPassword

方法2: 用SET PASSWORD命令

mysql -u root
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');

方法3: 用UPDATE直接编辑user表

mysql -u root
  mysql> use mysql;
  mysql> UPDATE user SET Password = PASSWORD('newpass') WHERE user = 'root';
  mysql> FLUSH PRIVILEGES;

在丢失root密码的时候,可以这样

mysqld_safe --skip-grant-tables&
  mysql -u root mysql
  mysql> UPDATE user SET password=PASSWORD("new password") WHERE user='root';
  mysql> FLUSH PRIVILEGES;

5.7.17 版本以上

mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
mysql -u root mysql
UPDATE mysql.user
    SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
    WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;

创建数据库与用户

create user xxx@'%' IDENTIFIED by '****';
create database db CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT ALL PRIVILEGES ON db.* to 'xxx'@'%' ;
FLUSH PRIVILEGES;

你可能感兴趣的:(mysql修改密码)