What is the safest way to reinstall MySQL / MariaDB?
After installing a LAMP stack on Debian Stretch I encountered all sorts of issues with MySQL / MariaDB (couldn't log in as root from my normal user and then root had no privileges). I thought the easiest solution would be to reinstall MariaDB:
# apt-get purge mysql-server mysql-client mysql-common mysql-server-core-5.5 mysql-client-core-5.5
# rm -rf /etc/mysql /var/lib/mysql
# apt-get autoremove
# apt-get autoclean
# apt-get install mariadb-server mariadb-client
However, the install fails:
Jan 24 23:07:55 debian mysqld[5243]: 2017-01-24 23:07:55 140080623286144 [Note] InnoDB: Initializing buffer pool, size = 128.0M
Jan 24 23:07:55 debian mysqld[5243]: 2017-01-24 23:07:55 140080623286144 [Note] InnoDB: Completed initialization of buffer pool
Jan 24 23:07:55 debian mysqld[5243]: 2017-01-24 23:07:55 140080623286144 [Note] InnoDB: Highest supported file format is Barracuda.
Jan 24 23:07:55 debian mysqld[5243]: 2017-01-24 23:07:55 140080623286144 [Note] InnoDB: 128 rollback segment(s) are active.
Jan 24 23:07:55 debian mysqld[5243]: 2017-01-24 23:07:55 140080623286144 [Note] InnoDB: Waiting for purge to start
Jan 24 23:07:55 debian mysqld[5243]: 2017-01-24 23:07:55 140080623286144 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.6.34-79.1 started; log sequence number 1622848
Jan 24 23:07:55 debian mysqld[5243]: 2017-01-24 23:07:55 140080018601728 [Note] InnoDB: Dumping buffer pool(s) not yet started
Jan 24 23:07:55 debian mysqld[5243]: 2017-01-24 23:07:55 140080623286144 [Note] Plugin 'FEEDBACK' is disabled.
Jan 24 23:07:55 debian mysqld[5243]: 2017-01-24 23:07:55 140080623286144 [Note] Server socket created on IP: '127.0.0.1'.
Jan 24 23:07:55 debian mysqld[5243]: 2017-01-24 23:07:55 140080623286144 [ERROR] Can't start server: Bind on TCP/IP port. Got error: 98: Address already in use
Jan 24 23:07:55 debian mysqld[5243]: 2017-01-24 23:07:55 140080623286144 [ERROR] Do you already have another mysqld server running on port: 3306 ?
Jan 24 23:07:55 debian mysqld[5243]: 2017-01-24 23:07:55 140080623286144 [ERROR] Aborting**
Jan 24 23:07:55 debian mysqld[5243]:
Jan 24 23:07:58 debian mysqld_safe[5297]: mysqld from pid file /var/run/mysqld/mysqld.pid ended
Jan 24 23:08:25 debian /etc/init.d/mysql[5552]: 0 processes alive and '/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf ping' resulted in
Jan 24 23:08:25 debian /etc/init.d/mysql[5552]: [61B blob data]
Jan 24 23:08:25 debian /etc/init.d/mysql[5552]: error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")'
Jan 24 23:08:25 debian /etc/init.d/mysql[5552]: Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!
Jan 24 23:08:25 debian /etc/init.d/mysql[5552]:
Jan 24 23:08:25 debian mysql[5070]: Starting MariaDB database server: mysqld . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . failed!
Jan 24 23:08:25 debian systemd[1]: mysql.service: Control process exited, code=exited status=1
Jan 24 23:08:25 debian systemd[1]: Failed to start LSB: Start and stop the mysql database server daemon.
I gather from the output that the database wasn't completely removed ("Do you already have another mysqld server running on port: 3306?"). Is there a better way to try and reinstall MySQL / MariaDB.
Solution 1:
The purge failed because mysqld was still running (and listening on port 3306).
# lsof -i TCP:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 12629 mysql 17u IPv4 78816 0t0 TCP localhost:mysql (LISTEN)
I should have killed the process before uninstalling. After killing the process I could reinstall MySQL / MariaDB:
# kill -9 12629
# apt-get install mariadb-server mariadb-client
# systemctl restart mysql
# systemctl restart apache
Solution 2:
I think it's important to note that one doesn't necessarily have to use purge
to reinstall a package.
In fact, before trying to purge everything (in which case I hope you backed up everything carefully), one can simply use
$ sudo apt-get --reinstall install mariadb-server mariadb-client
Should this approach fail however, you can try the answers in Command to purge and reinstall package or Reinstall package and its installed dependencies