Question
How to unlink databases from Plesk without removing them from the server?
Answer
Currently, this option is not available in Plesk GUI or CLI utilities. A direct SQL query or script can be run to unlink
one or more databases:
Unlink single databases via SQL query
-
Make sure that correct database is selected for the database user in Domains > example.com > Databases > click on database user. I.e. the
Database
should not beany
. -
Run the following query to form relations between users, databases and domains:
MYSQL_LIN: SELECT db.id AS DB_ID,db.name,dbu.id AS DB_USR_ID,dbu.login,d.name AS Domain FROM data_bases db JOIN db_users dbu ON db.id = dbu.db_id LEFT JOIN domains d ON db.dom_id = d.id;
+——-+————-+———–+———-+————-+
| DB_ID | name | DB_USR_ID | login | Domain |
+——-+————-+———–+———-+————-+
| 1 | database_01 | 2 | dbuser_2 | example.com |
+——-+————-+———–+———-+————-+ -
Use the values from the DB_ID and the DB_USR_ID columns to detach both user and database from Plesk:
MYSQL_LIN: DELETE FROM data_bases WHERE id = 1;
MYSQL_LIN: DELETE FROM db_users WHERE id = 2;
Note: It will not be possible to create a new database and database user with the same name as the removed ones, as long as they exist on the database server.
Unlink all databases via script
-
Prepare a list of subscriptions for which databases should be unlinked:
# plesk bin subscription -l | sort >> /root/subs.txt
-
Download script unlink.py to the server and extract it:
# wget https://plesk.zendesk.com/hc/article_attachments/360017243319/unlink.tgz && tar xvf unlink.tgz && chmod +x unlink.py
-
Run the script to detach database users and databases from Plesk:
# python unlink.py
Note: Do not change absolute path or name of the file
/root/subs.txt