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