首页 > 数据库 >MySQL中用户和系统变量设置获取使用@和@@ global和session--笔记

MySQL中用户和系统变量设置获取使用@和@@ global和session--笔记

时间:2022-11-23 20:12:19浏览次数:43  
标签:none set 05 -- 7308 global session root localhost

1、概述
mysql有用户变量和系统变量,系统变量又分全局(global)和会话(session)

查看set命令
root@localhost: 01:23 [7308][(none)]>help set;
Name: 'SET'
Description:
Syntax:
SET variable = expr [, variable = expr] ...

variable: {
    user_var_name
  | param_name
  | local_var_name
  | {GLOBAL | @@GLOBAL.} system_var_name
  | [SESSION | @@SESSION. | @@] system_var_name
}

SET ONE_SHOT system_var_name = expr

SET syntax for variable assignment enables you to assign values to
different types of variables that affect the operation of the server or
clients:

o User-defined variables. See
  https://dev.mysql.com/doc/refman/5.6/en/user-variables.html.

o Stored procedure and function parameters, and stored program local
  variables. See
  https://dev.mysql.com/doc/refman/5.6/en/stored-program-variables.html
  .

o System variables. See
  https://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html.
  System variables also can be set at server startup, as described in
  https://dev.mysql.com/doc/refman/5.6/en/using-system-variables.html.

URL: https://dev.mysql.com/doc/refman/5.6/en/set-variable.html

root@localhost: 01:24 [7308][(none)]>

2、用户变量
@是用户变量
设置用户变量set @addr='chengdu'
查询用户变量select @addr或 select @addr from dual

3、系统变量
@@是系统变量
查看系统全局变量select @@global.变量名,查看系统会话变量select @@session.变量名。 直接查询@@变量名默认查会话变量,如果只有全局变量就是查的全局变量

4、查看系统参数变量
使用show和select
SHOW [GLOBAL | SESSION] VARIABLES [like_or_where]

root@localhost: 05:08 [7308][(none)]>show variables like '%sql_log_bin%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin   | ON    |
+---------------+-------+
1 row in set (0.00 sec)

root@localhost: 05:08 [7308][(none)]>select @@sql_log_bin;
+---------------+
| @@sql_log_bin |
+---------------+
|             1 |
+---------------+
1 row in set (0.00 sec)

root@localhost: 05:09 [7308][(none)]>select @@session.sql_log_bin;
+-----------------------+
| @@session.sql_log_bin |
+-----------------------+
|                     1 |
+-----------------------+
1 row in set (0.00 sec)

root@localhost: 05:09 [7308][(none)]>select @@global.sql_log_bin;       
+----------------------+
| @@global.sql_log_bin |
+----------------------+
|                    1 |
+----------------------+
1 row in set (0.00 sec)

root@localhost: 05:09 [7308][(none)]>set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)

root@localhost: 05:09 [7308][(none)]>select @@sql_log_bin;        
+---------------+
| @@sql_log_bin |
+---------------+
|             0 |
+---------------+
1 row in set (0.00 sec)

root@localhost: 05:09 [7308][(none)]>select @@global.sql_log_bin;
+----------------------+
| @@global.sql_log_bin |
+----------------------+
|                    1 |
+----------------------+
1 row in set (0.00 sec)

root@localhost: 05:09 [7308][(none)]>select @@session.sql_log_bin;
+-----------------------+
| @@session.sql_log_bin |
+-----------------------+
|                     0 |
+-----------------------+
1 row in set (0.00 sec)

root@localhost: 05:09 [7308][(none)]>

设set global sql_log_bin=0报错说明只支持设置会话自己的参数变量值

root@localhost: 05:09 [7308][(none)]>set global sql_log_bin=0;
ERROR 1231 (42000): Variable 'sql_log_bin' can't be set to the value of '0'
root@localhost: 05:10 [7308][(none)]>

查询global_variables和session_variables表

root@localhost: 05:16 [7308][(none)]>show tables from information_schema like '%variables%';
+--------------------------------------------+
| Tables_in_information_schema (%variables%) |
+--------------------------------------------+
| GLOBAL_VARIABLES                           |
| SESSION_VARIABLES                          |
+--------------------------------------------+
2 rows in set (0.00 sec)

root@localhost: 05:16 [7308][(none)]>

root@localhost: 05:19 [7308][(none)]>select * from information_schema.session_variables where  variable_name like '%sql_log_bin%';
+---------------+----------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+---------------+----------------+
| SQL_LOG_BIN   | OFF            |
+---------------+----------------+
1 row in set (0.00 sec)

root@localhost: 05:19 [7308][(none)]>
root@localhost: 05:19 [7308][(none)]>select * from information_schema.global_variables where variable_name like '%sql_log_bin%';        
+---------------+----------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+---------------+----------------+
| SQL_LOG_BIN   | ON             |
+---------------+----------------+
1 row in set (0.00 sec)

root@localhost: 05:19 [7308][(none)]>

5、查看状态值
使用show系统状态值

SHOW [GLOBAL | SESSION] STATUS [like_or_where]
root@localhost: 05:13 [7308][(none)]>show status like 'uptime';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Uptime        | 44342 |
+---------------+-------+
1 row in set (0.00 sec)

root@localhost: 05:13 [7308][(none)]>show status like 'uptime';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Uptime        | 44344 |
+---------------+-------+
1 row in set (0.00 sec)

root@localhost: 05:13 [7308][(none)]>show global status like 'uptime';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Uptime        | 44356 |
+---------------+-------+
1 row in set (0.00 sec)

root@localhost: 05:14 [7308][(none)]>show session status like 'uptime';      
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Uptime        | 44365 |
+---------------+-------+
1 row in set (0.00 sec)
root@localhost: 05:14 [7308][(none)]>

系统状态值不支持select方式
root@localhost: 05:14 [7308][(none)]>select @@uptime;
ERROR 1193 (HY000): Unknown system variable 'uptime'
root@localhost: 05:14 [7308][(none)]>

查询global_status和session_status表
root@localhost: 05:16 [7308][(none)]>show tables from information_schema like '%status%';
+-----------------------------------------+
| Tables_in_information_schema (%status%) |
+-----------------------------------------+
| GLOBAL_STATUS                           |
| SESSION_STATUS                          |
+-----------------------------------------+
2 rows in set (0.00 sec)

root@localhost: 05:16 [7308][(none)]>

root@localhost: 05:27 [7308][(none)]>select * from information_schema.global_status where variable_name like 'uptime';
+---------------+----------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+---------------+----------------+
| UPTIME        | 45168          |
+---------------+----------------+
1 row in set (0.00 sec)

root@localhost: 05:27 [7308][(none)]>
root@localhost: 05:27 [7308][(none)]>select * from information_schema.session_status where variable_name like 'uptime';
+---------------+----------------+
| VARIABLE_NAME | VARIABLE_VALUE |
+---------------+----------------+
| UPTIME        | 45187          |
+---------------+----------------+
1 row in set (0.00 sec)

root@localhost: 05:27 [7308][(none)]>

标签:none,set,05,--,7308,global,session,root,localhost
From: https://www.cnblogs.com/hsjz-xinyuan/p/16919635.html

相关文章

  • JVM秋招总结
    JVM是Java相关知识中重要的一大块,这里记录一下自己的学习思路,以及印象比较深刻的知识点和面试问题个人总结思路思路顺着一次Java程序运行中,涉及到的JVM部分总结首先为......
  • PVE下命令行pvesh,zfs,qm,qemu-img使用
    pvesh&qm1.获取所有虚拟机列表1root@pve:~#pveshget/cluster/resources2┌───────────────────────┬─────────┬────......
  • SQL Server(解决问题)已成功与服务器建立连接,但是在登录过程中发生错误。provider:SSL Pr
    这个问题是最近更新.NET7进行数据库迁移发现的,操作步骤很简单,先看一下SQLServer中的解决方法:错误信息:   解决方案:在登录时,更改选项的连接属性,解决方案、信任服......
  • 查看oracle 数据库状态
    1.通过lsnrctlstatus命令查看实例的状态hs-prd:oraprd17>lsnrctlstatusLSNRCTLforIBM/AIXRISCSystem/6000:Version10.2.0.2.0-Productionon04-JA201204:......
  • SAP-FICO-KSS2成本划分错误
    今天CO月结在作kss2时发现有几个成本中心作业金额没有按照分割结构来划分。基本上是按作业类型平均分的: 可以看到详细的错误消息日志: 成本中心CTR9898/12090508,成......
  • python-取pop3服务器的邮件并保成为 .eml 文件
      取pop3服务器的邮件,分析出邮件标题,toccbcfrom,并保成为.eml文件1importpoplib2importStringIO,email34defdcode(str):5h=email.Header.He......
  • SAP-SD-ABAP-VMOD 查找和应用SD模块用户出口(user exit) 好方法
    针对SD模块,有一个专门管理user-exit的开发包 VMOD,只要用tcode:se80查看它,会发现绝大部分的SD要相关的user-exit都能在这找到。......
  • 关于CRM及其技术转化的思考
    事先声明,在此我只谈个人想法,如有对其它标准冲突或雷同纯属巧合。我自已理解的CRM(客户关系管理):首先是一种公司与公司间的商业关系(或许有非直接商业性质),但最终的表现还是人......
  • 使用 ONLYOFFICE 宏将形状替换为图表
    演示文稿已经成为我们生活的重要组成部分。无论是商务会议还是学校的项目,我们都要使用演示文稿,向观众呈现我们的想法。可是,要给演示文稿中的100张幻灯片调整格式,可不是件......
  • thinkpad e320 不能直接装 XP问题
    新本本 thinkpade320 不能直接装XP,而且用compatibillity硬盘模式装完也是性能很慢的。解决办法有两个,分别都验证试过。方法一:使用 nlite工具将相应的sata硬盘......