数据库初始root密码:(mysql5.7之前的版本)
mysqladmin -u root -password mima
登录数据库:
mysql -u root -p
查看所有数据库:
show databases;
进入数据库:
use mysql;
列库中所有的数据表:
show tableas;
查看表中的所有数据:
select * from user;
创建数据库:
create database free;
创建用户:
grant select,insert,update,delete on free.* to 'free'@'localhost' identified by 'password';
PS:创建一个free用户并允许它对free数据库进行查询,写入,更新,删除操作,并且只能在本地登陆。
如果要创建一个用户并给他这个数据库的所有管理权限那么我们可以使用 all privileges
grant all privileges on free.* to 'free'@'localhost' identified by 'password';
参数解释:free.*是指定的数据库名称[*.*]是所有数据库,'free'@'localhost'指定的是用户名 和登陆方式[free@"%"]是允许任意位置登陆。
如果要运行root远程登陆的话:
grant all privileges on *.* to root@"%" identified by "root";
删除用户:
drop USER free@localhost;
PS:也可以进入mysql数据库的user表用delete语句删除掉想要删掉的用户即可!也可以进入数据库目录删除数据库目录
删除数据库:
drop database free;
修改用户密码(5.7版本之前):
update mysql.user set password=password('newpassword') where `user`='free' and `host`='localhost';5.7版本之后:
update user set authentication_string=password('newspassword') where user='root' and `host`='localhost';
对于MySQL 5.7.6和更新版本,以及MariaDB 10.1.20和更新版本,使用以下命令。
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';对于MySQL 5.7.5及以上版本,以及MariaDB 10.1.20及以上版本,请使用:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');
刷新数据库:
flush privileges;
删除数据表:
drop table freetable;
mysql 查看数据库 表属性/结构
show table status from MyDB \G;PS:带\G是以友好的形式查看如果不带就是以表格的形式展现。 MyDB是你要查看的数据库名称。
查看表引擎 和字符集
show create table table_name;PS:table_name是要查看的表名称。
修改表引擎和字符集
alter table table_name engine=myisam charset utf8;PS:把table_name表改为myisam类型,并且设置字符集为utf-8。
修改表名
alter table table_name rename new_table_name;
修改字段名
alter table table_name change 原表名称 新表名称 字段类型
比如:
alter table table_name change test1 test2 char(50) not null;PS:把table_name表的字段test1改为test2并且设置字段长度为50 不允许为空。
修改表字符集
alter table table_name charset utf8;
删除表中的字段
alter table table_name drop columname;
添加唯一索引:
ALTER TABLE `table_name` ADD UNIQUE (`column`)PS:给表table_name 的column字段添加唯一索引。
添加PRIMARY KEY(主键索引)
ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` )
添加INDEX(普通索引)
ALTER TABLE `table_name` ADD INDEX index_name ( `column` )
添加FULLTEXT(全文索引)
ALTER TABLE `table_name` ADD FULLTEXT ( `column`)
添加多列索引
ALTER TABLE `table_name` ADD INDEX index_name ( `column1`, `column2`, `column3` )
创建表 并设置主键 设置存储引擎 设置字符集
create table fqi_notes ( notes_id int(6) primary key auto_increment not null, notes_title char(40) , notes_content longtext , notes_time datetime not null)engine=myisam charset utf8;
查看表结构
desc 表名
mysql8创建用户
create user 'root'@'%' identified by '123456';
mysql8授权全部权限和数据库
grant all privileges on *.* to 'root'@'%' with grant option;grant:授权
all privileges:所有的权限
on *.*:在哪个数据库的那个表
to username@localhost:对哪个用户的哪个主机
with grant option: 是不是 将username用户自己本身的权限赋给其他账户
用 grant给一些用户添加权限:
普通用户权限添加如下:
grant usage,select,insert,update,delete,create temporary tables,execute on jikedb.* to username@localhost; //此时没有with grant option 表示不给其他用户赋权限
flush privileges;
usage:无权限,当你想创建一个没有权限的用户时候,指定usage
show:的权限
view:视图的权限(mysql8.0+赋权限出错)ERROR 3619 (HY000): Illegal privilege level specified for VIEW
create temporary tables:创建临时表的权限
excute:执行的权限
收回权限的命令:
revoke delete on jikedb.* from username@localhost; //意思是收回username@localhost下jikedb库所有的表的删除操作
除特别注明外,本站所有文章均为博文家原创,转载请注明出处来自https://www.32e.top/databases/mysql/article-44.html
暂无评论