Tag Archives: MySQL

low_priority_updates=1

By default MySQL treats updates as higher priority operations. You can use SELECT HIGH_PRIORITY or UPDATE LOW_PRIORITY to adjust that or you can simply set low_priority_updates option. Anyway default behavior means any UPDATE statement which is blocked by long running select will also block further selects from this table – they will have to wait until UPDATE is executing which is waiting on SELECT to complete. This is often not accounted for and people think – “OK. I write my script so it does short updates so it will not block anything” – it still may cause total block if there are long selects running.

low_priority_updates=1
concurrent_insert=2

mysql enable remote access

Edit /etc/my.cnf and locate line that reads as follows
[mysqld]

Make sure line skip-networking is commented (or remove line) and add the following line:
bind-address=[YOUR.SERVER.IP.ADDRESS]

For example, if your MySQL server IP is 10.10.10.1 the config should look like as follows:
[mysqld]
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
bind-address = 10.10.10.1
# skip-networking

Also make sure you have access from outside for that user:
GRANT USAGE ON *.* TO '[User1]'@'%' IDENTIFIED BY '[password]';

mysql reset root password

Stop mysql services:

/etc/init.d/mysql stop

Start mysql in safe mode and skip user authentication:
mysqld_safe --skip-grant-tables &

Login to mysql as root, but you need use other console for this:
mysql -u root

Set a new password:
UPDATE mysql.user SET Password=PASSWORD('new_pass') WHERE User='root';

When you modify the grant tables manually you need to run:
flush privileges;

Restart the mysql services:
/etc/init.d/mysql start

mysql user management

Show users
SELECT user, host, password FROM mysql.user;

Create new user accessible only on localhost
CREATE USER '[User1]'@'localhost' IDENTIFIED BY '[password]';

Create new user accessible from anywhere
CREATE USER '[User1]'@'%' IDENTIFIED BY '[password]';

Grant permissions to all databases for User1 from localhost only
GRANT ALL PRIVILEGES ON *.* TO 'User1'@'localhost' WITH GRANT OPTION;

Grant permissions to specified database for User1 from anywhere
GRANT ALL PRIVILEGES ON [db_name].* TO 'User1'@'%' WITH GRANT OPTION;

Create a new user and grant permissions to database at once
GRANT ALL PRIVILEGES ON [db_name].* To 'user'@'hostname' IDENTIFIED BY '[password]';

Show users permissions
SHOW GRANTS FOR [username]@localhost;

mysql fast hints

Show MySQL uptime:
mysqladmin version

Login to MySQL
mysql -h [hostname] -u [username] -p

Show databases
show databases;

Create database
create database [name];

Delete database
drop database [name];

Use database
use [database];

Show tables
show tables;

Show columns
show columns from [table];

Find and replace string
update [table_name] set [column_name] = replace([column_name],'[string_to_find]','[string_to_replace]');

To backup and restore a mysql database see one of my earlier posts here:
http://www.homecomputerlab.com/backing-up-mysql-databases

 

mysql usage hints

Working with MySQL Database

Create a database with a specified name if it does not exist in database server

1
CREATE DATABASE [IF NOT EXISTS] database_name

Use database or change current database to another database you are working with

1
USE database_name

Drop a database with specified name permanently. All physical file associated with the database is no longer exists.

1
DROP DATABASE [IF EXISTS] database_name

Show all available databases in the MySQL database server

1
SHOW DATABASES

 

Working with Table

Lists all tables in a database.

1
SHOW TABLES

Create table statement that defines the structure of table .

1
2
3
4
5
6
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] table_name(
   key type(size) NOT NULL PRIMARY KEY AUTO_INCREMENT,
   c1 type(size) NOT NULL,
   c2 type(size) NULL,
   …
) Engine= [InnoDB|MyISAM]|…]

Altering table structure

Actions can be one of the following actions:

1
ADD [COLUMN]

Add a new column into a table

1
DROP [COLUMN]

Drop an existing column in a table

1
ADD INDEX [name](column_name, …)

Add index with a specific name to a table on a column

1
DROP INDEX index_name Drop an index from a table

Add primary key into a tables

1
ADD PRIMARY KEY (column_name,…)

Remove primary key from a table

1
DROP PRIMARY KEY

Deleting table permanently

1
2
DROP TABLE [IF EXISTS] table_name [, name2, …]
[RESTRICT | CASCADE]

Get information about the table or column.

1
2
DESCRIBE table [column_name]
DESC table [column_name

 

Working with Index

Creating an index with the specified name on a table

1
2
CREATE [UNIQUE|FULLTEXT] INDEX index_name
ON table (column_name,…)

Removing a specified index from table

1
DROP INDEX index_name

 

Querying Data

Query complete data in a database table

1
SELECT * FROM table_name

Query specified data which is shown in the column list from a database table

1
2
SELECT column_name, column_name2….
FROM table_name

Query unique records

1
2
SELECT DISTINCT (column_name)
FROM table_name

Query data from multiples table using join

1
2
3
SELECT *
FROM table_name1
INNER JOIN table_name2 ON conditions

 

1
2
3
SELECT *
FROM table_name1
LEFT JOIN table_name2 ON conditions

 

1
2
SELECT *
FROM table_name1

Counting number of rows in a database table

1
2
SELECT COUNT (*)
FROM table_name

Sorting ascending or descending based on one or more columns:

1
2
3
SELECT column_name, column_name2….
FROM table_name
ORDER BY column_name ASC [DESC], column_name2 ASC [DESC],…

Group the retrieved rows data:

1
2
3
SELECT *
FROM table_name
GROUP BY column_name

 

Matching Data based on a pattern

Matching data using LIKE operator:

1
2
SELECT * FROM table_name
WHERE column_name LIKE%value%’

 

Matching data using regular expression

 

1
2
SELECT * FROM table_name
WHERE column_name RLIKE ‘regular_expression’

MySQL database size limit

The effective maximum table size for MySQL databases is usually determined by operating system constraints on file sizes, not by MySQL internal limits. The following table lists some examples of operating system file-size limits. This is only a rough guide and is not intended to be definitive. For the most up-to-date information, be sure to check the documentation specific to your operating system.

Operating System File-size Limit
Win32 w/ FAT/FAT32 2GB/4GB
Win32 w/ NTFS 2TB (possibly larger)
Linux 2.2-Intel 32-bit 2GB (LFS: 4GB)
Linux 2.4+ (using ext3 file system) 4TB
Solaris 9/10 16TB
MacOS X w/ HFS+ 2TB
NetWare w/NSS file system 8TB

MySQL Can’t open file: ‘.//mysql_table.frm’ (errno: 24)

this error means that too many files are open for the given process. There is a read-only mysql variable called open_files_limit that will show how many open files are allowed by the mysql server.

SHOW VARIABLES LIKE 'open%'

A lot systems set this to something very low, like 1024. Unfortunately, the following will NOT work:

SET open_files_limit=100000

MySQL will respond with:

ERROR 1238 (HY000): Variable 'open_files_limit' is a read only variable

However, it is possible to make a change to /etc/my.cnf. This file may not exist, if not, just create it. Be sure it has the following contents:

[mysqld]
open_files_limit = 100000

Then, be sure to restart mysql:

sudo /etc/init.d/mysql restart

Now, SHOW VARIABLES LIKE 'open%' should show 100000. The number you use may be different.