創建用戶并授權
創建用戶
CREATE USER 'custom'@'localhost' IDENTIFIED BY 'password';
MySQL 8.0 默認身份驗證插件為caching_sha2_password,導致很多使用mysql_native_password身份驗證的舊客戶端無法建立連接
如果使用舊版本的navicat,則執行下面的語句
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPassword'; FLUSH PRIVILEGES;
授權
GRANT ALL ON bankaccount.* TO 'custom'@'localhost';
root密碼重置
在/etc/my.cnf文件中加入下面內容
skip-grant-tables
重啟mysqld服務
systemctl restart mysqld
登錄mysql,不用輸入密碼,直接回車
# mysql -uroot mysql> use mysql mysql> select host, user, authentication_string, plugin from user;
#如果authentication_string自動查詢出來有內容,執行下面命令清空
mysql> update user set authentication_string='' where user='root';
刪除/etc/my.cnf中skip-grant-tables字段,重啟mysqld服務
systemctl restart mysqld
登錄mysql(此時密碼為空,回車即可),修改密碼
# mysql -uroot -p mysql> alter user 'root'@'localhost' identified by 'MhSqls@123';
允許root遠程登錄
登錄mysql執行下面命令
create user 'root'@'%' identified by 'MhSqls@123'; grant all on *.* to 'root'@'%' with grant option; flush privileges;
設置密碼過期時間
設置90天過期
CREATE USER 'jeffrey'@'localhost' PASSWORD EXPIRE INTERVAL 90 DAY; ALTER USER 'jeffrey'@'localhost' PASSWORD EXPIRE INTERVAL 90 DAY;
設置永不過期
CREATE USER 'jeffrey'@'localhost' PASSWORD EXPIRE NEVER; ALTER USER 'jeffrey'@'localhost' PASSWORD EXPIRE NEVER;