Home >Database >Mysql Tutorial >How to install mysql using command in cmd?

How to install mysql using command in cmd?

青灯夜游
青灯夜游Original
2020-10-26 09:43:4410707browse

Method: 1. Download the compressed package from the official website and extract it to the specified directory; 2. Create a configuration file named my.ini; 3. Open the command line window as an administrator and switch directories Go to the bin directory of MySQL; 4. Execute the "mysqld install MySql service name" installation command, and then initialize.

How to install mysql using command in cmd?

(Recommended tutorial: mysql video tutorial)

1. Download the compressed package

Note: Under Windows, to install MySQL, you can directly download the installation package (download address: https://dev.mysql.com/downloads/installer/), and then directly follow the prompts of the installation package to install what you need. Function. You can also download the zip package directly. The compressed package only contains the basic files required by the MySQL server and does not have a visual graphical interface (download address: https://dev.mysql.com/downloads/mysql/). This article introduces the installation of MySQL (community edition: 8.0.12) through the installation package.

2. Installation of MySQL service

Unzip the compressed package

After decompressing the compressed package, You can see the following files and folders:
How to install mysql using command in cmd?
Note: There is no data directory or .ini type configuration file in the decompressed files and folders. The configuration file is used to configure some basic information of the MySQL server, including the port when starting, and the data directory is used to store the database of the MySQL server.

Installation Service

If there is no configuration file, you can create a configuration file named my.ini yourself.
Run as administratorOpen the command line window (note, you must use the identity of the administrator), switch the current directory to the bin directory of MySQL (the previous bin directory , it has little effect in which directory the decompressed folder is placed). Execute the installation command:

mysqld install MySql80

If you want to uninstall a MySql service, you can use the following command:

mysqld remove MySqll80

Note: the following MySql80 is the name of the MySql service, you can Specify the name yourself. If you want to install multiple MySql services on one machine, the names of the services must be different.

Initialization

After installation, you need to use the following command to initialize:

mysqld --initialize --console

--console Used to output some information during initialization. You can see some basic information during initialization. In the output information, find the following statement: You can see that the user name of the account is root, and the password is a randomly generated string.

A temporary password is generated for root@localhost: J-Wh5?#=<ifv><br> You can use this command to log in later, but you need to change the password to normal. use. </ifv>

initialize is used for initialization. You can also use initialize-insecure to indicate non-safe mode. In this way, after initialization, the user name of the account is root and the password is empty.
root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
It is recommended to use the initialize command for initialization. The data directory must be empty during initialization.

You can also add other parameters to specify some initialization parameters. For example:

--defaults-file= to specify the configuration file to use.

Start the MySQL service

Enter the command at the command line to start the mysql service, where mysql80 is installed previously MySQL service name:

net start mysql80

If you want to shut down the service, you can enter the following command to shut down the mysql service:

net stop mysql80

Note: After the previous installation is initialized, it will not start automatically. MySQL service, you need to start the MySQL service manually. You can enter the command to start the service in any directory during startup (because the net command can be used in any directory, not the MySQL command).

Log in to MySQL

Command: mysql -uroot -p
After pressing Enter, you will be prompted Enter the password:
How to install mysql using command in cmd?
You can also fill in the password directly after -p. For example: For an account with the user name root and password abcd, you can log in using the following method:
mysql -uroot -pabcd. Note: There is no space between -u and username, -p and password. Some parameters of
mysql:

  • -u指定用户名。
  • -p指定密码。
  • -h指定主机地址。默认为localhost
  • -P指定端口号,默认为3306(mysql服务默认的启动端口为3306)。例如:-P13306表示连接端口号为13306的MySQL服务器。(注意:密码为小写的p,端口号为大写的P)
    可以在配置文件中指定启动的端口号。

修改密码

登录之后,在输入命令时,前面的提示符为:mysql >在这后面输入的语句中,默认提交符号为分号(;)。按回车时只会换行,并不会提交语句。之后输入 ; 之后按回车才会提交语句。

  • 如果前面对mysql进行初始化使用的是--initialize-insecure命令,则密码为空,在输入密码时直接按回车即可。登录之后,不用修改密码即可直接使用MySQL。例如:创建数据库等操作。
  • 如果前面对mysql进行初始化使用的是--initialize命令,则密码为系统随机生成的一个字符串。登录之后需要修改密码才能使用。
    若不修改密码,直接操作数据库,会出现如下错误:
    ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
    使用如下命令修改密码:
    set password='new_password'注意:新的密码前后有单引号。在mysql中,单引号之间表示的是字符串。
    alter user 'root'@'localhost' password expire never;设置密码永不过期。
    flush privileges刷新权限。
    之后退出(使用\q,或者exit或者quit都可以退出),然后重新登录即可。

至此,可以正常使用MySQL了。

三、多个MySQL的安装

若想要在一台机器上安装多个MySQL服务。可以将之前解压后的文件夹复制到其他目录下(注意:data需要为空,否则无法初始化)。然后按照前面的步骤安装MySQL服务。需要注意的有:

  • 多个MySQL服务的名称不能相同。相同时无法安装成功
  • 多个MySQL服务的端口号不能相同(可以在配置文件中配置端口号)。一个端口只能监听一个MySQL服务,因此端口号相同时只能启动一个MySQL服务。

四、修改配置文件

可以在配置文件中填写自己需要的服务器配置。(配置文件中 # 表示单行注释)
下面是几个基本的,当然你也可以让配置文件为空,让服务器使用默认的配置。可以参照官方文档填写自己需要的配置。

[mysqld]
# 指定mysql的安装目录
basedir=D:\BaiduYunDownload\mysql-8.0.12-winx64
# 指定mysql数据库的数据的存放目录data
datadir=D:\BaiduYunDownload\mysql-8.0.12-winx64\data
# 指定端口号
port=3306
# 设置允许的最大连接数
max_connections=200

其中,对于多个MySQL服务的安装最重要的就是指定端口号了。在安装时指定配置文件,若没有指定,默认为安装目录下的my.ini文件(与bin目录在同一个文件夹下)。对于端口号不在3306的MySQL服务,登录时需要添加-P来指定其端口号。

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of How to install mysql using command in cmd?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn