安装MySQL。
# sudo yum -y install mysql-server

配置MySQL
#vim /etc/my.cnf

[quote]
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1  ← 找到这一行,在这一行的下面添加新的规则,让MySQL的默认编码为UTF-8
default-character-set = utf8  ← 添加这一行
[/quote]
然后在配置文件的文尾填加如下语句:
[quote]
[mysql]
default-character-set = utf8
[/quote]

设置MySQL服务随系统启动自启动
# chkconfig mysqld on

确认MySQL自启动
# chkconfig --list mysqld

mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off  ← 如果2--5为on的状态就OK


启动MySQL服务


#/etc/rc.d/init.d/mysqld start



用root用户登录MySQL服务器


# mysql -u root



查看用户信息


mysql> select user,host,password from mysql.user;


+------+------------------------------+---------------+
| user | host          | password |
+------+------------------------------+---------------+
| root | localhost        |       |  ← root密码为空
| root | sample.centospub.com  |       |  ← root密码为空
|   | sample.centospub.com |       |
|   | localhost       |       |
|root | % |XXX     |
|   |     |       |
+------+------------------------------+---------------+



设置root密码


mysql> set password for root@localhost=password('在这里填入root密码');



设置root密码


set password for root@'sample.centospub.com'=password('在这里填入root密码');



设置root密码


mysql> set password for root@'xxx'=password('xxx');



退出MySQL服务器


mysql> exit



通过密码用root登录


#mysql -u root -p


Enter password:  ← 在这里输入密码



通过密码用root登录


# mysql -u root -h sample.centospub.com -p


Enter password:  ← 在这里输入密码



删除匿名用户


mysql> delete from mysql.user where user='';



建立对test数据库有完全操


mysql> grant all privileges on test.* to centospub@localhost identified by '在这里定义密码';



连接到数据库


mysql> use test



在数据库中建立表


mysql> create table test(num int, name varchar(50));



查看表信息


mysql> show tables;



查看已存在的数据库


mysql> show databases;



删除名为test的数据库


mysql> drop database test;



刷新,使以上操作生效


mysql> flush privileges;