MySQL的安装和配置

发布于:2025-03-29 ⋅ 阅读:(22) ⋅ 点赞:(0)

MySQL的安装和配置

针对不同的操作系统,根据上面的情况,来选择对应的版本。

window操作系统安装

存在两种安装方式:

  1. 安装包方式
  2. 压缩包方式

安装包方式

没有什么特别的地方,下载安装包。下一步、下一步,直至结束。

在此过程中,注意一些选择问题。

安装方式如下:

1、下载安装包

官网下载对应的安装包,根据需要下载对应的版本即可:
8.0:https://cdn.mysql.com//Downloads/MySQLInstaller/mysql-installer-community-8.0.18.0.msi
5.7:https://cdn.mysql.com//Downloads/MySQLInstaller/mysql-installer-community-5.7.28.0.msi

当然也可以选择自己需要的版本:https://downloads.mysql.com/archives/installer/

2、安装过程

image-20200918153023381

image-20200918153036669

image-20200918153103330

image-20200918153129754

image-20200918153139557

image-20200918153150122

image-20200918153158538

image-20200918153207428

image-20200918153219274

image-20200918153229294

image-20200918153241830

image-20200918153256899

image-20200918153306872

image-20200918153322636

安装包的方式比较简单,安装提示对应的完成操作即可(不同的版本可能有所区别,但是都大同小异),这种方式适合于新手使用,一般开发者都建议使用压缩包的方式完成。

压缩包方式

推荐大家使用这种方式

  1. 下载位置:mysql下载位置

  2. 解压缩后位置:D:\mysql-8.0.15-winx64

  3. 在主目录下复制、创建一个xx.ini,修改为my.ini,修改其中的basedirdatadir

    ​ basedir=mysql主目录
    ​ datadir=mysql主目录/data

  4. 配置bin目录为环境变量

    方便在终端情况下,查找MySQL的相关命令

    ⚠️ 注意:一定要重新启动CMD

  5. 初始化命令(记录控制台给出的随机密码,一定记住):

    ⚠️ 一定要注意:打开cmd时,必须使用管理员身份!!!cmd打开后,切换到bin目录再执行如下命令

    mysqld --initialize --console

  6. 然后先给mysql服务创建名称(方便到时候建立多个mysql服务时不冲突)

    mysqld --install mysql8

  7. 启动服务:

    net start mysql

  8. 登陆旧密码登陆(第3步中的密码):

    mysql -u root -p

  9. 修改密码:

    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
    				    
    # 之后键入修改密码的命令:
    ALTER USER root@localhost IDENTIFIED  BY 'root';
    
  10. 使用新密码登陆:

    mysql -u root -p 新密码

卸载MySQL:

​ 停止服务:net stop mysql

​ 删除服务:mysqld --remove

登录成功后,执行命令:

show databases;

MySQL的配置文件(window版——my.ini):

[mysqld]
# skip_grant_tables
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=D:\dev_soft\mysql-8.0.20-winx64
# 设置mysql数据库的数据的存放目录
datadir=D:\dev_soft\mysql-8.0.20-winx64\data
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为UTF8
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB

[client]
#password = your_password
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8mb4

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4

使用客户端登录MySQL服务器

cmd输入如下命令:

mysql --uroot --p 
mysql -h127.0.0.1 -P3306 -uroot -p

查看安装的MySQL版本:

mysql -V

使用帮助命令查看MySQL终端命令:

mysql> \h

For information about MySQL products and services, visit:
   http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
   http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
   https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
notee     (\t) Don't write into outfile.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
resetconnection(\x) Clean session context.

For server side help, type 'help contents'