Have you tried MySQLTuner yet? It's free and it makes optimizing your MySQL server easier than ever!

Archive for August, 2007

By default, views in MySQL 5.x are created with a security definer set to the root user. However, Plesk drops the root user from MySQL and replaces it with the admin user. When this happens, your views cannot by dumped by mysqldump since the root user (the security definer for the view) doesn’t exist in the mysql.user table.

You receive an error similar to the following:

mysqldump: Couldn't execute 'SHOW FIELDS FROM `some_tablename`': There is no 'root'@'localhost' registered (1449)

Usually, if you run a SHOW CREATE VIEW `tablename`, you’ll see something like this:

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `some_tablename` AS select distinct `some_database`.`some_tablename`.`some_column` AS `alias` from `some_tablename`

You have two options in this situation:

  • Change the security definer for each of your views to ‘admin’@'localhost’. Any new views you create will need to be adjusted as well.
  • Create a root user in MySQL with the same privileges as the admin user and use the root user’s login to run mysqldump.

Comments No Comments »

Sometimes MySQL’s process list will fill with unauthenticated login entries that look like this:

| 971 | unauthenticated user | xxx.xxx.xxx.xxx:35406 | NULL | Connect | NULL | login | NULL |

Generally, this means one of two things are happening. First, this could be a brute force attack against your server from an external attacker. Be sure to firewall off access to port 3306 from the outside world or run MySQL with skip-networking in the /etc/my.cnf file, and that should curtail those login attempts quickly.

However, MySQL could be attempting to resolve the reverse DNS for each connection, and this definitely isn’t necessary if your grant statements refer to remote machines’ IP addresses rather than hostnames (as they should). In this case, add skip-name-resolve to your /etc/my.cnf and restart MySQL. These connection attempts should authenticate much faster, and they shouldn’t pile up in the queue any longer.

Note: Connections via sockets aren’t affected by DNS resolution since sockets don’t involve any networking access at all. If your web applications use ‘localhost’ for their connection string, then MySQL won’t bring DNS resolution into play whatsoever.

Recommended reading: 6.5.9. How MySQL Uses DNS

Comments No Comments »

Help me out! Digg my MySQLTuner script on digg.com!

Don’t have the money or time for a DBA? Use this free MySQL tuning script to review your server’s variables and statistics. It will suggest specific variable changes and point to configuration errors that exist on your server.

read more | digg story

Comments No Comments »

I’ve been flooded with requests for MySQLTuner and I’ve answered them this weekend. Here’s the changes that were made:

  • Specific variable recommendations are made with suggested values as well
  • Odd recommendations have been reduced
  • Some math errors were corrected
  • More configuration items are supported, like table locks, thread caching, table caching and open file limits.

To find out more and to download the script, head on over to mysqltuner.com.

Comments No Comments »

In some situations, you may want to have domain.com as well as *.domain.com point to the same site in Plesk. Plesk will automatically set up hosting for domain.com and www.domain.com within the Apache configuration, but you can direct all subdomains for a particular domain to a certain virtual host fairly easily.

DNS
Add a CNAME or A record for *.domain.com which points to domain.com (for a CNAME), or the domain’s IP (for an A record.

Apache Configuration
Edit the /var/www/vhosts/domain.com/conf/vhost.conf or /home/httpd/vhosts/domain.com/conf/vhost.conf file and enter this information:

ServerAlias *.domain.com

If the vhost.conf didn’t exist before, you will need to run:

# /usr/local/psa/admin/bin/websrvmng -av

Whether the vhost.conf was new or not, you will need to reload the Apache configuration:

# /etc/init.d/httpd reload

Credit for this fix goes to SWSoft’s KB #955

Comments No Comments »

With Plesk 7.5.x, a PHP upgrade to version 5 will cause some issues with Horde. These issues stem from problems with the pear scripts that Horde depends on.

To fix it, run these commands:

# pear upgrade DB
# cp -a /usr/share/pear/DB.php /usr/share/pear/DB/ /usr/share/psa-horde/pear/

Credit for this fix goes to Mike J.

Comments No Comments »

Urchin sometimes takes it upon itself to do some weird things, and this is one of those times. If Urchin has archived a month of data, and then you ask Urchin to parse a log that contains accesses from that archived month, you’ll receive this ugly error:

Unable to open database for writing since it has been archived

To fix it, cd into /usr/local/urchin/data/[profile name]/ and unzip the YYYYMM-archive.zip files, then move the zip files out of the way. Make sure that the unzipped files are owned by the Urchin user and group. You should then be able to re-run your stats without a problem.

Credit for this fix goes to Google

Comments No Comments »

Sometimes you need a good reference, and this is one of those times. MySQL’s perror command returns many error codes, and I’ve provided a listing here. To find errors individually, just run:

# perror 28
OS error code 28: No space left on device

Here’s the list:

OS error code 1: Operation not permitted
OS error code 2: No such file or directory
OS error code 3: No such process
OS error code 4: Interrupted system call
OS error code 5: Input/output error
OS error code 6: No such device or address
OS error code 7: Argument list too long
OS error code 8: Exec format error
OS error code 9: Bad file descriptor
OS error code 10: No child processes
OS error code 11: Resource temporarily unavailable
OS error code 12: Cannot allocate memory
OS error code 13: Permission denied
OS error code 14: Bad address
OS error code 15: Block device required
OS error code 16: Device or resource busy
OS error code 17: File exists
OS error code 18: Invalid cross-device link
OS error code 19: No such device
OS error code 20: Not a directory
OS error code 21: Is a directory
OS error code 22: Invalid argument
OS error code 23: Too many open files in system
OS error code 24: Too many open files
OS error code 25: Inappropriate ioctl for device
OS error code 26: Text file busy
OS error code 27: File too large
OS error code 28: No space left on device
OS error code 30: Read-only file system
OS error code 31: Too many links
OS error code 32: Broken pipe
OS error code 33: Numerical argument out of domain
OS error code 34: Numerical result out of range
OS error code 35: Resource deadlock avoided
OS error code 36: File name too long
OS error code 37: No locks available
OS error code 38: Function not implemented
OS error code 39: Directory not empty
OS error code 40: Too many levels of symbolic links
OS error code 42: No message of desired type
OS error code 43: Identifier removed
OS error code 44: Channel number out of range
OS error code 45: Level 2 not synchronized
OS error code 46: Level 3 halted
OS error code 47: Level 3 reset
OS error code 48: Link number out of range
OS error code 49: Protocol driver not attached
OS error code 50: No CSI structure available
OS error code 51: Level 2 halted
OS error code 52: Invalid exchange
OS error code 53: Invalid request descriptor
OS error code 54: Exchange full
OS error code 55: No anode
OS error code 56: Invalid request code
OS error code 57: Invalid slot
OS error code 59: Bad font file format
OS error code 60: Device not a stream
OS error code 61: No data available
OS error code 62: Timer expired
OS error code 63: Out of streams resources
OS error code 64: Machine is not on the network
OS error code 65: Package not installed
OS error code 66: Object is remote
OS error code 67: Link has been severed
OS error code 68: Advertise error
OS error code 69: Srmount error
OS error code 70: Communication error on send
OS error code 71: Protocol error
OS error code 72: Multihop attempted
OS error code 73: RFS specific error
OS error code 74: Bad message
OS error code 75: Value too large for defined data type
OS error code 76: Name not unique on network
OS error code 77: File descriptor in bad state
OS error code 78: Remote address changed
OS error code 79: Can not access a needed shared library
OS error code 80: Accessing a corrupted shared library
OS error code 81: .lib section in a.out corrupted
OS error code 82: Attempting to link in too many shared libraries
OS error code 83: Cannot exec a shared library directly
OS error code 84: Invalid or incomplete multibyte or wide character
OS error code 85: Interrupted system call should be restarted
OS error code 86: Streams pipe error
OS error code 87: Too many users
OS error code 88: Socket operation on non-socket
OS error code 89: Destination address required
OS error code 90: Message too long
OS error code 91: Protocol wrong type for socket
OS error code 92: Protocol not available
OS error code 93: Protocol not supported
OS error code 94: Socket type not supported
OS error code 95: Operation not supported
OS error code 96: Protocol family not supported
OS error code 97: Address family not supported by protocol
OS error code 98: Address already in use
OS error code 99: Cannot assign requested address
OS error code 100: Network is down
OS error code 101: Network is unreachable
OS error code 102: Network dropped connection on reset
OS error code 103: Software caused connection abort
OS error code 104: Connection reset by peer
OS error code 105: No buffer space available
OS error code 106: Transport endpoint is already connected
OS error code 107: Transport endpoint is not connected
OS error code 108: Cannot send after transport endpoint shutdown
OS error code 109: Too many references: cannot splice
OS error code 110: Connection timed out
OS error code 111: Connection refused
OS error code 112: Host is down
OS error code 113: No route to host
OS error code 114: Operation already in progress
OS error code 115: Operation now in progress
OS error code 116: Stale NFS file handle
OS error code 117: Structure needs cleaning
OS error code 118: Not a XENIX named type file
OS error code 119: No XENIX semaphores available
OS error code 120: Is a named type file
OS error code 121: Remote I/O error
OS error code 122: Disk quota exceeded
OS error code 123: No medium found
OS error code 124: Wrong medium type
OS error code 125: Operation canceled
MySQL error code 126: Index file is crashed
MySQL error code 127: Record-file is crashed
MySQL error code 128: Out of memory
MySQL error code 130: Incorrect file format
MySQL error code 131: Command not supported by database
MySQL error code 132: Old database file
MySQL error code 133: No record read before update
MySQL error code 134: Record was already deleted (or record file crashed)
MySQL error code 135: No more room in record file
MySQL error code 136: No more room in index file
MySQL error code 137: No more records (read after end of file)
MySQL error code 138: Unsupported extension used for table
MySQL error code 139: Too big row
MySQL error code 140: Wrong create options
MySQL error code 141: Duplicate unique key or constraint on write or update
MySQL error code 142: Unknown character set used
MySQL error code 143: Conflicting table definitions in sub-tables of MERGE table
MySQL error code 144: Table is crashed and last repair failed
MySQL error code 145: Table was marked as crashed and should be repaired
MySQL error code 146: Lock timed out; Retry transaction
MySQL error code 147: Lock table is full; Restart program with a larger locktable
MySQL error code 148: Updates are not allowed under a read only transactions
MySQL error code 149: Lock deadlock; Retry transaction
MySQL error code 150: Foreign key constraint is incorrectly formed
MySQL error code 151: Cannot add a child row
MySQL error code 152: Cannot delete a parent row

Comments No Comments »

An often misused and misunderstood aspect of MySQL is the query cache. I’ve seen blog post after blog post online talking about query caching as the most integral and important feature in MySQL. Many of these same posts advocate cranking the variables to the max to give you “ultimate performance.” One of the worst things you can do to a MySQL server is crank your variables up and hope for the best. I’ll try to clear some things up here.

The MySQL query cache is available in MySQL 4.0, 4.1, 5.0, 5.1, and 6.0 (3.23 has no query cache). The goal of the query cache is to hold result sets that are retrieved repeatedly. Since the data is held in memory, MySQL only feeds the data from memory (which is fast) into your application without digging into the tables themselves (which is slow). The result set from the query you’re running and the query in the query cache must be completely identical, or MySQL will pull the data as it traditionally does from the tables.

Queries and result sets must meet certain criteria to make it into the query cache:

  • Must not be prepared statements (See 12.7. SQL Syntax for Prepared Statements)
  • Subqueries are not cached, only the outer query is cached
  • Queries that are run from stored procedures, functions, or triggers are not cached (applies to versions 5.0+ only)
  • The result set must be equal to or smaller than the query_cache_limit (more on this below)
  • The query cannot refer to the mysql database
  • Queries cannot use user variables, user-defined functions, temporary tables or tables with column-level privileges

Besides these rules, all other queries are approved to enter the query cache. This includes wild things such as views, joins, and queries with subqueries.

The MySQL query cache is controlled by several variables:

  • query_alloc_block_size (defaults to 8192): the actual size of the memory blocks created for result sets in the query cache (don’t adjust)
  • query_cache_limit (defaults to 1048576): queries with result sets larger than this won’t make it into the query cache
  • query_cache_min_res_unit (defaults to 4096): the smallest size (in bytes) for blocks in the query cache (don’t adjust)
  • query_cache_size (defaults to 0): the total size of the query cache (disables query cache if equal to 0)
  • query_cache_type (defaults to 1): 0 means don’t cache, 1 means cache everything, 2 means only cache result sets on demand
  • query_cache_wlock_invalidate (defaults to FALSE): allows SELECTS to run from query cache even though the MyISAM table is locked for writing

Explaining the query_cache_type is a little rough. If the query_cache_type is 0:

  • and the query_cache_size is 0: no memory is allocated and the cache is disabled
  • and the query_cache_size is greater than 0: the memory is allocated but the cache is disabled

If the query_cache_type is 1:

  • and the query_cache_size is 0: no memory is allocated and the cache is disabled
  • and the query_cache_size is greater than 0: the cache is enabled and all queries that don’t use SQL_NO_CACHE will be cached automatically

If the query_cache_type is 2:

  • and the query_cache_size is 0: no memory is allocated and the cache is disabled
  • and the query_cache_size is greater than 0: the cache is enabled and queries must use SQL_CACHE to be cached

Now that we have the variables behind us, how can we tell if we’re using the query cache appropriately? Each time a query runs against the query cache, the server will increment the Qcache_hits status variable instead of Com_select (which is incremented when a normal SELECT runs). If the table changes for any reason, its data is rendered invalid and is dropped from the query cache.

It’s vital to understand the performance implications of the query cache:

Purging the cache
If the query cache fills completely, it will be flushed entirely - this is a significant performance hit as many memory addresses will have to be adjusted. Check your Qcache_lowmem_prunes in your status variables and increase the query_cache_size if you find yourself pruning the query cache more than a few times per hour.

Query cache utilization
There’s a simple formula to calculate your query cache efficiency in percentage form:

Qcache_hits / (Com_select + Qcache_hits) x 100

A query cache efficiency percentage of 20% or less points to a performance problem. You may want to shrink your result sets by building more restrictive queries. If that isn’t possible, then you can increase your query_cache_limit so that more of your larger result sets actually make it into the cache. Keep in mind, however, that this will increase your prunes (see the previous paragraph) and can reduce performance. Increasing the query_cache_limit by small amounts and then recalculating your efficiency is a good idea.

Fighting fragmentation
As queries move in and out of the query cache, the memory may become fragmented. This is normally signified by an increase in slow queries, but your query cache efficiency percentage still remains high. In this situation, run FLUSH QUERY CACHE from the MySQL client and keep monitoring your efficiency. If this doesn’t help, you may be better off flushing the cache entirely with RESET QUERY CACHE.

I’ve tried to piece quite a bit of documentation and DBA knowledge into this article, but you may benefit from reviewing the following documentation sections on MySQL.com: 5.2.3. System Variables, 5.2.5 Status Variables, and 6.5.4. The MySQL Query Cache.

Comments No Comments »

Should you find yourself in the situation where you’ve forgotten the Urchin admin password, don’t worry. It’s easily reset with the following command:

cd util ./uconf-driver action=set_parameter table=user name="(admin)" ct_password=urchin

This will set the password to ‘urchin’, and then you can log into Urchin’s web interface and change it to a secure password. The credit for this fix goes to Urchin’s site.

Comments No Comments »