-
安装mysql
yum -y install mysqlyum -y install mysql-server
yum -y install php-mysqlyum -y install mysql-devel
2.配置MySQL
[root@sample ~]#vim /etc/my.cnf ← 编辑MySQL的配置文件
[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 ← 添加这一行
然后在配置文件的文尾填加如下语句:
[mysql]
default-character-set = utf8
3.启动MySQL服务
[root@sample ~]# chkconfig mysqld on ← 设置MySQL服务随系统启动自启动
[root@sample ~]# chkconfig --list mysqld← 确认MySQL自启动
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off← 如果2--5为on的状态就OK
[root@sample ~]#/etc/rc.d/init.d/mysqld start ← 启动MySQL服务
4.修改密码
[root@sampl ~]# mysql -uroot root默认无密码
mysql> select user,host,password from mysql.user;
+------+-----------------------+----------+
| user | host | password |
+------+-----------------------+----------+
| root | localhost | |
| root | ay140611163431555064z | |
| root | 127.0.0.1 | |
| | localhost | |
| | ay140611163431555064z | |
+------+-----------------------+----------+
下面是把密码都设置成123456
mysql> set password for root@localhost=password('123456');
Query OK, 0 rows affected (0.00 sec)
mysql> set password for root@127.0.0.1=password('123456');
Query OK, 0 rows affected (0.00 sec)
mysql> set password for root@ay140611163431555064z=password('123456');
Query OK, 0 rows affected (0.00 sec)
删除匿名账号
mysql> delete from mysql.user where user='';
mysql> select user,host,password from mysql.user;
+------+-----------------------+-------------------------------------------+
| user | host | password |
+------+-----------------------+-------------------------------------------+
| root | localhost | *047871A860B9795D35702CA5974FA67374791CAD |
| root | ay140611163431555064z | *047871A860B9795D35702CA5974FA67374791CAD |
| root | 127.0.0.1 | *047871A860B9795D35702CA5974FA67374791CAD |
+------+-----------------------+-------------------------------------------+
3 rows in set (0.00 sec)
mysql> quit
设置完成
补充:设置或修改用户密码的另一种方法
把root的密码设为123456,如果root已经有密码,命令运行时会让输入原密码
[root@sample ~]# mysqladmin -uroot -p password 123456
注意!!!
安装完后,查看max_allowed_packet配置值
show VARIABLES like '%max_allowed_packet%';
如果写1024会有问题,需要改大些
在my.cnf的[mysqld]下添加
max_allowed_packet = 20M
如果遇到远程连接数据库,得到报错信息" Lost connection to MySQL server at 'reading initial communication packet', system error: 104",那么在my.cnf的[myslqd]里增加一行:
skip-name-resolve
本文转自 zl1030 51CTO博客,原文链接:http://blog.51cto.com/zl1030/1425089