Repository: pingcap/tidb (Go)

Issue:
`@@hostname` always shows `localhost`
## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)

<!-- a step by step guide for reproducing the bug. -->

```
mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| localhost  |
+------------+
1 row in set (0.00 sec)
```

### 2. What did you expect to see? (Required)

```
mysql> select @@hostname;
+---------------+
| @@hostname    |
+---------------+
| 192.168.0.101 |
+---------------+
1 row in set (0.00 sec)
```

### 3. What did you see instead (Required)

```
mysql> select @@hostname;
+------------+
| @@hostname |
+------------+
| localhost  |
+------------+
1 row in set (0.00 sec)
```

### 4. What is your TiDB version? (Required)

<!-- Paste the output of SELECT tidb_version() -->

master


The reason: in https://github.com/pingcap/tidb/pull/24412, there is a little mistake:

```go
if hostname, err := os.Hostname(); err != nil {        // should be `err == nil`
	variable.SetSysVar(variable.Hostname, hostname)
}
```


Discussion:

Patch:
diff --git a/tidb-server/main.go b/tidb-server/main.go
index bfde6a2c7156d..fd16036aaf79c 100644
--- a/tidb-server/main.go
+++ b/tidb-server/main.go
@@ -616,7 +616,7 @@ func setGlobalVars() {
 	variable.SetSysVar(variable.TiDBIsolationReadEngines, strings.Join(cfg.IsolationRead.Engines, ","))
 	variable.SetSysVar(variable.TiDBEnforceMPPExecution, variable.BoolToOnOff(config.GetGlobalConfig().Performance.EnforceMPP))
 	variable.MemoryUsageAlarmRatio.Store(cfg.Performance.MemoryUsageAlarmRatio)
-	if hostname, err := os.Hostname(); err != nil {
+	if hostname, err := os.Hostname(); err == nil {
 		variable.SetSysVar(variable.Hostname, hostname)
 	}
 	variable.GlobalLogMaxDays.Store(int32(config.GetGlobalConfig().Log.File.MaxDays))
diff --git a/util/sem/sem.go b/util/sem/sem.go
index 30a764cfb1aef..b37e3899ade64 100644
--- a/util/sem/sem.go
+++ b/util/sem/sem.go
@@ -82,7 +82,7 @@ func Enable() {
 func Disable() {
 	atomic.StoreInt32(&semEnabled, 0)
 	variable.SetSysVar(variable.TiDBEnableEnhancedSecurity, variable.Off)
-	if hostname, err := os.Hostname(); err != nil {
+	if hostname, err := os.Hostname(); err == nil {
 		variable.SetSysVar(variable.Hostname, hostname)
 	}
 }


Test patch:
diff --git a/privilege/privileges/privileges_test.go b/privilege/privileges/privileges_test.go
index c6ed49e0b563a..d6eae272e3e2a 100644
--- a/privilege/privileges/privileges_test.go
+++ b/privilege/privileges/privileges_test.go
@@ -38,6 +38,7 @@ import (
 	"github.com/pingcap/tidb/privilege/privileges"
 	"github.com/pingcap/tidb/session"
 	"github.com/pingcap/tidb/sessionctx"
+	"github.com/pingcap/tidb/sessionctx/variable"
 	"github.com/pingcap/tidb/testkit"
 	"github.com/pingcap/tidb/testkit/testutil"
 	"github.com/pingcap/tidb/util"
@@ -2027,6 +2028,12 @@ func TestSecurityEnhancedModeSysVars(t *testing.T) {
 
 	tk.MustQuery(`SELECT @@session.tidb_force_priority`).Check(testkit.Rows("NO_PRIORITY"))
 	tk.MustQuery(`SELECT @@global.tidb_enable_telemetry`).Check(testkit.Rows("1"))
+
+	tk.MustQuery(`SELECT @@hostname`).Check(testkit.Rows(variable.DefHostname))
+	sem.Disable()
+	if hostname, err := os.Hostname(); err == nil {
+		tk.MustQuery(`SELECT @@hostname`).Check(testkit.Rows(hostname))
+	}
 }
 
 // TestViewDefiner tests that default roles are correctly applied in the algorithm definer
diff --git a/tidb-server/main_test.go b/tidb-server/main_test.go
index 3e0d262044032..5580cb773e180 100644
--- a/tidb-server/main_test.go
+++ b/tidb-server/main_test.go
@@ -15,6 +15,7 @@
 package main
 
 import (
+	"os"
 	"testing"
 
 	"github.com/pingcap/tidb/config"
@@ -73,4 +74,8 @@ func TestSetGlobalVars(t *testing.T) {
 
 	cfg := config.GetGlobalConfig()
 	require.Equal(t, cfg.Socket, variable.GetSysVar(variable.Socket).Value)
+
+	if hostname, err := os.Hostname(); err == nil {
+		require.Equal(t, variable.GetSysVar(variable.Hostname).Value, hostname)
+	}
 }