1.使用yum命令安装mysql
[root@jerry ~]# yum -y install mysql-server
2.设置开机启动
[root@jerry ~]# chkconfig mysqld on
3.启动MySQL服务
[root@jerry ~]# service mysqld start
4.设置MySQL的root用户设置密码
[root@jerry ~]# mysql -u root
mysql> select user,host,password from mysql.user;
查询用户的密码,都为空,用下面的命令设置root的密码为root
mysql> set password for root@localhost=password(‘root’);
mysql> exit
5.创建mysql新用户test_user
[root@bogon ~]# mysql -u root -p
mysql> create user ‘test_user’@’%’ identified by ‘test_user’;
Query OK, 0 rows affected (0.00 sec)
6.给新用户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;
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
7.查看mysql5.1的默认存储引擎
从下面的执行结果可以看出,mysql的默认引擎是MyISAM,这个引擎是不支持事务的。
mysql> show engines;
也可以以下面的方式查看
mysql> show variables like ‘storage_engine’;
8.修改mysql的默认引擎为InnoDB
8.1 停止mysql
mysql> exit;
[root@jerry ~]# service mysqld stop
8.2 修改/etc/my.cnf
[mysqld] 后加入
default-storage-engine=InnoDB
加入后my.cnf的内容为:
[root@bogon etc]# more my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0 default-storage-engine=InnoDB
#设置数据库表名大小写不敏感(必须在mysql初始化之前设置)
lower_case_table_names=1
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
8.3 启动mysql
[root@jerry etc]# service mysqld start
Starting mysqld: [ OK ]
8.4 查看mysql默认存储引擎
[root@jerry etc]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution
Copyright © 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
mysql> show variables like ‘storage_engine’;
9.CentOS6.5开放mysql端口3306
CentOS6.5默认是不开放端口的,如果要让外部的系统访问CentOS6.5上的mysql,必须开放mysql的端口3306
9.1 修改/etc/sysconfig/iptables
添加下面一行
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
修改后iptables中的内容是
[root@jerry etc]# more /etc/sysconfig/iptables
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
#添加配置项
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
10.重启防火墙
[root@bogon etc]# service iptables restart
这样就可以从外部访问mysql了。
至此,mysql在CentOS6.5上的安装过程、用户创建、外部访问的步骤全部完成。
—————————分割线————————————————
远程拒绝访问的情况可能有两种:
一、mysql设置中,不允许远程访问.也即你使用的mysql登陆账号没有远程访问的权限。比如root账号,在我们之前看到的use权限列表中,root就没有远程访问的权限。现在来设置root的远程访问权限。
方法一:实现远程连接(授权法)
mysql> use mysql;
Database changed
mysql> grant all privileges on . to root@’%’ identified by “root”;
mysql> select host,user,password from user;
±-------------±-----±------------------------------------------+
| host | user | password |
±-------------±-----±------------------------------------------+
| localhost | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
| 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
| % | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
±-------------±-----±------------------------------------------+
3 rows in set (0.00 sec)
这样机器就可以以用户名root密码root远程访问该机器上的MySql.
方法二:实现远程连接(改表法)
将host字段的值改为%就表示在任何客户端机器上能以root用户登录到mysql服务器,建议在开发时设为%。
mysql> use mysql;
Database changed
mysql> update user set host = ‘%’ where user = ‘root’;
这时,root将权限改为ALL PRIVILEGES;
这样在远端就可以通过root用户访问Mysql.
二、确定服务器上的防火墙没有阻止 3306 端口。也即服务器上面有没有开放mysql的端口
参考资料:mysql5安装: https://www.jianshu.com/p/276d59cbc529