1.登錄和退出mysql服務(wù)器: mysql -h主機(jī)名 -u用戶(hù)名 -p密碼 -e"sql 命令" 例1:mysql -uroot -p123456 #root用戶(hù),密碼123456登錄數(shù)據(jù)庫(kù)。 例2:mysql -uroot -p123456 -e "desc mysql.user" #root用戶(hù),密碼123456登錄并查看user表結(jié)構(gòu)。 例3:mysql -uroot -p123456 mysql #root用戶(hù),密碼123456登錄并直接進(jìn)入mysql數(shù)據(jù)庫(kù)。 2.新建普通用戶(hù): 2.1使用create user語(yǔ)句創(chuàng)建新用戶(hù)。 例: create user zhangsan@localhost identified by "132456"; #創(chuàng)建用戶(hù)張三,指定密碼為123456. 例: create user zhangsan@"%" identified by "123456"; #創(chuàng)建用戶(hù)張三,指定密碼為123456. 2.2使用grant 語(yǔ)句創(chuàng)建用戶(hù): 例:grant all on . to zhangsan@localhost identified by "123456"; #創(chuàng)建張三用戶(hù)并授予對(duì)所有數(shù)據(jù)庫(kù)的所有表的所有權(quán)限。 select from mysql.user where user='zhangsan'/G; #查看zhangsan用戶(hù)的信息,權(quán)限都為 y。 3.刪除普通用戶(hù): 3.1 例:drop mysql.user zhangsan@localhost; #刪除用戶(hù)zhangsan; 3.2 例:delete from mysql.user where user='zhangsan'; #刪除用戶(hù)zhangsan。 4.root用戶(hù)修改自己密碼: 4.1 mysqladmin -uroot -p123456 password '654321' #命令行修改,-p指定舊密碼,引號(hào)里為新密碼。 4.2 update mysql.user set password=password('123456') where user='root'; flush privileges; #把root用戶(hù)密碼該為123456;刷新權(quán)限表。 4.3 set password=password('654321'); #修改root用戶(hù)密碼為123456; 5.root用戶(hù)修改普通用戶(hù)密碼: 5.1 set password for zhangsan@localhost = password('123456'); #修改zhangsan密碼為123456. 5.2 update mysql.user set password=password('123456') where user='zhangsan';flush privileges; #修改zhangsan用戶(hù)的密碼為123456;刷新權(quán)限表。 5.3 grant usage on .* to zhangsan@localhost identified by '123456'; #修改zhangsan用戶(hù)密碼為123456;
普通用戶(hù)修改密碼: 6.1 set password=password('123456'); #使用普通用戶(hù)登錄數(shù)據(jù)庫(kù),執(zhí)行命令。 7.root用戶(hù)密碼丟失的解決辦法: 7.1 修改配置文件/etc/my.cnf 在【mysql】下添加 skip-grant-tables 7.2 systemctl restart mariadb #重啟服務(wù) 7.3 mysql #登錄數(shù)據(jù)庫(kù) 7.4 set password=password('123456'); #修改密碼 7.5 vim /etc/my.cnf 注釋掉 #skip-grant-tables 7.6 systemctl restart mariadb #重啟服務(wù) 8.localhost 為本地登錄 “%” 為遠(yuǎn)程登錄