Articles in the MySQL Category
MySQL, debian »
The quickest way to disable autostart of services in Debian is to utilize a convenient curses interface to selectively choose which services to autostart in different run levels. Its so much easier to use than update-rc.d. Its good for instances where you are migrating services out of an existing box, such as removing the mysql server without uninstalling.
The package is called:
sysv-rc-conf
(Install it using aptitude or apt-get install)
MySQL, PHP »
The error message looked like:
“Illegal mix of collations (latin1_swedish_ci,COERCIBLE) and (utf8_general_ci,IMPLICIT) for operation ‘find_in_set’”
I ran into this problem today while importing text from a CSV file. Evidently, the text format of the document had UTF characters, which caused CodeIgniter to error out when it tried to insert it to the database. The line had French characters (The ç in Français) My database fields by default are set to “latin1_swedish_ci” character set.
The fix?
I decided the best way to fix this was to set the Collation option for that specific field in the MySQL database to “utf8_general_ci
MySQL »
MySQL by default will make the ORDER BY option in queries case sensitive. This means that rows with the same starting letters (but different case) may be ordered non-alphabetically. There are three solutions to this. One is to force all database entries to be forced to upper-case or lower-case when inserting. Another is to use the collate option. The other solution is to change the query itself.
MySQL »
Here’s the quickest way to sort data in MySQL after retrieving the last 10 entries with the LIMIT and ORDER BY DESC commands. If you simply do a ORDER BY DESC LIMIT 10, this will return the last 10 results in reverse order (newest first). Adding an inline SELECT command will sort the results after retrieving the last 10 items. (oldest first)
SQL Command
$sql = "SELECT * FROM (SELECT * FROM [TABLE] WHERE ORDER BY modified_date DESC LIMIT 10) AS tbl ORDER BY tbl.modified_date"
This will retrieve the last 10 rows while sorting by date chronologically. I used this SQL command for …
MySQL »
If your site experiences the infamous “Mysql : Too many connections” error, you will need to tune your MySQL server to handle the load of a heavily used website.
There are some configuration variables that you can adjust to make your site handle more connections.
The wait_timeout default in MySQL is 28840 seconds (8 hours), which can seriously limit the number of open connections you have on your page. I adjusted mine to 60 seconds. This frees up the thread after idle timeout, which allows for more connections to connect. The 8 hour timeframe is fine if you only have a few connections to the server. For popular websites, its best to make this value as low as possible. Set this variable to the maximum time your page typically runs. For instance, if you have a page that needs to retrieve some data processed from another server (60-80 seconds) and you only use one mysql_connect, you may lose the mysql connection for processing the remaining parts of the page if your wait_timeout is too low. If the processing takes much longer, a workaround is to use multiple mysql_connects to prevent an idle connection from disconnecting.
MySQL, PHP »
I’ve been working on tuning my Apache/MySQL/PHP configuration lately to see how to improve the performance. Lately, I’ve been noticing some crawlers that have been ignoring my robots.txt file and hitting my server really hard with several page requests every second. So, I decided to run some tests to see where I could speed up my website in the code.
MySQL, PHP »
WordPress introduced a new version of its multi-user blog software in January 2009. I have been using version 2.6 for the past few months and it was working well, although the site management was very confusing to use. Version 2.7 fixes this problem by making the Site Admin menu more uniform with the main dashboard.
MySQL, PHP »
A good practice is to check input strings to make sure users don’t put in mySQL commands in your server. For instance, if a username or password POST variable isn’t filtered, there is a potential for an injection like ‘OR myusername=’. In the past, I’ve been using my own PHP toolkit to “clean” the input variables. But recently, I began searching to see if there are a built-in solution in PHP for this, especially since I’m converting a script written in Python that had the filter MySQLdb.escape_string. Enter mysql_real_escape_string()
MySQL, PHP »
Converting the datetime format is really simple if you let MySQL do the formatting for you.
MySQL 5.1 SELECT DATE_FORMAT manual
For instance, if you wanted to convert the datetime into something that reads
function ConvertNiceDateOnly($date)
{
// Convert mySQL date to nice formatted date
$normaldate = mysql_query("SELECT DATE_FORMAT('$date','%M %e, %Y')");
$normaldate = mysql_fetch_row($normaldate);
$normaldate = $normaldate[0];
return $normaldate;
}
MySQL, PHP »
$unixseconds = strtotime($mysqldate);
For instance, you can use this to write a timeout script for login failures. Usually, a system should lock after 3-5 consecutive failed login attempts. I save the timestamp after the 5th consecutive login failure, then run a check on this timestamp if the current time is within the ~5-10 minute lockout window. 5 minutes is 300 seconds, 10 minutes is 600 seconds.
