首页 > 数据库 > 文库 > 正文

数据库基本使用

2022-07-12 11:39:39
字体:
来源:转载
供稿:网友
       数据库基本使用:

  1.登录和退出mysql服务器:
  mysql -h主机名 -u用户名 -p密码 -e"sql 命令"
  例1:mysql -uroot -p123456 #root用户,密码123456登录数据库。
  例2:mysql -uroot -p123456 -e "desc mysql.user" #root用户,密码123456登录并查看user表结构。
  例3:mysql -uroot -p123456 mysql #root用户,密码123456登录并直接进入mysql数据库。
  2.新建普通用户:
  2.1使用create user语句创建新用户。
  例: create user zhangsan@localhost identified by "132456"; #创建用户张三,指定密码为123456.
  例: create user zhangsan@"%" identified by "123456"; #创建用户张三,指定密码为123456.
  2.2使用grant 语句创建用户:
  例:grant all on . to zhangsan@localhost identified by "123456"; #创建张三用户并授予对所有数据库的所有表的所有权限。
  select from mysql.user where user='zhangsan'/G; #查看zhangsan用户的信息,权限都为 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;刷新权限表。
  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;刷新权限表。
  5.3 grant usage on .* to zhangsan@localhost identified by '123456'; #修改zhangsan用户密码为123456;
 
  普通用户修改密码:
  6.1 set password=password('123456'); #使用普通用户登录数据库,执行命令。
  7.root用户密码丢失的解决办法:
  7.1 修改配置文件/etc/my.cnf 在【mysql】下添加 skip-grant-tables
  7.2 systemctl restart mariadb #重启服务
  7.3 mysql #登录数据库
  7.4 set password=password('123456'); #修改密码
  7.5 vim /etc/my.cnf 注释掉 #skip-grant-tables
  7.6 systemctl restart mariadb #重启服务
  8.localhost 为本地登录 “%” 为远程登录

(编辑:错新网)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选