Archive for the ‘MySQL’ Category
Optimize MySQL performance with mysqltuner
The following tutorial describes the steps to optimize the performance of a MySQL database with the mysqltuner script.
Login to your server on the shell, then execute the following commands:
Download the mysqltuner script:
cd /usr/local/bin wget http://mysqltuner.pl/mysqltuner.pl chmod +x mysqltuner.pl
Run mysqltuner
/usr/local/bin/mysqltuner.pl
Then enter root as username and the mysql root password.
You will get a output similar to this:
root@v221:/usr/local/bin# /usr/local/bin/mysqltuner.pl
>> MySQLTuner 1.0.1 - Major Hayden <major@mhtx.net> >> Bug reports, feature requests, and downloads at http://mysqltuner.com/ >> Run with '--help' for additional options and output filtering Please enter your MySQL administrative login: root Please enter your MySQL administrative password:
-------- General Statistics -------------------------------------------------- [--] Skipped version check for MySQLTuner script [OK] Currently running supported MySQL version 5.0.51a-24+lenny2 [!!] Switch to 64-bit OS - MySQL cannot currently use all of your RAM
-------- Storage Engine Statistics ------------------------------------------- [--] Status: +Archive -BDB -Federated +InnoDB -ISAM -NDBCluster [--] Data in MyISAM tables: 26M (Tables: 215) [!!] InnoDB is enabled but isn't being used [!!] Total fragmented tables: 33
-------- Performance Metrics ------------------------------------------------- [--] Up for: 96d 23h 3m 41s (10M q [1.239 qps], 686K conn, TX: 701M, RX: 1B) [--] Reads / Writes: 44% / 56% [--] Total buffers: 58.0M global + 2.6M per thread (100 max threads) [OK] Maximum possible memory usage: 320.5M (12% of installed RAM) [OK] Slow queries: 0% (20/10M) [OK] Highest usage of available connections: 33% (33/100) [OK] Key buffer size / total MyISAM indexes: 16.0M/8.5M [OK] Key buffer hit rate: 99.9% (57M cached / 30K reads) [OK] Query cache efficiency: 78.6% (5M cached / 6M selects) [!!] Query cache prunes per day: 483 [OK] Sorts requiring temporary tables: 0% (0 temp sorts / 408K sorts) [!!] Temporary tables created on disk: 36% (269K on disk / 745K total) [OK] Thread cache hit rate: 99% (427 created / 686K connections) [!!] Table cache hit rate: 2% (64 open / 3K opened) [OK] Open file limit used: 11% (120/1K) [OK] Table locks acquired immediately: 99% (3M immediate / 3M locks)
-------- Recommendations ----------------------------------------------------- General recommendations: Add skip-innodb to MySQL configuration to disable InnoDB Run OPTIMIZE TABLE to defragment tables for better performance Enable the slow query log to troubleshoot bad queries When making adjustments, make tmp_table_size/max_heap_table_size equal Reduce your SELECT DISTINCT queries without LIMIT clauses Increase table_cache gradually to avoid file descriptor limits Variables to adjust: query_cache_size (> 16M) tmp_table_size (> 32M) max_heap_table_size (> 16M) table_cache (> 64)
The script recommends to adjust or add the following variables in the mysql my.cnf file. The location of my.cnf is normally /etc/my.cnf or /etc/mysql/my.cnf depending on the Linux distribution that is installed on your server.
Open the my.cnf file:
vi /etc/mysql/my.cnf
and increase or set the variables in the [mysqld] section of the file. Mine looks now like this:
[mysqld] # # * Basic Settings # user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp language = /usr/share/mysql/english skip-external-locking # # Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure. bind-address = 127.0.0.1 # # * Fine Tuning # key_buffer = 16M max_allowed_packet = 16M thread_stack = 128K thread_cache_size = 8 # This replaces the startup script and checks MyISAM tables if needed # the first time they are touched myisam-recover = BACKUP #max_connections = 100 table_cache = 128 #thread_concurrency = 10 # # * Query Cache Configuration # query_cache_limit = 1M
query_cache_size = 32M tmp_table_sizee = 64M max_heap_table_sizee = 32M
Then save the file and restart mysql. After a few hours, rerun mysqltuner and check again if the values are fine now or if the have to be increased to a higher value.
How to reset the administrator password in ISPConfig 3
If you lost your ISPConfig 3 administrator password, you can reset it with the following SQL query.
UPDATE sys_user SET passwort = md5('admin') WHERE username = 'admin';
The SQL query sets the password to “admin” for the user “admin”, it has to be executed in the ISPConfig mysql database, e.g. with phpmyadmin. If you dont have phpmyadmin installed, then the query can be executed with the mysql commandline utility as well:
Login to the mysql database.
mysql -u root -p
and enter the password of the mysql root user. To switch to the ISPConfig database, run this command:
use dbispconfig;
Now execute the SQL command:
UPDATE sys_user SET passwort = md5('admin') WHERE username = 'admin';
and close the mysql shell:
quit;
How to reset the MySQL root password
The following steps describe the procedure to reset the mysql root password on Linux.
1) Stop the mysql server
/etc/init.d/mysql stop
2) Start the mysql server manually without permission tables which allows us to login as root user without password:
mysqld_safe --skip-grant-tables &
3) Login into mysql as root user without a password and switch to the “mysql” database:
mysql -u root mysql
Then execute this SQL query to set a new password for the mysql root user:
update user set Password=PASSWORD('mynewpassword') WHERE User='root';
(Replace “mynewpassword” with the new root password in the above command).
Then logout from the mysql prompt by typing:
exit
4) Now bring back the running mysql instance into the foreground by typing:
fg
and then press [ctrl] + c to kill the mysql process.
5) Start the mysql server again:
/etc/init.d/mysql start
How to enable the query log in MySQL
To debug applications which use a mysql database, it comes in handy to enable the query log to get all SQL queries that were sent to the database. Open the MySQL configuration file (my.cnf)
vi /etc/mysql/my.cnf
and add the line:
log=/var/log/mysql.log
in the [mysql] section of the file. Depending on the Linux distribution that you use, the my.cnf file can be /etc/my.cnf or /etc/mysql/my.cnf.
Afterwards restart mysql to apply the new the configuration:
/etc/init.d/mysql restart
Install postfix with MySQL support on CentOS 5.3 (64Bit)
The following article explains the steps to compile postfix MTA with MySQL support on CentOS 5.3 as e.g. needed for ISPConfig 3.
Remove the postfix package from CentOS which does not has MySQL support:
/etc/init.d/postfix stop rpm -e --nodeps postfix Download the postfix source RPM and install it: <p class="command">cd /tmp wget http://mirror.rackspace.com/CentOS/5.3/os/SRPMS/postfix-2.3.3-2.1.el5_2.src.rpm rpm -i postfix-2.3.3-2.1.el5_2.src.rpm Edit the spec file to enable MySQL support. <p class="command">vi /usr/src/redhat/SPECS/postfix.spec
and change the line:
%define MYSQL 0
to:
%define MYSQL 1
Install a few prerequisites before we build the new postfix RPM package
yum install pcre-devel mysql-devel
Build the postfix RPM with MySQL support:
rpmbuild -ba /usr/src/redhat/SPECS/postfix.spec
and install it.
cd /usr/src/redhat/RPMS/x86_64
rpm -i postfix-2.3.3-2.1.x86_64.rpm
Start postfix and configure it to be started at boot time:
/etc/init.d/postfix start
chkconfig –levels 235 postfix on
Backup and restore mysql databases on the shell
One way to create a backup of a mysql database on the shell is to use the mysqldump command. Mysqldump creates a dump of the database in form of sql commands.
Backup
mysqldump -u root -p mydatabase > /tmp/backup_mydatabase.sql
This command creates a backup of the database with the name “mydatabase” in the file /tmp/backup_mydatabase.sql
Restore
To restore the backup, use the command:
mysql -u root -p mydatabase < /tmp/backup_mydatabase.sql