1.使用yum命令安裝mysqlhtml
- [root@bogon ~]# yum -y install mysql-server
2.設置開機啓動mysql
- [root@bogon ~]# chkconfig mysqld on
3.啓動MySQL服務sql
- [root@bogon ~]# service mysqld start
4.設置MySQL的root用戶設置密碼socket
- [root@bogon ~]# 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的密碼爲roottcp
- mysql> set password for root@localhost=password('root');
- mysql> exit
5.用新密碼登錄ide
- [root@bogon ~]# mysql -u root -p
- Enter password:
6.建立mysql新用戶test_userthis
- mysql> create user 'test_user'@'%' identified by 'test_user';
- Query OK, 0 rows affected (0.00 sec)
7.給新用戶test_user受權,讓他能夠從外部登錄和本地登錄
注意:@左邊是用戶名,右邊是域名、IP和%,表示能夠訪問mysql的域名和IP,%表示外部任何地址都能訪問。spa
- 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.查看mysql5.1的默認存儲引擎
從下面的執行結果能夠看出,mysql的默認引擎是MyISAM,這個引擎是不支持事務的。rest
- mysql> show engines;
- +------------+---------+------------------------------------------------------------+--------------+------+------------+
- | Engine | Support | Comment | Transactions | XA | Savepoints |
- +------------+---------+------------------------------------------------------------+--------------+------+------------+
- | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
- | CSV | YES | CSV storage engine | NO | NO | NO |
- | MyISAM | DEFAULT | Default engine as of MySQL 3.23 with great performance | NO | NO | NO |
- | InnoDB | YES | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
- | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
- +------------+---------+------------------------------------------------------------+--------------+------+------------+
- 5 rows in set (0.00 sec)
也能夠如下面的方式查看orm
- mysql> show variables like 'storage_engine';
- +----------------+--------+
- | Variable_name | Value |
- +----------------+--------+
- | storage_engine | MyISAM |
- +----------------+--------+
- 1 row in set (0.00 sec)
9.修改mysql的默認引擎爲InnoDB
9.1 中止mysql
- mysql> exit;
- [root@bogon ~]# service mysqld stop
9.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
- # Disabling symbolic-links is recommended to prevent assorted security risks
- symbolic-links=0
-
- default-storage-engine=InnoDB
-
- [mysqld_safe]
- log-error=/var/log/mysqld.log
- pid-file=/var/run/mysqld/mysqld.pid
9.3 啓動mysql
- [root@bogon etc]# service mysqld start
- Starting mysqld: [ OK ]
9.4 查看mysql默認存儲引擎
- [root@bogon 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 (c) 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';
- +----------------+--------+
- | Variable_name | Value |
- +----------------+--------+
- | storage_engine | InnoDB |
- +----------------+--------+
- 1 row in set (0.00 sec)
10.CentOS6.5開放mysql端口3306
CentOS6.5默認是不開放端口的,若是要讓外部的系統訪問CentOS6.5上的mysql,必須開放mysql的端口3306
10.1 修改/etc/sysconfig/iptables
添加下面一行
- -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
修改後iptables中的內容是
- [root@bogon etc]# more /etc/sysconfig/iptables
- # Firewall configuration written by system-config-firewall
- # Manual customization of this file is not recommended.
- *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
11.重啓防火牆
- [root@bogon etc]# service iptables restart
這樣就能夠從外部訪問mysql了。
至此,mysql在CentOS6.5上的安裝過程、用戶建立、外部訪問的步驟所有完成。