centos6.5下安装mysql很有用



centos6.5下安装mysql1.使用yum命令安装mysql

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. [root@bogon ~]#  yum -y install mysql-server

2.设置开机启动

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. [root@bogon ~]#  chkconfig mysqld on

3.启动MySQL服务

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. [root@bogon ~]#  service mysqld start

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

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. [root@bogon ~]#  mysql -u root
  2. mysql> select user,host,password from mysql.user;
  3. +——+———–+———-+
  4. | user | host      | password |
  5. +——+———–+———-+
  6. | root | localhost |          |
  7. | root | bogon     |          |
  8. | root | 127.0.0.1 |          |
  9. |      | localhost |          |
  10. |      | bogon     |          |
  11. +——+———–+———-+
  12. 5 rows in set (0.01 sec)

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

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. mysql> set password for root@localhost=password(‘root’);
  2. mysql> exit

5.用新密码登陆

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. [root@bogon ~]#  mysql -u root -p
  2. Enter password:

6.创建mysql新用户test_user

[html] view plaincopy在CODE上查看代码片派生到我的代码片

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

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

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. mysql> grant all privileges on *.* to ’test_user’@'localhost’ identified by ’test_user’;
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> grant all privileges on *.* to ’test_user’@'%’ identified by ’test_user’;
  4. Query OK, 0 rows affected (0.00 sec)
  5. mysql> select user,host,password from mysql.user;
  6. +———-+———–+——————————————-+
  7. | user     | host      | password                                  |
  8. +———-+———–+——————————————-+
  9. | root     | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
  10. | root     | bogon     |                                           |
  11. | root     | 127.0.0.1 |                                           |
  12. |          | localhost |                                           |
  13. |          | bogon     |                                           |
  14. | test_user | %         | *3046CF87132BBD4FDDF06F321C6859074843B7D3 |
  15. | test_user | localhost | *3046CF87132BBD4FDDF06F321C6859074843B7D3 |
  16. +———-+———–+——————————————-+
  17. 7 rows in set (0.00 sec)
  18. mysql> flush privileges;
  19. Query OK, 0 rows affected (0.01 sec)

8.查看mysql5.1的默认存储引擎
从下面的执行结果可以看出,mysql的默认引擎是MyISAM,这个引擎是不支持事务的。

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. mysql> show engines;
  2. +————+———+————————————————————+————–+——+————+
  3. | Engine     | Support | Comment                                                    | Transactions | XA   | Savepoints |
  4. +————+———+————————————————————+————–+——+————+
  5. | MRG_MYISAM | YES     | Collection of identical MyISAM tables                      | NO           | NO   | NO         |
  6. | CSV        | YES     | CSV storage engine                                         | NO           | NO   | NO         |
  7. | MyISAM     | DEFAULT | Default engine as of MySQL 3.23 with great performance     | NO           | NO   | NO         |
  8. | InnoDB     | YES     | Supports transactions, row-level locking, and foreign keys | YES          | YES  | YES        |
  9. | MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables  | NO           | NO   | NO         |
  10. +————+———+————————————————————+————–+——+————+
  11. 5 rows in set (0.00 sec)

也可以以下面的方式查看

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. mysql> show variables like ’storage_engine’;
  2. +—————-+——–+
  3. | Variable_name  | Value  |
  4. +—————-+——–+
  5. | storage_engine | MyISAM |
  6. +—————-+——–+
  7. 1 row in set (0.00 sec)

9.修改mysql的默认引擎为InnoDB
9.1 停止mysql

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. mysql> exit;
  2. [root@bogon ~]# service mysqld stop


9.2 修改/etc/my.cnf
[mysqld] 后加入

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. default-storage-engine=InnoDB

加入后my.cnf的内容为:

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. [root@bogon etc]# more my.cnf
  2. [mysqld]
  3. datadir=/var/lib/mysql
  4. socket=/var/lib/mysql/mysql.sock
  5. user=mysql
  6. # Disabling symbolic-links is recommended to prevent assorted security risks
  7. symbolic-links=0
  8. default-storage-engine=InnoDB
  9. [mysqld_safe]
  10. log-error=/var/log/mysqld.log
  11. pid-file=/var/run/mysqld/mysqld.pid

9.3 启动mysql

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. [root@bogon etc]# service mysqld start
  2. Starting mysqld:  [  OK  ]

9.4 查看mysql默认存储引擎

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. [root@bogon etc]# mysql -u root -p
  2. Enter password:
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.
  4. Your MySQL connection id is 2
  5. Server version: 5.1.73 Source distribution
  6. Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type ’help;’ or ’\h’ for help. Type ’\c’ to clear the current input statement.
  11. mysql> show variables like ’storage_engine’;
  12. +—————-+——–+
  13. | Variable_name  | Value  |
  14. +—————-+——–+
  15. | storage_engine | InnoDB |
  16. +—————-+——–+
  17. 1 row in set (0.00 sec)

10.CentOS6.5开放mysql端口3306
CentOS6.5默认是不开放端口的,如果要让外部的系统访问CentOS6.5上的mysql,必须开放mysql的端口3306
10.1 修改/etc/sysconfig/iptables
添加下面一行

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. -A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT

修改后iptables中的内容是

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. [root@bogon etc]# more /etc/sysconfig/iptables
  2. # Firewall configuration written by system-config-firewall
  3. # Manual customization of this file is not recommended.
  4. *filter
  5. :INPUT ACCEPT [0:0]
  6. :FORWARD ACCEPT [0:0]
  7. :OUTPUT ACCEPT [0:0]
  8. -A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
  9. -A INPUT -p icmp -j ACCEPT
  10. -A INPUT -i lo -j ACCEPT
  11. -A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
  12. #添加配置项
  13. -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT
  14. -A INPUT -m state –state NEW -m tcp -p tcp –dport 11211 -j ACCEPT
  15. -A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT
  16. -A INPUT -j REJECT –reject-with icmp-host-prohibited
  17. -A FORWARD -j REJECT –reject-with icmp-host-prohibited
  18. COMMIT

11.重启防火墙

[html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. [root@bogon etc]# service iptables restart

这样就可以从外部访问mysql了。

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

http://www.centoscn.com/mysql/2014/0812/3481.html