MRKAVANA (mrkavana@gmail.com) - www.facebook.com/kavanathai
Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

Aug 25, 2011

How to defragment or optimize a database in Mysql?


In case you remove a lot of data from the tables OR change the database structure, a de-fragmentation/optimizing of the database is necessary to avoid performance loss, especially while running queries. The above changes results in a performance loss, so make sure you run the “optimizer” on the database.
SSH to your server and execute:
mysqlcheck -o <databasename>
where, -o stands for optimize which is similar to defragmentation. You should look to defragment the tables regularly when using VARCHAR fields since these coloumns get fragmented too often.

Shell script to backup a Mysql database and save it on a remote server using Ftp


The following shell script will dump the mysql database and will save the .sql file on a remote location using Ftp. This script will create a backup file including the current date so you can have multiple copies of the backups of the same database under one directory.
Create a file called mysqlbkup.sh
# vi /root/mysqlbkup.sh
and paste the following code in the file as it is.
##############START OF THE SCRIPT##############
#!/bin/bash
# Specify the temporary backup directory
BKUPDIR="/tmp"
# Database Name
dbname="dbname_here"
# store the current date
date=`date '+%Y-%m-%d'`
# Specify Ftp details
ftpserver="FtpServerIP"
ftpuser="username"
ftppass="password"
# Dump the mysql database with the current date and compress it.
#Save the mysql password in a file and specify the path below
/usr/bin/mysqldump -uroot -p`cat /path/to/passfile` $dbname | gzip > $BKUPDIR/$date.$dbname.sql.gz
# Change directory to the backup directory
cd $BKUPDIR
# Upload the backup
ftp -n $ftpserver <<!EOF!
user $ftpuser $ftppass
binary
prompt
mput *.sql.gz
quit
!EOF!
# Remove the local backup file
/bin/rm -f /$BKUPDIR/$date.$dbname.sql.gz
##############END OF THE SCRIPT##############
Save the file and schedule a cronjob to execute the file on daily basis, say during night hours at 1.00AM. Edit the cron file
# crontab -e
and set the following cronjob
0  1  *  *  *  /bin/sh /root/mysqlbkup.sh
save the file and restart the crond service
# service crond service
The script will work on a Linux/Plesk server as well. You just have to replace the mysqldump line in the script with the following
/usr/bin/mysqldump -uadmin -p`cat /etc/psa/.psa.shadow` $dbname | gzip > $BKUPDIR/$date.$dbname.sql.gz
Make sure you assign the db_name, ftpserver/user/pass values properly at the start of the script.
Note: Leave a comment if you have any suggestions, questions OR have received any error message using this script.

Mysql: Access denied for user ‘root’@'localhost’


You may receive the “Access denied for user ‘root’@'localhost’” message while accessing mysql from the command prompt. The error message states that the Mysql password for user ‘root’ is incorrect and need to reset the password using skip-grant-tables option.
ERROR 1045 (28000): Access denied for user ‘root’@'localhost’ (using password: NO)
How to reset a Mysql password for ‘root’?
# /etc/init.d/mysql stop
Make sure all the mysql processes are stopped by executing the killall command
# killall -9 mysqld
Next, connect to mysql server using the skip-grant-tables method.
# /usr/bin/mysqld_safe –skip-grant-tables &
now, execute ‘mysql’ and you will be at the mysql prompt
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 23056
Server version: xx.xx-community MySQL Community Edition (GPL)
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql>
Goto the ‘mysql’ database and update the password for user ‘root’ in the “user” table.
mysql> use mysql;
To set a password, execute
mysql> update user set password=PASSWORD(“passhere”) where user=’root’;
OR to set a blank password, execute the mysql ‘update user’ query
mysql> update user set password=PASSWORD(“”) where user=’root’;
Once done, reload privileges and quit
mysql> flush privileges;
mysql> quit
Now, restart the mysql service
# /etc/init.d/mysql restart
and you should be able to connect mysql server:
# mysql
OR
# mysql -uroot -p

How to enable ‘General Query Log’ in Mysql?


General Query Log is used to keep track of mysql status i.e. it writes the information when a client connects/disconnects OR a query is executed. It is useful when the number of people managing the database is high. In order to enable ‘General Query Log’,
edit the Mysql configuration file
vi /etc/my.cnf
enable the log under the ‘mysqld’ section
log=/var/log/mysql.general.log
Save the file. Now create the log file and set the mysql ownership
touch /var/log/mysql.general.log
chown mysql.mysql /var/log/mysql.general.log
Now, restart the mysql service
/etc/init.d/mysql restart
You can now execute the queries using phpMyAdmin OR 3rd party sql software and watch the logs
tail -f /var/log/mysql.general.log

Error: Unable to create the domain because a DNS record exists


Error:
Error message “Error: Unable to create the domain example.com because a DNS record pointing to the host example.com already exists.”
The error message is displayed when you add a domain from Plesk control panel and it fails. The reason it fails is because the DNS records of the domain already exist in the psa database. The tables dns_recs and dns_zone holds the DNS records for a domain.
In order to add the domain example.com, you will have to remove the DNS entries from the tables dns_recs and dns_zone.
1) Goto Mysql prompt:
root@host [~]# mysql -uadmin -p `cat /etc/psa/.psa.shadow`
2) Use the psa database
mysql>  use psa;
3) Remove the DNS entries from the dns_recs and dns_zone tables:
mysql> delete from dns_recs where dns_zone_id=10;
mysql> delete from dns_zone where id=10;
where, 10 is the dns_zone_id of the domain example.com.
4) Restart the mysql service:
root@host [~]# service mysqld restart
You should now be able to add the domain from Plesk control panel successfully.


How to enable log-queries-not-using-indexes in mysql


How to enable “log-queries-not-using-indexes” in Mysql?
Edit /etc/my.cnf and place the following line under the [mysqld] section:
log-queries-not-using-indexes
Save the file and restart the mysql service:
# /etc/init.d/mysql restart
To verify if it is set to ON, use the following command:
# mysqladmin variables |grep  log_queries_not_using_indexes

Plesk webmail + DB Error: connect failed


You may receive the following error message while accessing Webmail on a Plesk server:
A fatal error has occurred
DB Error: connect failed
You need to make sure the ‘horde’ user is able to connect to the ‘horde’ databaseusing the password mentioned in the file “/etc/psa/.webmail.shadow”. You have to create the file and specify a random password if the file is missing.
To set the password for user ‘horde’ , go to the mysql prompt.
#mysql -uadmin -p`cat /etc/psa/.psa.shadow` mysql
On the mysql prompt, execute:
mysql> update user set password=password(password-from-.webmail.shadow) where user=’horde’;
Also make sure sql.safe_mode is set to off in /etc/php.ini file:
sql.safe_mode=Off
Restart Apache server once you save the file.
If the problem persists, use the password from “/etc/psa/webmail/horde/.horde.shadow” file. Go through the following steps:
#cp /etc/psa/webmail/horde/.horde.shadow /etc/psa/.webmail.shadow
#mysql -uadmin -p`cat /etc/psa/.psa.shadow` mysql
mysql> update user set password=password(password-from-/etc/psa/webmail/horde/.horde.shadow) where user=’horde’;
You should now be able to access Webmail client.


Grant privileges to a Mysql user?


Create a database:
mysql> create database dbname;
and grant the privileges using the following query:
mysql> grant all on dbname.* to ‘dbuser’@'localhost’ identified by ‘password’;
The user ‘dbuser’ will now have access to database ‘dbname’ locally using the password ‘password’. Using the above command, you can also allow new users to access your database as well. Just replace the ‘dbuser’ and ‘password’ with the new user and password.
To allow the user to access the database remotely say from IP 111.222.333.444, execute:
mysql> grant all on dbname.* to ‘dbuser’@’111.222.333.444‘ identified by ‘password’;

Jul 18, 2011

How to reset user password in mysql in redhat or centos linux

Resetting the mysql user password in safemode. Lets see how to reset the password of root user in Centos / redhat linux.

Step1 : Stop the mysql service

#/etc/init.d/mysqld stop

step2 : Enter mysql safemode

#mysqld_safe --skip-grant-tables &

Step3 : Change the password

mysql> update mysql.user set password=PASSWORD("NEWPASSWORD") where User='root';

Step4 : Flush the privileges

mysql> flush privileges;

Step5 : Its over. Now you can quit.

mysql> quit

Step6 : Start the mysql service

#/etc/init.d/mysqld start

Step7 : Restart the mysql service to confirm that the safemode has been exsited.

#/etc/init.d/mysqld restart


How to set password for root user in mysql database

This explains how to set password for root user from command line interface. 

Login to mysql as root user without password after installing mysql
[root@mysql ~]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>

Changing the root password for the first time
[root@mysql ~]# mysqladmin password abc123

Loging as before without password
[root@mysql ~]# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

Loging with new password
[root@mysql ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>

Changing the current password to another
[root@mysql ~]# mysqladmin -u root -pabc123 password redhat

Dont forget to flush privileges after deleting/modifying users or resetting passwords.

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql>


How to secure anonymous user in mysql

There are two ways for securing the anonymous user.

1. Set a good password for anonymous user.
2. Disable it.

Setting password for anonymous user :

mysql> set password for ''@localhost=password('password');
Query OK, 0 rows affected (0.00 sec)

OR

2. Deleting the anonymous user.

Before:
mysql> select user,host,password from user;
+------+----------------+------------------+
| user | host | password |
+------+----------------+------------------+
| root | localhost | 27c30f0241a5b69f |
| root | mysql.lap.work | 27c30f0241a5b69f |
| root | 127.0.0.1 | 27c30f0241a5b69f |
| | localhost | 27c30f0241a5b69f | ->anonymous user
| | mysql.lap.work | 27c30f0241a5b69f | ->anonymous user
+------+----------------+------------------+
5 rows in set (0.00 sec)

Deleting..
mysql> delete from mysql.user where user = '';
Query OK, 2 rows affected (0.00 sec)

After:
mysql> select user,host,password from user;
+------+----------------+------------------+
| user | host | password |
+------+----------------+------------------+
| root | localhost | 27c30f0241a5b69f |
| root | mysql.lap.work | 27c30f0241a5b69f |
| root | 127.0.0.1 | 27c30f0241a5b69f |
+------+----------------+------------------+
3 rows in set (0.00 sec)
mysql>

Dont forget to flush privileges after deleting/modifying users or resetting passwords.

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql>



mysql> set password for ''@FQDN=password('password');
Query OK, 0 rows affected (0.00 sec)
mysql>

How to create a new user in mysql

Creating a new super user

The command is :
mysql> GRANT ALL PRIVILEGES ON *.* TO 'randeep'@'%' IDENTIFIED BY 'randeep' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from user;
+---------+----------------+------------------+
| user | host | password |
+---------+----------------+------------------+
| root | localhost | 27c30f0241a5b69f |
| root | mysql.lap.work | 27c30f0241a5b69f |
| root | 127.0.0.1 | 27c30f0241a5b69f |
| randeep | % | 0d9b9da57a2d7649 | >> new user (% means all hosts)
+---------+----------------+------------------+
4 rows in set (0.00 sec)
mysql>

Creating a user without grant option :

mysql> grant all privileges on *.* to 'abc'@'localhost' identified by 'abc123';
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from user;
+---------+----------------+------------------+
| user | host | password |
+---------+----------------+------------------+
| root | localhost | 27c30f0241a5b69f |
| root | mysql.lap.work | 27c30f0241a5b69f |
| root | 127.0.0.1 | 27c30f0241a5b69f |
| abc | localhost | 4b5698aa4603595b | >> newly added user.
| randeep | % | 0d9b9da57a2d7649 |
+---------+----------------+------------------+
5 rows in set (0.01 sec)
mysql>


How to harden mysql server

Hardening mysql

First all, before doing anything take the backup of configuration file.
cp /etc/my.cnf{,.orig}
vi /etc/my.cnf

[mysqld]
skip-locking
key_buffer = 384M
max_allowed_packet = 1M
max_connections = 500
max_user_connections = 35
wait_timeout=40
connect_timeout=10
table_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
thread_concurrency = 8
server-id = 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

Save the file.

killall -9 mysqld safe_mysqld mysqld_safe 2>/dev/null
pkill -9 mysql
pkill -9 mysqld

Restart mysql service.
/etc/init.d/mysql restart