Ubuntu操作系統下之-MYSQL的常用操作

操作系統

本次記錄過程是基於如下操作系統進行操作演示,執行指令lsb_release -a

root@instance-8scsy8qh:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:        18.04
Codename:       bionic

遠程工具

SecureCRT 7.3
Navicat for MySQL

Mysql服務指令

以下均基於root用戶下操作,普通用戶在指令前加 sudo

Mysql環境檢測

dpkg -l | grep mysql 檢測是否安裝了Mysql,如果出現以下信息,即已經安裝

root@instance-8scsy8qh:~# dpkg -l | grep mysql
ii  mysql-client-5.7                      5.7.28-0ubuntu0.18.04.4           amd64        MySQL database client binaries
ii  mysql-client-core-5.7                 5.7.28-0ubuntu0.18.04.4           amd64        MySQL database core client binaries
ii  mysql-common                          5.8+1.0.4                         all          MySQL database common files, e.g. /etc/mysql/my.cnf
ii  mysql-server                          5.7.28-0ubuntu0.18.04.4           all          MySQL database server (metapackage depending on the latest version)
ii  mysql-server-5.7                      5.7.28-0ubuntu0.18.04.4           amd64        MySQL database server binaries and system database setup
ii  mysql-server-core-5.7                 5.7.28-0ubuntu0.18.04.4           amd64        MySQL database server binaries
root@instance-8scsy8qh:~#

當然有安裝了Mysql纔會有幫助指令,所以也可以使用 mysql --help 且同時還能看到Mysql的版本信息

root@instance-8scsy8qh:~# mysql --help
mysql  Ver 14.14 Distrib 5.7.28, for Linux (x86_64) using  EditLine wrapper
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
以下內容省略......

Mysql的安裝

apt-get install mysql-server 安裝Mysql服務

root@instance-fqhtxc7e:~# apt-get install mysql-server
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libaio1 libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libevent-core-2.1-6 libfcgi-perl libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl
  libhttp-date-perl libhttp-message-perl libio-html-perl liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client-5.7 mysql-client-core-5.7 mysql-common
  mysql-server-5.7 mysql-server-core-5.7
Suggested packages:
  ......

service mysql status|stop|start|restart 服務相關操作cmd
啓動停止查看數據庫運行狀態

root@instance-8scsy8qh:~# service mysql start
root@instance-8scsy8qh:~# service mysql status
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2020-03-01 10:47:23 CST; 3s ago
  Process: 13190 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid (code=exited, status=0/SUCCESS)
  Process: 13181 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
 Main PID: 13192 (mysqld)
    Tasks: 27 (limit: 2327)
   CGroup: /system.slice/mysql.service
           └─13192 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

Mar 01 10:47:22 instance-8scsy8qh systemd[1]: Starting MySQL Community Server...
Mar 01 10:47:23 instance-8scsy8qh systemd[1]: Started MySQL Community Server.
root@instance-8scsy8qh:~# service mysql stop
root@instance-8scsy8qh:~# service mysql status
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Sun 2020-03-01 13:34:20 CST; 3s ago
  Process: 13190 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid (code=exited, status=0/SUCCESS)
  Process: 13181 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
 Main PID: 13192 (code=exited, status=0/SUCCESS)

Mar 01 10:47:22 instance-8scsy8qh systemd[1]: Starting MySQL Community Server...
Mar 01 10:47:23 instance-8scsy8qh systemd[1]: Started MySQL Community Server.
Mar 01 13:34:16 instance-8scsy8qh systemd[1]: Stopping MySQL Community Server...
Mar 01 13:34:20 instance-8scsy8qh systemd[1]: Stopped MySQL Community Server.

Mysql的遠程訪問

作爲服務器上的Mysql服務,其開發者主要還是關心如何操作其數據庫,所以只需要關心第三方的遠程訪問數據庫工具能否訪問即可,而不巧的是關於Ubuntu系統下的Mysql服務,在安裝完成時默認只允許本機訪問,並不允許其他第三方工具遠程訪問,比如上邊所提供的Navicat for MySQL工具,當還沒對Mysql的初始配置進行任何修改,就用該工具去訪問時,即會出現如下現象
Navicat for MySQL遠程訪問數據庫時被拒現象
步驟一 修改配置文件
正常情況下,安裝上的Mysql默認端口號爲3306 通常情況下,我們也沒有必要去修改它,那麼遠程連接會被拒絕呢?優先考慮兩個方向 防火牆 以及端口占用
service iptables stop 關閉防火牆
netstat -an|grep 3306 檢測端口情況

root@instance-8scsy8qh:~# netstat -an|grep 3306
tcp        0      0 127.0.0.1:3306            0.0.0.0:*               LISTEN
......

以上信息是表示3306端口監聽在127.0.0.1,即只有本機客戶端可以訪問,其他服務器無法訪問,那麼怎麼讓其他服務器可以正常連接到該服務區的端口呢?需要修改配Mysql的置文件mysql.cnf,其路徑爲/etc/mysql/mysql.conf.d/
方法一、把3306端口監聽在0.0.0.0上,表示沒有訪問地址的限制,那麼其他服務器則可以連接該服務器的該端口
bind-address = 127.0.0.1改成bind-address = 0.0.0.0即可

root@instance-8scsy8qh:~# netstat -an|grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN
......

方法二、既然這邊綁定的地址是本機,那麼是否可以追加訪問者的地址呢?當然可以,只要將bind-address後面增加遠程訪問IP地址或者禁掉這句話就可以讓遠程機登陸訪問了(允許多個IP可訪問mysql服務器,空格隔開

bind-address=127.0.0.1 183.251.17.221

反觀方法二,對於開發者來說,所用網絡的IP基本都是動態的,所用用這方法的人比較少,不過對於類似局域網的雲服務器來說,是不是就可以設定固定服務器來訪問,增加數據庫的安全性?
步驟二 更新用戶權限
進入mysql之後操作如下指令
1、grant 權限1,權限2,...權限n on 數據庫名稱.表名稱 to 用戶名@用戶地址 identified by '連接密碼'; 進行權限設置

  • 權限 可以多個,共14種,分別爲:select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等,其中賦予全部權限時使用all privileges
  • 數據名稱.表名稱 如果用*.*表示,說明賦予用戶服務器上所有數據庫所有表的權限。
  • 用戶名 即用於登錄數據庫的名稱,如root
  • 用戶地址 可以是localhost,可以是ip地址、機器名字、域名等等,也可以用%表示任何連接地址。
  • 連接密碼 不能爲空,否則會賦予權限失敗。

2、flush privileges; 進行設置刷新
3、退出mysql指令並重啓Mysql服務 service mysql restart
如下操作

root@instance-8scsy8qh:~# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4154
Server version: 5.7.28-0ubuntu0.18.04.4 (Ubuntu)

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> grant all on *.* to root@'%' identified by 'root123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye
root@instance-8scsy8qh:~# service mysql restart
root@instance-8scsy8qh:~#

以上意思爲 允許任何用戶地址以root的用戶名和root123的連接密碼進行訪問,並賦予了所有數據庫的操作權限

修改默認用戶密碼

1、進入mysql目錄 /etc/mysql/ 進行查看debian.cnf文件

root@instance-8scsy8qh:~# cd /etc/mysql/
root@instance-8scsy8qh:/etc/mysql# cat debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = klGn4HxPadFXxzQU
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = klGn4HxPadFXxzQU
socket   = /var/run/mysqld/mysqld.sock

2、使用user爲debian-sys-maint的用戶以及password爲klGn4HxPadFXxzQU進行數據庫登錄,當然,只要能登錄mysql,自然就能操作數據庫的很多事情了,比如密碼的修改,數據庫的查看等操作

root@instance-8scsy8qh:/etc/mysql# mysql -udebian-sys-maint -pklGn4HxPadFXxzQU
mysql: [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 16
Server version: 5.7.28-0ubuntu0.18.04.4 (Ubuntu)

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| PLEASE_READ_ME_VVV |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

mysql>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章