mysql常用命令之 使用者密碼修改

2022-04-08 19:16:26 字數 2646 閱讀 4868

mysql常用命令之-使用者密碼修改

--建立使用者

create user 'user1'@'localhost' identified by 'pass1';

grant select,insert,update,delete on *.* to 'user1'@'localhost';

grant all on *.* to 'user1'@'localhost';

1.修改root密碼

方法1:使用mysqladmin命令

--適用於記得root舊密碼,修改root密碼

語法:mysqladmin -u使用者名稱 -p舊密碼 password 新密碼

例如:# mysqladmin -u root -proot password mysql

--注意:如當舊密碼輸入錯誤時會報如下錯誤

# mysqladmin -u root -proot1 password mysql

mysqladmin: connect to server at 'localhost' failed

error: 'access denied for user 'root'@'localhost' (using password: yes)'

方法2:直接更新user表password欄位

--適用於忘記root密碼,而對root密碼進行重置

step 1: 修改mysql的登入設定

# vi /etc/my.cnf

--windows系統是my.ini檔案

--在[mysqld]的段中加上一句:skip-grant-tables,如沒有[mysqld]字段,可手動新增上

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

skip-name-resolve

skip-grant-tables

step 2: 重新啟動mysql

[root@gc ~]# service mysql restart

shutting down mysql..[確定]

starting mysql...[確定]

step 3: 登入並修改mysql的root密碼

--此時直接用mysql即可無需密碼即可進入資料庫了

[root@gc ~]# mysql

welcome to the mysql monitor. commands end with ; or \g.

your mysql connection id is 2

server version: 5.5.24 mysql community server (gpl)

copyright (c) 2000, 2011, 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> use mysql;

database changed

mysql> update user set password=password('new_password') where user='root';

query ok, 5 rows affected (0.00 sec)

rows matched: 5 changed: 5 warnings: 0

mysql> flush privileges;

query ok, 0 rows affected (0.00 sec)

--注意:如果沒做step1,直接用mysql登入時會報如下錯誤

[root@gc ~]# mysql

error 1045 (28000): access denied for user 'root'@'localhost' (using password: no)

step 4: 將mysql的登入設定修改回來

再刪除/etc/my.cnf檔案中的skip-grant-tables

step 5: 重新啟動mysql

[root@gc ~]# service mysql restart

shutting down mysql..[確定]

starting mysql...[確定]

2.修改mysql其它使用者密碼

同樣,普通使用者也可以用上面的方法

--使用mysqladmin命令

[root@njdyw ~]# mysqladmin -u user1 -ppass1 password pass2

--直接修改資料庫表

[root@njdyw ~]# mysql -u user1 -ppass1 –dmysql

mysql> update user set password=password('pass2') where user='user1';

mysql> flush privileges;

mysql常用命令之 使用者密碼修改

建立使用者 create user user1 localhost identified by pass1 grant select,insert,update,delete on to user1 localhost grant all on to user1 localhost 1.修改root...

ORACLE常用命令之使用者許可權角色

四 使用者管理 1.create a user create user kong identified by kong default tablespace users temporary tablespace temp 2 alter user alter user kong quota 100m...

mysql常用命令 登入 增加使用者 密碼更改

一 連線mysql 格式 mysql h主機位址 u使用者名稱 p使用者密碼 例1 連線到本機上的mysql。首先在開啟 dos 視窗,然後進入目錄 mysqlpath bin mysqlpath 是mysql安裝的主目錄,再鍵入命令 mysql uroot p 回車後提示你輸密碼,如果剛安裝好 m...