Fix for mysql2 error "Incorrect MySQL client library version! This gem was compiled for x.x.x but the client library is y.y.y."

Posted Over 1 year ago by Tobias Kraze.

This should be fixed in the latest LTS-branches of our mysql2 fork, 0.2.x-lts and 0.3.x-lts. Use gem 'mysql2', git: 'https://github.com/makandra/mysql2', branch: '0.2.x...

...lts' # for Rails 2.x gem 'mysql2', git: 'https://github.com/makandra/mysql2', branch: '0.3.x-lts' # for Rails 3.x in your Gemfile, and do a bundle update mysql2 Background

Using multiple MySQL versions on the same linux machine using docker

Posted Over 2 years ago by Daniel Straßner.

We had a card that described how to install multiple mysql versions using mysql-sandbox. Nowadays with the wide adoption of docker it might be easier to use a MySQL...

...docker image for this purpose. Create a new mysql instance docker run --name projectname_db -e MYSQL_ROOT_PASSWORD=secret -p "33008:3306" -d --restart unless-stopped mysql:5.7

MySQL will not use indexes if you query the wrong data type

Posted About 11 years ago by Arne Hartherz.
mysqlperformanceblog.com

When MySQL refuses to use your index, you can do a number of things wrong. One of them might be conditions with improper data types. An example For example, let...

...assume you have a users table with an email field (varchar) which is indexed. MySQL will use the index when your query is well-formed: mysql> EXPLAIN SELECT * FROM users...

MySQL: Careful when using database locks in transactions

Posted Over 10 years ago by Tobias Kraze.

...databases are actually very complicated, and chances are, your making some incorrect assumptions. The MySQL innodb engine actually has four different modes for transactions: READ UNCOMMITTED READ COMMITTED REPEATABLE READ...

...are consistent nonlocking reads. The problem with consistent nonlocking reads If you open a MySQL transaction, the first SELECT statement will establish a "snapshot" of the database's state. All...

uninitialized constant MysqlCompat::MysqlRes (NameError)

Posted Over 11 years ago by Thomas Eisenbarth.

If you get a stacktrace complaining about uninitialized constant MysqlCompat::MysqlRes a system library update might broke your gem. You might have switched from MySQL to MariaDB, but forgot to...

...rebuild your MySQL gems. Try fully removing and re-installing the gem: gem uninstall mysql mysql2 bundle install

Reset mysql root password

Posted Over 11 years ago by Florian Heinle.

This article describes how to reset MySQL's or MariaDB's root password on your workstation. It's meant for local development purposes only, don't do this in production...

...you prefer using a password for root. Solution Step 1 is getting a root mysql shell that allows us to change user credentials. We need to stop the mysql daemon...

Installing multiple MySQL versions on the same Linux with mysql-sandbox

Posted Over 10 years ago by Henning Koch.

Ubuntu has a package mysql-sandbox that lets you install multiple MySQL versions into your user home: Install mysql-sandbox sudo apt install mysql-sandbox Download the version of MySQL...

...you want to use from mysql.com: https://dev.mysql.com/downloads/file/?id=480427 Make sure to choose "Generic Linux" instead of "Ubuntu" so you get a .tar.gz instead of .deb cd into the directory...

Don't ever use the float type for database columns

Posted About 10 years ago by Henning Koch.

Like in any language, a FLOAT will eventually corrupt data due to rounding errors. Please use DECIMAL, which has well...

Do not pass an empty array to ActiveRecord.where when using NOT IN

Posted About 11 years ago by Andreas Robecke.

Be careful with the Active Record where method. When you accidentally pass an empty array to the where method using...

Understanding race conditions with duplicate unique keys in Rails

Posted About 11 years ago by Henning Koch.

validates_uniqueness_of is not sufficient to ensure the uniqueness of a value. The reason for this is that in...

Don't compare datetimes with date ranges in MySQL and PostgreSQL

Posted Over 12 years ago by Henning Koch.

When selecting records in a date range, take care not to do it like this: start_date = Date.parse('2007-05...

mysqltuner.pl

Posted Almost 13 years ago by Lexy.
mysqltuner.pl

This Perl script will run diagnostics on your MySQL database and recommend changes to your MySQL configuration...

PostgreSQL cheat sheet for MySQL lamers

Posted Over 10 years ago by Thomas Eisenbarth.

So you're switching to PostgreSQL from MySQL? Here is some help... General hints on PostgreSQL \? opens the command overview \d lists things: \du lists users, \dt lists tables etc...

...Command comparison Description MySQL command PostgreSQL equivalent Connect to the database mysql -u $USERNAME -p sudo -u postgres psql Show databases SHOW DATABASES; \l[ist] Use/Connect to a database named...

Simple database lock for MySQL

Posted About 13 years ago by Tobias Kraze.

Note: For PostgreSQL you should use advisory locks. For MySQL we still recommend the solution in this card. If you need to synchronize multiple rails processes, you need some shared...

...can be used as a mutex. One option is to simply use your existing (MySQL) database. The attached code provides a database-based model level mutex for MySQL. You use...

In MySQL, a zero number equals any string

Posted About 12 years ago by Andreas Robecke.

In MySQL comparing zero to a string 0 = "any string" is always true! So when you want to compare a string with a value of an integer column, you have...

...knowing a secret token: Potential Query Manipulation with Common Rails Practises CVE-2013-3211 MySQL madness and Rails

MySQL: CONCAT with NULL fields

Posted Over 13 years ago by Tobias Kraze.

CONCAT('foo', 'bar', NULL) = NULL the NULL always wins in MySQL. If you would rather treat NULL as an empty string, use CONCAT_WS (concatenation with separator) instead...

MySQL: Disable query cache for database profiling

Posted Over 13 years ago by Tobias Kraze.

...want to see how long your database queries actually take, you need to disable MySQL's query cache. This can be done globally by logging into a database console, run...

ActiveRecord::Base.uncached do yield end true end Don't forget to re-enable MySQL query caching later with SET GLOBAL query_cache_type=ON...

ActiveRecord::StatementInvalid: Mysql2::Error: closed MySQL connection

Posted Over 10 years ago by Andreas Robecke.

I recently experienced the error ActiveRecord::StatementInvalid: Mysql2::Error: closed MySQL connection. Apparently this happens when there is a timeout during query execution. In order to fix this you can...

All TEMPORARY tables are closed (and dropped). ... For further details see the mysql doc. You can actually force this error by setting a short timeout and sleeping in...

MySQL: Can I speed up LIKE queries by adding an index?

Posted Over 11 years ago by Henning Koch.

For string columns, MySQL indexes the left side of a string. That means an index can speed a like query that has a wildcard on the right side:

How to add a user with all privileges to MariaDB

Posted Almost 6 years ago by Emanuel.

Add a user without password (recommended) Replace newuser with your desired username: mysql -uroot -p CREATE USER 'newuser'@'localhost' IDENTIFIED BY ''; GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost'; FLUSH PRIVILEGES...

...by a password, you can remove the password. Replace existinguser with your desired username: mysql -uroot -p SET PASSWORD FOR existinguser@localhost=''; FLUSH PRIVILEGES; exit; Add a user with unix...

PostgreSQL vs MySQL: How to UPDATE using a JOIN

Posted Almost 9 years ago by Arne Hartherz.

...lives in another table, you need to JOIN both tables during the UPDATE. In MySQL you can do it like this: UPDATE employees LEFT JOIN departments ON employees.department_id = departments.id...

Show and change MySQL default character set

Posted Over 12 years ago by Kim Klotz.

To show the MySQL default character set you have to login to the MySQL console and execute SHOW VARIABLES LIKE 'char%'; mysql> SHOW VARIABLES LIKE 'char%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+

...to uft8 you have to add this default-character-set = utf8 to the client, mysqld_safe, mysqld and mysqldump section (may differ depending on configuration) in your my.cnf and restart...

Hints for debugging MySQL InnoDB deadlocks

Posted Over 13 years ago by Tobias Kraze.

...in different order. Solving deadlocks is potentially complicated, so here are a few pointers: MySQL should always detect the deadlock right when it happens, and will throw an error to...

...still cannot see what actually happened, you might need to get better acquainted with MySQLs locking model. While it keeps out of your way usually, it's actually pretty complex...

Understanding SQL compatibility modes in MySQL and MariaDB

Posted About 6 years ago by Henning Koch.

MySQL and MariaDB have an SQL mode setting which changes how MySQL behaves. The SQL mode value is comprised of multiple flags like "STRICT_TRANS_TABLES, NO_ZERO_IN_DATE...

...or disables a particular behavior. The default SQL mode varies widly between versions of MySQL and MariaDB. In general, more recent versions of MySQL and MariaDB have stricter settings than...