MySQL數據庫安裝配置詳細教程


本文介紹MySQL 在 Windows系統和Linux系統安裝方法。

Windows系統安裝MySQL

1、下載

下載地址:https://dev.mysql.com/downloads/mysql/
選擇要下載的MySQL版本

2、解壓zip包

解壓下載的zip壓縮包

3、配置環境變量

在Path下添加D:\tools\mysql-8.0.16-winx64\bin

4、編寫配置文件

在安裝根目錄下添加my-default.ini文件:D:\tools\mysql-8.0.16-winx64\my-default.ini

[mysqld]
# 3306端口
port=3306
# mysql的安裝目錄
basedir=D:\tools\mysql-8.0.16-winx64
# 設置mysql數據庫的數據的存放目錄
datadir=D:\tools\mysql-8.0.16-winx64\data
# 允許最大連接數
max_connections=200
# 允許連接失敗的次數,防止有人從該主機試圖攻擊數據庫系統
max_connect_errors=10
# 服務端使用的字符集默認為UTF8
character-set-server=utf8
# 創建新表時將使用的默認存儲引擎
default-storage-engine=INNODB
# 默認使用“mysql_native_password”插件認證
default_authentication_plugin=mysql_native_password
[mysql]
# 設置mysql客戶端默認字符集
default-character-set=utf8
[client]
# 設置mysql客戶端連接服務端時默認使用的端口
port=3306
default-character-set=utf8

5、MySQL服務安裝

mysqld --install MySQL
mysqld install

報錯1:
信息如下:
Install/Remove of the Service Denied
解決辦法:
打開cmd.exe程序的時候選擇“用管理員身份打開”。

報錯2:

C:\WINDOWS\system32>mysqld --install MySQL
The service already exists!
The current server installed: D:\xampp\mysql\bin\mysqld MySQL

解決辦法:

mysqld -remove MySQL

6、MySQL初始化

在MySQL安裝目錄的 bin 目錄下執行命令:

mysqld --initialize --console

執行完成后,會打印 root 用戶的初始默認密碼:

C:\WINDOWS\system32>mysqld --initialize --console
2019-05-15T09:27:48.338369Z 0 [System] [MY-013169] [Server] D:\tools\mysql-8.0.16-winx64\bin\mysqld.exe (mysqld 8.0.16) initializing of server in progress as process 15308
2019-05-15T09:28:50.242794Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: u#vTkx6L:dg,
2019-05-15T09:29:42.201421Z 0 [System] [MY-013170] [Server] D:\tools\mysql-8.0.16-winx64\bin\mysqld.exe (mysqld 8.0.16) initializing of server has completed

啟動MySQL服務

net start mysql

7、更改密碼和密碼認證插件

進入D:\tools\mysql-8.0.16-winx64\bin路徑下,執行

mysql -u root -p

然后輸入第6步的密碼:u#vTkx6L:dg

然后修改用戶密碼,在MySQL中執行命令:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密碼';

8、查看默認數據庫mysql

初始化完成后,初始化了名為mysql數據庫,其中user表里面存儲MySQL用戶信息。

mysql> show databases;
mysql> use mysql;
mysql> show tables;

# 顯示user表結構
mysql> desc user;
mysql> select user,host,authentication_string from mysql.user;

添加新的用戶

除了管理員root用戶,也可以添加其他用戶。

允許本地 IP訪問localhost的Mysql數據庫

mysql> create user 'admin'@'localhost' identified by 'admin';
Query OK, 0 rows affected (0.22 sec)

允許外網IP訪問數據庫

允許開放其他ip登錄

mysql> create user 'admin'@'%' identified by 'admin'; 
Query OK, 0 rows affected (0.08 sec)
mysql> GRANT ALL PRIVILEGES ON e_menu.* TO 'admin'@'%' WITH GRANT OPTION; //將數據庫e_menu授權給admin用戶 
Query OK, 0 rows affected (0.15 sec)
mysql> flush privileges; //刷新授權
Query OK, 0 rows affected (0.05 sec)

注意:上面的授權命令適用於MySQL8.0及之后的版本,MySQL8.0之前的版本使用如下命令授權

grant all privileges on 庫名.表名 to '用戶名'@'IP地址' identified by '密碼' with grant option;

授權基本的查詢修改權限:

GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON 庫名.表名 TO '用戶名'@'IP地址';

查看用戶權限

show grants for '用戶名'@'IP地址';

查看MYSQL數據庫中所有用戶

mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
+---------------------------------------+
| query                                 |
+---------------------------------------+
| User: 'admin'@'%';                    |
| User: 'admin'@'localhost';            |
| User: 'mysql.infoschema'@'localhost'; |
| User: 'mysql.session'@'localhost';    |
| User: 'mysql.sys'@'localhost';        |
| User: 'root'@'localhost';             |
+---------------------------------------+
6 rows in set (0.04 sec)

mysql>

Linux系統安裝MySQL:Docker安裝

Linux系統推薦使用docker安裝,簡單方便。
如果沒有安裝docker,需要先安裝一下,可參考 容器技術介紹:Docker簡介及安裝

1、查看可用的 MySQL 版本

先查看一下可以安裝的 MySQL 版本,MySQL鏡像的可用版本可在docker hub中搜索查看:https://hub.docker.com/_/mysql?tab=tags

2、拉取 MySQL 鏡像

拉取指定版本的MySQL

$ docker pull mysql:5.7

查看是否安裝MySQL鏡像

$ docker images
[root@server ~]# docker images
REPOSITORY                   TAG               IMAGE ID       CREATED         SIZE
jenkins/jenkins              latest            10fafa8484ea   4 weeks ago     721MB
nginx                        latest            f6d0b4767a6c   4 weeks ago     133MB
mysql                        5.7               9cfcce23593a   8 months ago    448MB

3、運行容器

運行 mysql 容器:

$ docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
  • --name mysql-test:設置容器名
  • -p 3306:3306 :映射容器服務的 3306 端口到宿主機的 3306 端口。
  • MYSQL_ROOT_PASSWORD=123456:設置 MySQL 服務 root 用戶的密碼。

查看MySQL鏡像是否啟動成功:

$ docker ps
[root@server ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED        STATUS          PORTS                               NAMES
2c5ae10a7543   mysql:5.7   "docker-entrypoint.s…"   7 months ago   Up 23 minutes   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-test

4、進入mysql容器

$ docker exec -it 容器ID或者容器名 /bin/bash
[root@server ~]# docker exec -it mysql-test /bin/bash
root@2c5ae10a7543:/# exit
exit
[root@server ~]# docker exec -it 2c5ae10a7543 /bin/bash
root@2c5ae10a7543:/# 

通過 root 和密碼 123456 訪問 MySQL 服務

5、查看mysql鏡像的ip地址

方法一

$ docker inspect mysql-test

方法二

[root@server local]# docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                              NAMES
3bab7ac2a2af        jenkins/jenkins:lts   "/sbin/tini -- /usr/…"   9 hours ago         Up 9 hours          0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp   jenkins
2c5ae10a7543        mysql:5.7             "docker-entrypoint.s…"   6 days ago          Up 8 hours          0.0.0.0:3306->3306/tcp, 33060/tcp                  mysql-test
[root@server local]# docker exec -it mysql-test bash
root@2c5ae10a7543:/# cat /etc/hosts
127.0.0.1    localhost
::1    localhost ip6-localhost ip6-loopback
fe00::0    ip6-localnet
ff00::0    ip6-mcastprefix
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
172.17.0.3    2c5ae10a7543
root@2c5ae10a7543:/#

命令行連接mysql

root@2c5ae10a7543:/# mysql -h 172.17.0.3 -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.30 MySQL Community Server (GPL)

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

6、增加遠程訪問docker MySQL權限

在步驟4中,可以進入mysql容器后連接mysql數據庫,那么怎么在外面(我的docker mysql安裝在centos虛擬機中,想在宿主機上連接docker安裝的mysql)也可以連接mysql容器呢?

前面已經講過授權其他IP訪問mysql,這里類似。先進入docker mysql容器內,連接mysql數據庫,然后對root賬戶進行授權並刷新設置:

root@2c5ae10a7543:/# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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 PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.66 sec)

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

授權成功后,在宿主機上連接虛擬機上docker安裝的mysql:

C:\WINDOWS\system32>mysql -h 192.168.30.8 -P 3306 -u root -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.30 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> select user,host,authentication_string from mysql.user;
+---------------+-----------+-------------------------------------------+
| user          | host      | authentication_string                     |
+---------------+-----------+-------------------------------------------+
| root          | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys     | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| root          | %         | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+---------------+-----------+-------------------------------------------+
4 rows in set (0.11 sec)

mysql>

連接成功!

數據庫管理工具連接MySQL

除了通過命令行連接管理數據庫,為了更加方便管理mysql數據庫,有很多數據庫管理工具可以通過GUI界面管理數據庫,下面介紹數據庫管理工具MySQL Workbench連接數據庫方法。

MySQL Workbench是MySQL官方自帶的免費管理工具,下載地址:https://dev.mysql.com/downloads/workbench/

1. 連接宿主機安裝的mysql
安裝成功后打開Workbench,添加連接,設置連接名,配置主機名,用戶名。

輸入密碼,連接成功,可以看到數據庫

2. 連接虛擬機安裝的docker mysql
添加連接,設置連接名,配置主機名,用戶名

輸入密碼,連接成功

也可以使用Navicat工具連接數據庫,連接方法類似

--THE END--

文章標題:MySQL數據庫安裝配置詳細教程
本文作者:hiyo
本文鏈接:https://www.cnblogs.com/hiyong/p/14406550.html
歡迎關注公眾號:「測試開發小記」及時接收最新技術文章!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM