Saturday, December 10, 2016

remove old mysql binary logs

The binary logs can use lots of storage space. Following are 2 ways to clean them up, by name and by time. 

To remove all binary logs up to mysql-bin.003456:

mysql> PURGE BINARY LOGS TO 'mysql-bin.003456';

To remove all binary logs by date/time (Before December 8th, 2016 @6:00):

mysql> PURGE BINARY LOGS BEFORE '2016-12-08 00:06:00';

Search for MySQL binary logs, error logs, temporary files:

# lsof -nc mysqld | grep -vE '(.so(..*)?$|.frm|.MY?|.ibd|ib_logfile|ibdata|TCP)'

Tuesday, December 6, 2016

Enable https for Nginx

Modify listen line in config file for example /etc/nginx/sites-enabled/default.

listen 80 default_server;

and add listen 443 and certificate/key path. Following example is for Let's Encrypt installation.

listen 80;
listen 443 default ssl;
ssl_certificate /etc/letsencrypt/live/domainname/cert.pem;
ssl_certificate_key /etc/letsencrypt/live/domanname/privkey.pem;

To force http to https, add following in the server block say just after above entries. Myserver.com is used as server_name in following example.

server_name myserver.com;
if ($scheme = http) {
      return 301 https://$server_name$request_uri;
}

Check syntax error in config file:

nginx -t

Reload or restart Nginx server.

/etc/init.d/nginx reload|restart

Saturday, December 3, 2016

Reset MySQL root password


Following is a way to reset MySQL root password when you forgot.
  1. Stop MySQL: /etc/init.d/mysqld stop
  2. Startup MySQL without password prompt: mysqld_safe --skip-grant-tables &
  3. Login to MySQL: mysql --user=root mysql
  4. Reset password: mysql> update user set Password=PASSWORD('new-password') where user='root';
  5. mysql> flush privileges;
  6. mysql> exit;
  7. Kill running mysql daemon: kill mysql-pid
  8. Start MySQL: /etc/init.d/mysqld start

Saturday, November 26, 2016

Troubleshooting logrotate and cron on Ubuntu server

My client's logrotate was not rotate the logs for months. However, manually run logrotate is fine. use debug, verbose, even force options can't find anything funny. The content of /var/lib/logrotate/status looks normal. Finally, check the cron job to see if anything wrong there.
  1. Tail /var/log/cron.log and see "Authentication token is no longer valid; new one required".
  2. Run "chage -l root" to check the aging information of root account, and "password must be changed" message shown up.
  3. Generate a new password from https://strongpasswordgenerator.com, then run passwd command to change to the new passwd for root. 
  4. Check /var/log/cron.log again after a cron job is executed, and the cron.log seems OK. The problem fixed.
Conclusion: Since the cron job is essential for automatic log rotation. If cron job has any problem, the log files don't get rotated even manual run has no problem at all.  


Friday, November 25, 2016

Speed test from Linux Command line

Run following 3 commands to do the test.
  1. wget -O speedtest https://raw.github.com/sivel/speedtest-cli/master/speedtest.py
  2. chmod +x speedtest
  3. ./speedtest
Following is a sample test result:
Retrieving speedtest.net configuration...
Testing from DigitalOcean (x.x.x.x)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by fdcservers.net (New York City, NY) [2.13 km]: 5.872 ms
Testing download speed................................................................................
Download: 832.04 Mbit/s
Testing upload speed....................................................................................................
Upload: 485.81 Mbit/s