Thursday, August 9, 2012

Install MySQL and pymysql on CentOS 6

Install MySQL-server and MySQL client:

yum install mysql-server mysql

install the required connector (for python on our case):

yum install MySQL-python

We already had some scripts that used pymysql instead of MySQL-python driver
so we also installed pymysql using pip or easy_install. easy_install comes with python-setuptools:

yum install python-setuptools

I prefer pip, so:

easy_install pip

pip install pymysql

Make MySQL daemon to start automatically at login:

chkconfig --levels 235 mysqld on

Start mysqld manually for the first time:

service mysqld start

Login to mysql

mysql -u root

Set password for root

SET PASSWORD FOR 'root'@'localhost'=PASSWORD('some-password');
SET PASSWORD FOR 'root'@'localhost.localdomain'=PASSWORD('some-password);

SET PASSWORD FOR 'root'@'127.0.0.1'=PASSWORD('some-password');

Please note that localhost.localdomain should be replaced with FQDN of the host.

Drop the Any User:

DROP USER ''@'localhost';
DROP USER ''@'localhost.localdomain';

Create the required database:

CREATE DB MYTEST;

Create a new user with full rights on this db only:

CREATE USER 'myUser'@'localhost' IDENTIFIED BY 'some-password';
GRANT ALL PRIVILEGES ON MYTEST.*  TO myUser;
FLUSH PRIVILEGES;
(Note that myUser can only connect from localhost)
EXIT

It is probably best for security reasons to allow connections from localhost only. Add the following line to /etc/my.cnf under [mysqld] and [mysqld_safe]

skip-networking

P.S. make sure not to leave any orphan accounts having access to the db.