阿里云使用(04)_Mysql數(shù)據(jù)庫安裝

一、下載mysql數(shù)據(jù)庫

1、從mysql官網(wǎng)上下載自己適合的mysql版本https://dev.mysql.com/downloads/mysql/5.6.html#downloads,進(jìn)入mysql官網(wǎng),依次點擊

圖1.png

圖2.png

筆者選擇MySQL Community Server 5.6.44 Linux-Generic 64位

二、安裝

1、將下載好的mysql壓縮文件放置在linux的/usr/local文件夾下,解壓該壓縮文件

tar -zxvf mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz

2、將解壓后的文件重命名為mysql

mv mysql-5.6.44-linux-glibc2.12-x86_64  mysql

3、創(chuàng)建mysql用戶組及用戶

groupadd mysql
useradd -r -g mysql mysql

4、入到mysql目錄,執(zhí)行添加MySQL配置的操作

cp support-files/my-medium.cnf /etc/my.cnf
或:
cp support-files/my-default.cnf /etc/my.cnf

是否覆蓋?按y 回車
5、編輯/etc/my.cnf文件;

vi /etc/my.cnf

在my.cnf文件中添加或者修改相關(guān)配置,更改完成后保存退出

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
# server_id = .....
socket = /tmp/mysql.sock
character-set-server = utf8
skip-name-resolve
log-err = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/data/mysql.pid

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

6、在mysql當(dāng)前目錄下設(shè)定目錄的訪問權(quán)限(注意后面的小點,表示當(dāng)前目錄)

chown -R mysql .
chgrp -R mysql .
scripts/mysql_install_db --user=mysql
chown -R root .
chown -R mysql data

7、初始化數(shù)據(jù)(在mysql/bin或者mysql/scripts下有個 mysql_install_db 可執(zhí)行文件初始化數(shù)據(jù)庫),進(jìn)入mysql/bin或者mysql/scripts目錄下,執(zhí)行下面命令

./mysql_install_db --verbose --user=root --defaults-file=/etc/my.cnf --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql --pid-file=/usr/local/mysql/data/mysql.pid --tmpdir=/tmp

8、啟動mysql,進(jìn)入/usr/local/mysql/bin目錄,執(zhí)行下面命令

[root@iz2ze9z5o9dc mysql]# ./bin/mysql -h127.0.0.1 -uroot -proot
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 

三、踩過的坑

1、問題:本地可以訪問,遠(yuǎn)程訪問請求超時
2、嘗試的方法
①. 排除網(wǎng)絡(luò)或防火墻問題
I、先看是否能ping通遠(yuǎn)程服務(wù)器,ping 192.168.1.211,如果不可以就是網(wǎng)絡(luò)問題。然后,檢查端口是否被防火墻擋住了,telnet 192.168.1.211 3306,如果連接失敗,配置防火墻。
II、配置防火墻,開啟3306端口
vi /etc/sysconfig/iptables
-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT(允許3306端口通過防火墻)
III、service iptables restart(重啟防火墻使配置生效)
或者關(guān)閉防火墻
②檢查MySQL配置
如果開啟了防火墻,telnet還是失敗,通過netstat查看3306的端口狀態(tài):

[root@iz2ze9z5 mysql]# netstat -apn|grep 3306 
tcp        0      0 127.0.0.1:37806         127.0.0.1:3306          TIME_WAIT   -                          
tcp6       0      0 :::3306                 :::*                    LISTEN      1932/mysqld                
tcp6       0      0 172.17.134.67:3306      223.72.43.25:21593      ESTABLISHED 1932/mysqld                
tcp6       0      0 172.17.134.67:3306      223.72.43.25:21762      ESTABLISHED 1932/mysqld    

檢查一下my.cnf的配置,這里可以配置綁定ip地址。
bind-address=addr
不配置或者IP配置為0.0.0.0,表示監(jiān)聽所有客戶端連接。
③. 檢查用戶訪問權(quán)限
MySQL建用戶的時候會指定一個host,默認(rèn)是127.0.0.1/localhost,那么這個用戶就只能本機(jī)訪問, 其它機(jī)器用這個用戶帳號訪問會提示沒有權(quán)限,host改為%,表示允許所有機(jī)器訪問。

mysql> use mysql; 
Database changed 
mysql> update user set host=’%’ where user=’root’; 
Query OK, 0 rows affected (0.00 sec) 
Rows matched: 1 Changed: 0 Warnings: 0
mysql> select host,user from user; 
+-------------------------+------+
| host                    | user |
+-------------------------+------+
| %                       |      |
| %                       | root |
| 127.0.0.1               | root |
| ::1                     | root |
| iz2ze9z5o9dc8zmb6  |      |
| iz2ze9z5o9dc8zmb6  | root |
| localhost               | root |
+-------------------------+------+
7 rows in set (0.00 sec)

重啟mysql使配置生效。

 mysql]# ./support-files/mysql.server restart                                 

嘗試以上三步遠(yuǎn)程連接仍然連接不上,發(fā)現(xiàn)阿里云服務(wù)器上的安全組端口需要放行

圖3.png
圖4.png

3、問題解決,遠(yuǎn)程連接成功

只要智商不滑坡,辦法總比困難多

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容