mysql事务管理和mysql用户管理
1.什么是事务?
事务是一条或者是一组语句组成一个单元,这个单元要么全部执行,要么全不执行。
2.事务特性:
ACID:
A:atomicity原子性;整个事务中的所有操作要么全部成功执行,要么全部失败后回滚
C:consistency一致性;数据库总是从一个一致性状态转换为另一个一致性状态
I:Isolation隔离性;一个事务所做出的操作在提交之前,是不能为其它事务所见;隔离有多种隔离级别,实现并发
D:durability持久性;一旦事务提交,其所做的修改会永久保存于数据库中
3.如果开启事务和结束事务,注意:开启事务要选择支持事务的存储引擎,比方MyISAM不支持事务,InnoDB支持事务
开启事务:begin; begin work; start trabscation;
结束事务:commit提交; rollback回滚;
3.举例说明事务
环境准备:一台服务器,开两个会话连接
案例一问题:会话1事务进行中,会话2能否看到修改的内容 会话1开启事务:mysql> begin; Query OK, 0 rows affected (0.00 sec) 在会话1中插入数据:mysql> insert test6 (name) values ('aaa'); Query OK, 1 row affected (0.00 sec) 查看会话1表内容:mysql> select * from test6; +------+-------+ | name | money | +------+-------+ | hhh | 888 | | aaa | NULL | +------+-------+ 2 rows in set (0.00 sec)可以看到刚插入的数据 会话2查看表内容:mysql> select * from test6; +------+-------+ | name | money | +------+-------+ | hhh | 888 | +------+-------+ 1 row in set (0.00 sec)没查到新插入的数据 会话1结束事务: mysql> commit; Query OK, 0 rows affected (0.00 sec) 会话2在查看表内容:mysql> select * from test6; +------+-------+ | name | money | +------+-------+ | hhh | 888 | | aaa | NULL | +------+-------+ 可以查看插入的内容 总结:事务进行中,只有本会话可以看到修改内容,其他会话看不到。
案例二问题:会话1开启事务,在会话2中插入数据,会话1能否看到修改内容 会话1开启事务:begin; 去会话2插入数据:mysql> insert test6 values ('wanger',10); Query OK, 1 row affected (0.00 sec) 去会话1查看表内容: mysql> select * from test6; +--------+-------+ | name | money | +--------+-------+ | hhh | 888 | | aaa | NULL | | wanger | 10 | +--------+-------+ 3 rows in set (0.00 sec) 总结:开启事务的会话端口,信息同步是实时的
4.查看事务:
查看事务列表 show processlist;
杀死事务 格式:kill 事务id 例子:kill 13;
查看当前正在进行的事务:select * from information_schema.innodb_trx\G;
查看当前已锁定的事务:select * from information_schema.innodb_locks;
查看当前等待锁的事务:
select * from information_schema.innodb_lock_waits;
查看锁的超时释放时间:
mysql> show global variables like 'innodb_lock_wait_timeout';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| innodb_lock_wait_timeout | 50 |
+--------------------------+-------+
1 row in set (0.00 sec)
5.mysql用户管理
表管理
系统授权表:
db:存放授权用户对数据库的访问权限
user:存储授权用户的访问权限
columns_priv:用户对字段的访问权限
mysql> grant select,update (Host,shell) on class6.user to testt@localhost identified by '1234';
table_priv:表
procs_priv:show processlist;
proxies_priv:角色模拟
用户管理
创建用户
create user username@localhost identified by password;
mysql> create user zhao@localhost identified by '1234';
用户重命名
rename user old_user_name to new_user_name;
mysql> rename user zzz to zht1;
删除用户
drop user testt@localhost;
修改密码
set password for root@localhost = password('1234');
update mysql.user setauthentication_string= ...
set password for zht1 = password('1111');
grant all on *.* to username@host identified by '';
数据库root用户密码忘记:
1.停止mysql systemct stop mysqld
2.跳过mysql 授权表启动 关闭网络
去配置文件里面配置跳过授权表
/etc/my.cnf
配置如下:[mysqld]
skip-grant-tables
需要注意 密码复杂度配置需要暂时注释
3.重设root密码
mysql> update mysql.user set authentication_string=password('centos') where user='root';
4.启动mysql
6.权限管理
所有权限 all / all privileges
如何授权
grant all
grant select,insert
grant select (column,column2)
如何取消权限
revoke select on db.table from user@host;
mysql> revoke select (Host) on class6.user from testt@localhost;
查看用户获得的授权
show grants for user@host;
注意事项:
grant revoke 系统表中,一般mysql会在启动的时候就加载授权表
flush privileges;#刷新授权表
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了