The best way to do this is to back up the required database and reconfigure the server.
Backup
The mysqldump command is used to create text dumps of databases managed by MySQL. These dumps are just files with all the SQL commands needed to recreate the database from scratch. The process is quick and easy.
If you want to back up one database, you simply dump and send the output to a file, for example:
mysqldump database_name > database_name.sql
You can back up several databases at the same time:
mysqldump --databases database_one database_two > two_databases.sql
In the above code, database_one is the name of the first backup of the database, and database_two is the name of the second.
It is also easy to back up all databases on the server:
mysqldump --all-databases > all_databases.sql
After performing the backup, uninstall mysql and reinstall it. After reinstalling with the desired port number.
Backup recovery
Since dump files are only SQL commands, you can restore the database backup by telling mysql to run the commands in it and put the data in the appropriate database.
mysql database_name < database_name.sql
In the above code, the database name is the name of the database you want to restore, and database_name.sql is the name of the backup file to be restored.
If you are trying to restore one database from a dump of all databases, you should tell mysql as follows:
mysql --one-database database_name < all_databases.sql