Tuesday, July 10, 2012

How to install MySql in Linux 




1. update and Upgrate linux packages


$ sudo apt-get update
$ sudo apt-get upgrade


for centos
---------
$ yum update
$ yum upgrade


2. Install MySQL Server


$ sudo apt-get install mysql-server mysql-common mysql-client

for centos
----------
$ yum install mysql-server mysql-common mysql-client


3. How Do I Access MySQL Server?


$ mysql -u root -p


4. Give root password


Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 107
Server version: 5.1.41-3ubuntu12.3 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>


5. List Your Databases


mysql> show databases;


6. Add A New Database


mysql> create database myapps;


7. Add A New User For myapps Database


mysql> GRANT ALL ON myapps.* TO venkat@localhost IDENTIFIED BY 'Add-Your-Password-Here';


8. Add A New User For myapps Database (Network Access)


mysql> GRANT ALL ON myapps.* TO venkat@127.0.0.1 IDENTIFIED BY 'Your-Password-Here';


9. How Do I View Log Files?


$ tail -f /var/log/mysql/error.log
$ grep 'something' /var/log/mysql/error.log


10.Configure MySQL Server 


$ sudo vi /etc/mysql/my.cnf


Change network binding to 192.168.1.5 so that web server located at 192.168.1.10 can access database:
bind-address = 192.168.1.5


11. How Do I Start / Stop / Restart Server?


$ sudo service mysql restart
$ sudo service mysql stop
$ sudo service mysql start


mysql start/running, process 4930


12. You can also use the following command for older version:


$ sudo /etc/init.d/mysql start
$ sudo /etc/init.d/mysql stop
$ sudo /etc/init.d/mysql restart


GRANT ALL ON klargenews.* TO root@'117.201.18.188' IDENTIFIED BY 'DoNotTell$AnyBody';


To list installed mysql packages
--------------------------------
rpm -qa | grep -i mysql


How to Uninstall Mysql
----------------------
 rpm -e --nodeps mysql mysql-server




Start / Stop Apache Server
--------------------------
apachect1 start
apachect1 stop




1. Verify MySQL Installation


Execute rpm -qa, to confirm that the mysql related packages are installed.


# rpm -qa | grep -i mysql
MySQL-python-1.2.1-1
mysql-5.0.77-4.el5_4.2
mysql-connector-odbc-3.51.26r1127-1.el5
mysql-server-5.0.77-4.el5_4.2
libdbi-dbd-mysql-0.8.1a-1.2.2
perl-DBD-MySQL-3.0007-2.el5
Check the /etc/passwd and /etc/group to make sure it has created a mysql username and group.


# grep mysql /etc/passwd
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash


# grep mysql /etc/group
mysql:x:27:




2. MySQL Post installation – Execute mysql_install_db




mysql_install_db program will setup the necessary grant tables. The mysql_install_db program gets executed as part of the rpm installation. But, it doesn’t hurt to execute the mysql_install_db program again to make sure the grant tables are setup properly.


# /usr/bin/mysql_install_db --user=mysql
Installing MySQL system tables...OK
Filling help tables...OK
.....
The latest information about MySQL is available on the web at http://www.mysql.com


3. Start MySQL Server


# service mysqld status
mysqld is stopped


# service mysqld start
Starting MySQL:                                            [  OK  ]




4. Verify that the MySQL server is up and running.


# /usr/bin/mysqladmin version
/usr/bin/mysqladmin  Ver 8.41 Distrib 5.0.77, for redhat-linux-gnu on i686
Copyright (C) 2000-2006 MySQL AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license


Server version 5.0.77
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/lib/mysql/mysql.sock
Uptime: 39 sec


Threads: 1  Questions: 2  Slow queries: 0  Opens: 12  Flush tables: 1
Open tables: 6  Queries per second avg: 0.051
# /usr/bin/mysqlshow
+--------------------+
|     Databases      |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+


# /usr/bin/mysqlshow mysql
Database: mysql
+---------------------------+
|          Tables           |
+---------------------------+
| columns_priv              |
| db                        |
| func                      |
| help_category             |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
Stop and start the mysql server again to make sure they are no issues.


# service mysqld stop
Stopping MySQL:                                            [  OK  ]


# service mysqld start
Starting MySQL:                                            [  OK  ]






5. Change the MySQL root account password


Change the MySQL root account password to something secure.


# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.0.77 Source distribution


Type 'help;' or '\h' for help. Type '\c' to clear the buffer.


mysql> select host, user from mysql.user;
+-----------+------+
| host      | user |
+-----------+------+
| 127.0.0.1 | root |
| localhost |      |
| localhost | root |
+-----------+------+
5 rows in set (0.00 sec)


mysql> set password for 'root'@'localhost' = PASSWORD('DoNotTell$AnyBody');
Query OK, 0 rows affected (0.00 sec)


mysql> set password for 'root'@'127.0.0.1' = PASSWORD('DoNotTell$AnyBody');
Query OK, 0 rows affected (0.00 sec)






Make sure you are able to login to MySQL using the new password as shown below.


# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)


# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.0.77 Source distribution


Type 'help;' or '\h' for help. Type '\c' to clear the buffer.


mysql>




set password for 'root'@'localhost' = PASSWORD('password');
set password for 'root'@'kla.klarge.com' = PASSWORD('password');






-------------------------------------------------------------




I have come to a solution for Chinese UTF-8, may it works for you :)
The context is
* ubuntu linux 6.10
* locale zh_cn.UTF-8
* mysql-server-5.0
* mysql connector/J 5.0.4


The essential part is adding "--character-set-server utf8 --collation-server=utf8_general_ci" to the startup command of mysqld_safe ( in my case it is in /etc/init.d/mysql ) and then it all works, no need to add characterEncoding to connection string, no need to "SET NAMES utf8", no need to do the "getBytes(xxx)" trick.


But you still have to add CHARACTER SET and COLLATION clause to CREATE DATABASE and  CREATE TABLE


You can have a try and tell us if it works for your language.





No comments:

Post a Comment