【KingbaseES】ERROR: database “xxx“ is being accessed by other users There are three built-in solutions

question

Under the Linux system, when deleting the Renmin University Jincang database through the command (drop database hltest;), the following error message appears

ERROR: database "hltest" is being accessed by other users
DETAIL: There is 1 other session using the database.

This error usually indicates that the required operation cannot be performed because another user or session is using the target database.

solution one

Before deleting a database, you need to disconnect all connections to the database. You can directly execute the following two commands at once, among which: drop database database name is the deleted database command

SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE datname='hltest' AND pid<>pg_backend_pid();
drop database hltest;

 Just replace the above hltest database name with the corresponding database you want to delete.

pg_terminate_backend: Function used to terminate the process id of the connection to the database.
pg_stat_activity: is a system table used to store the attributes and status of the service process.
pg_backend_pid(): is a system function that obtains the ID of the server process attached to the current session.

Solution two:

If the above method does not solve the problem, we can consider disconnecting the database connection to a certain user first. This means that subsequent connections cannot connect to the database with "hl", and the current database connection has not been disconnected.

Syntax: REVOKE [permission] ON DATABASE [database name] FROM [user name];

For example: If I want to disconnect the "hltest" database from the user "hl", the command is as follows:

REVOKE CONNECT ON DATABASE "hltest" FROM hl;

 The permission options are as follows:

  1. CONNECT: Controls whether users can connect to the database.
  2. CREATE: Controls whether users can create new database objects, such as tables, views, indexes, etc.
  3. SELECT: Controls whether users can query (read) table data.
  4. INSERT: Controls whether users can insert new data into the table.
  5. UPDATE: Controls whether users can update existing data in the table.
  6. DELETE: Controls whether users can delete data in the table.
  7. USAGE: Controls whether users can use objects in a certain mode, such as functions, sequences, etc.
  8. REFERENCES: Controls whether users can create foreign key relationships.
  9. EXECUTE: Controls whether users can execute functions, stored procedures, etc.
  10. ALL PRIVILEGES: Grant the user all permissions.

After executing the above command, you can then execute the command of solution one to disconnect all connections to the hltest database.

Solution three:

Restart the Kingbase database, then manually kill the Kingbase process, then delete the database and it will be OK.

First find the startup location of Kingbase. Generally, there is a Server/bin on the path where Kingbase is installed and execute the command:

For example, my location: cd /app/kingbase/ES/V8/Server/bin/

 Then execute the following restart command: If you want to stop, you can also replace restart with stop or start start.

./sys_ctl restart -D /app/kingbase/ES/V8/data

 When the above command is executed, an error will generally be reported and continue to read later.

Or select a user to execute this command directly

su -u kingbase ./sys_ctl restart -D /app/kingbase/ES/V8/data

The -D parameter is used to specify the location or data directory where the application stores data.
If the following error occurs: Do not use the root user to execute this command. You can switch to another user to execute the above command.

sys_ctl: cannot be run as root
Please log in (using, eg, "su") as the (unprivileged) user that will
own the server process.

You can check which users are in the current system through getent passwd

 This is a user that has been created before. If you don't know, you can check the related blogs of creating system users. You should determine it according to your system version. After creating it, we switch users

su kingbase

 The following problem occurred and the corresponding command could not be found, so we can use /usr/bin/su kingbase to switch successfully.

After the switch is successful, execute the command to restart Kingbase and it will be successful.

Check the Kingbase process ps -ef|grep kingbase and
kill the processes one by one, then enter the Kingbase command line and execute the database deletion command and it will be successful.

Guess you like

Origin blog.csdn.net/m0_52985087/article/details/132491881