騰訊雲CentOS6.5下安裝mysql,並配置好遠程訪問等權限,途中遇到的問題

1.使用yum命令安裝mysqlmysql

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

2.設置開機啓動linux

[root@bogon ~]#  chkconfig mysqld on 

3.啓動MySQL服務sql

[root@bogon ~]#  service mysqld start  

4.設置MySQL的root用戶設置密碼數據庫

[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的密碼爲rootcentos

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

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.查看mysql5.1的默認存儲引擎
從下面的執行結果能夠看出,mysql的默認引擎是MyISAM,這個引擎是不支持事務的。socket

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)  

也能夠如下面的方式查看tcp

mysql> show variables like 'storage_engine';  
+----------------+--------+  
| Variable_name  | Value  |  
+----------------+--------+  
| storage_engine | MyISAM |  
+----------------+--------+  
1 row in set (0.00 sec)  

9.修改mysql的默認引擎爲InnoDB
9.1 中止mysqlide

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上的安裝過程、用戶建立、外部訪問的步驟所有完成。

 

—————————分割線—————————————

上面都是從別人發表的文章中截取的~~~做者要是看到了莫怪,我只是想收藏,怕源文被刪就看不到了,謝謝大家這些摘樹的前人。

其實這樣在雲主機上設置mysql並訪問是OK的。適用於一切的centos6.5,亦或者其餘linux系統。萬變不離其宗,系統的改變,無非是命令行語言的改變,其宗旨是不會變的。

 

LZ第一次用雲主機,想試試在本地遠程訪問一下,結果發現,咦,鏈接出錯了~~~,後來試了不少方法,仍是沒有能成功。

剛開始,用Navicat鏈接遠程數據庫,出現以下錯誤:應該就是遠程不容許訪問了。

遠程拒絕訪問的狀況可能有兩種:

1、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.
 
 
2、肯定服務器上的防火牆沒有阻止 3306 端口。也即服務器上面有沒有開放mysql的端口~(個人就是這個問題)
  看到上面的第10條中,在端口管理iptables中添加3306端口的開放配置,這樣算是硬件的配置。而後看看你的雲主機設置裏面有沒有開放對應的端口,或者說是否是開放了全部端口~個人雲主機以下圖:
騰訊雲主機中,貌似是沒有開放全部端口的,因此須要咱們本身在這裏稍微設置一下,勾選「默認安全組放通所有端口」選項,「肯定」。基本上,咱們能作的都作了,要否則就是配置文件出錯了,再返回去看看。設置好了之後,我去試了一下鏈接,結果成功了~
哈哈,好高興~之後遇到這些問題就知道在哪些地方找問題了~~~
相關文章
相關標籤/搜索