Howto: repair a mysql table

October 4, 2009    |   Posted by admin   |    Category: Mysql & PostGres SQL

If you receive the following error message on your website, which means the table is crashed.

“Table “tablename” is marked as crashed and should be repaired”

The Mysql table consists of 3 files, .MYI, .MYD and .frm. The .MYI file contains the structure and results in the error message if something goes wrong with it. There are 3 ways to repair the table:

1)  Login to your cPanel, goto “Mysql Databases” option and click “Repair DB” in front of the databases.

2) Login to your server, goto mysql directory /var/lib/mysql and execute the command:              myisamchk -r dbname/tablename.MYI

3) Login to your server, goto Mysql prompt and execute the Mysql query:                                                         repair table tablename;

Anyone of the above 3 steps will repair the table and site will be back online.

Comments Off on Howto: repair a mysql table

Howto: change collation for mysql

October 3, 2009    |   Posted by admin   |    Category: Mysql & PostGres SQL

People may think of changing “collation” for their Mysql databases and here how to change it. First, there are two ways to check the current collation on your server. One from the command line and one from the mysql prompt:

root@server [~]# mysqladmin variables | grep collation
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |

OR

mysql> SHOW VARIABLES LIKE ‘collation%’;
+———————-+—————–+
| Variable_name | Value |
+———————-+—————–+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+———————-+—————–+
3 rows in set (0.00 sec)

As you can see the current collation is set to “utf8_general_ci”.

In order to change it to something for example “latin1_general_ci”, edit your /etc/my.cnf file and place the following code:

collation-server=latin1_general_ci

Save the file and restart mysql service.

Comments Off on Howto: change collation for mysql

Grant privileges to a Mysql user from Mysql Command line?

September 28, 2009    |   Posted by admin   |    Category: Mysql & PostGres SQL

HowTo GRANT privileges to a Mysql user from the command line? This can be done by logging to the server via SSH. Once you are logged in, goto the Mysql prompt with the mysql user and password.

# mysql -uroot -p<mysql-pass-here>

Once at the prompt, create a database:

mysql> create database dbname;

The database with ‘dbname’ has been created. Now we need to create a user who will have access to the database. This can be done by granting privileges using ‘grant’ command

mysql> grant all on dbname.* to 'dbuser'@'localhost' identified \
by 'password';

The user ‘dbuser’ will now have access to database ‘dbname’ using the password ‘password’. This user can access only from the local server and not from external machine.

To allow user to access the database remotely, say from IP 1.2.3.4, execute:

mysql> grant all on dbname.* to 'dbuser'@'1.2.3.4' identified \
by 'password';

Using the above command, you can grant privileges to multiple users to access a database.

Comments Off on Grant privileges to a Mysql user from Mysql Command line?