centos6.5下安装mysql

1.使用yum命令安装mysql

yum -y install mysql-server

2.设置开机启动

chkconfig mysqld on

3.启动MySQL服务

service mysqld start  

4.设置MySQL的root用户设置密码

mysql -u root

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

查询用户的密码,都为空,用下面的命令设置root的密码为root

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

5.用新密码登陆

mysql -u root -p 

6.创建mysql新用户test_user

mysql> create user 'test_user'@'%' identified by 'test_user';  
Query OK, 0 rows affected (0.00 sec)

7.给新用户test_user授权,让他可以从外部登陆和本地登陆
注意:@左边是用户名,右边是域名、IP和%,表示可以访问mysql的域名和IP,%表示外部任何地址都能访问。

mysql> grant all privileges on *.* to 'test_user'@'localhost' identified by 'test_user';  
Query OK, 0 rows affected (0.00 sec)  
mysql> grant all privileges on *.* to 'test_user'@'%' identified by 'test_user';  
Query OK, 0 rows affected (0.00 sec)  
mysql> select user,host,password from mysql.user;  
+----------+-----------+-------------------------------------------+  
| user     | host      | password                                  |  
+----------+-----------+-------------------------------------------+  
| root     | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |  
| root     | bogon     |                                           |  
| root     | 127.0.0.1 |                                           |  
|          | localhost |                                           |  
|          | bogon     |                                           |  
| test_user | %         | *3046CF87132BBD4FDDF06F321C6859074843B7D3 |  
| test_user | localhost | *3046CF87132BBD4FDDF06F321C6859074843B7D3 |  
+----------+-----------+-------------------------------------------+  
7 rows in set (0.00 sec)  
mysql> flush privileges;  
Query OK, 0 rows affected (0.01 sec)  

8.启动mysql

 service mysqld start  

9.CentOS6.5开放mysql端口3306
CentOS6.5默认是不开放端口的,如果要让外部的系统访问CentOS6.5上的mysql,必须开放mysql的端口3306

iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT  
service iptables save

10.重启防火墙

service iptables restart  

这样就可以从外部访问mysql了。
至此,mysql在CentOS6.5上的安装过程、用户创建、外部访问的步骤全部完成。


扫描二维码,在手机上阅读!

标签: centos, mysql

添加新评论