Thursday, July 25, 2013

Moving MySQL's Data Directory on a New Install on Linux

It seems pretty simple on a new install. You can copy the files from /var/lib/mysql to the new location. If you want to initialize it from scratch:

1. set up the datadir variable in /etc/my.cnf under [mysqld] like so:

[mysqld]
datadir           = /my/path/to/data/directory



2. You'll need to run the mysql_install_db perl script as the mysql user like so:

su - mysql -c "mysql_install_db --datadir=/my/path/to/data/directory"

or, if you have sudo configured:

sudo -u mysql mysql_install_db --datadir=/my/path/to/data/directory


2b. If you're using selinux, copy the security contexts from /var/lib/mysql recursively like so:

chcon -R --reference=/var/lib/mysql /my/path/to/data/directory



3. Now, you should be able to start MySQL from init or using the service command (on Redhat based distributions)

sudo /etc/init.d/mysql start

or 

sudo /sbin/service mysql start




Monday, July 1, 2013

One Line Windows Touch Command (Windows 7, Windows 8, etc.)

I was looking around the web for an easy way to simulate the unix command touch - but I kept finding old variants that would not work past Windows ME. I worked with the FOR loop in batch scripting. Here's the version that works for me:

To set all CSV files to the current time:

for /F "usebackq delims=|"  %i in (`dir /B *.csv`) do copy %i /B+ ,,/Y

Note: to set a single file to the current date:

copy filenamt /B+ ,,/Y

A lot of sites suggested dir with no /B - but this doesn't work for me in Windows 8.

Of course, you can "touch" all files like so:

for /F "usebackq delims=|"  %i in (`dir /B *.*`) do copy %i /B+ ,,/Y