Home >Technology peripherals >It Industry >How to Install MySQL

How to Install MySQL

Joseph Gordon-Levitt
Joseph Gordon-LevittOriginal
2025-02-15 10:53:12338browse

How to Install MySQL

Overview of MySQL installation method

MySQL is a popular free and open source relational database that can be installed in a variety of ways, including cloud solutions, Docker containers, or directly installed on your operating system. This article explores three main ways to use MySQL in a local development environment: cloud solutions, Docker containers, and installation directly on your computer.

1. Cloud MySQL solution

MySQL services are provided by AWS, Azure, Google Cloud, Oracle and many other professional hosting services. Even low-cost shared hosting provides MySQL with remote HTTPS or tunneled SSH connections. Therefore, you can use MySQL database remotely in local development.

Pros:

  • No need to install or manage database software;
  • Your production environment can use the same system;
  • Multiple developers can easily access the same data;
  • Ideal for using cloud-based IDE or low-spec devices (such as Chromebooks);
  • May include functions such as automatic scaling, copying, sharding and backup.

Disadvantages:

  • Setting can still take quite a while;
  • The connection library and process across hosts may be slightly different;
  • The experiment is more risky; any developer may accidentally erase or change the database;
  • Development will stop when the network is disconnected;
  • The cost of use can be very high.

The cloud option may be suitable for situations where database needs are minimal or large teams deal with the same complex data set.

2. Run MySQL using Docker container

Docker is a platform that allows you to build, share, and run applications in containers. A container is like an isolated virtual machine with its own operating system, libraries and application files. (In fact, containers are lightweight processes that share host resources.)

Docker image is a snapshot of the file system and can be run as a container. Docker Hub provides images of a variety of popular applications and databases, including MySQL and MariaDB.

Pros:

  • All developers can use the same Docker image on macOS, Linux, and Windows;
  • MySQL installation configuration and maintenance are minimal;
  • The same base image can be used in the development and production environments;
  • Developers retain the advantages of local development and can experiment without risk.

Docker installation (partial steps):

Instructions for installing Docker on Linux can be found on Docker Docs. You can also use official repositories, although these repositories may contain older versions. For example, on Ubuntu:

<code class="language-bash">sudo apt-get update
sudo apt-get remove docker docker-engine docker.io
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker</code>

The installation methods of other Linux distributions will be different. Please search for the corresponding instructions online. The installation methods of macOS and Windows are also slightly different, please refer to the official documentation.

Running MySQL container:

To facilitate communication between Docker containers, create a bridged network called dbnet (if you only want to access MySQL from the host device, you can skip this step):

<code class="language-bash">sudo apt-get update
sudo apt-get remove docker docker-engine docker.io
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker</code>

Now create a data folder on your system where the MySQL table will be stored, for example mkdir data.

The latest MySQL 8 server can be started using the following command:

<code class="language-bash">docker network create --driver bridge dbnet</code>

Particle description:

  • -d Run the container as a background service.
  • --rm Delete the container when it stops running.
  • --name mysql Assign a name to the container called mysql for easy management.
  • -p 3306:3306 Forward the container port to the host.
  • -e Defines environment variables. In this example, the default MySQL root password is set to mysecret.
  • -v Mount the volume so that the /var/lib/mysql MySQL data folder in the container will be stored in the data subfolder of the current folder on the host.

$PWD is the current folder, but this only works on macOS and Linux. Windows users must specify the entire path using forward slash notation, such as /c/mysql/data.

MySQL takes several minutes to start when the first time this command is running, because the Docker image is being downloaded and the MySQL container is being configured. Subsequent restarts will be instant, assuming you have not deleted or changed the original image. You can check progress at any time using the following command:

<code class="language-bash">docker run -d --rm --name mysql --net dbnet -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mysecret -v $PWD/data:/var/lib/mysql mysql:8</code>

Manage containers using Docker Compose:

Docker Compose can manage any number of containers without typing verbose Docker commands. Create a docker-compose.yml file in the current folder:

<code class="language-bash">docker logs mysql</code>

And run:

<code class="language-yaml">version: '3.7'

services:
  mysql:
    environment:
      - MYSQL_ROOT_PASSWORD=mysecret
    image: mysql:8
    container_name: mysql
    volumes:
      - ./data:/var/lib/mysql
    networks:
      - dbnet
    ports:
      - "3306:3306"

  adminer:
    image: adminer
    container_name: adminer
    depends_on:
      - mysql
    networks:
      - dbnet
    ports:
      - "8080:8080"

networks:
  dbnet:</code>

3. Run MySQL on your operating system

If you want to use it across multiple projects in one instance, or need to run the service at startup, installing MySQL on a local development machine may be more practical.

All-in-one software package:

Some excellent all-in-one macOS, Linux, and Windows distributions include Apache, PHP, MySQL, phpAdmin, SSL certificates, frameworks, and other applications in one installation package. Options include: XAMPP, WampServer, EasyPHP, AMPPS, WPN-XM, Wnmp.

Install MySQL on Linux:

There are many ways to install MySQL on various Linux distributions: Some distributions (such as Ubuntu Server) provide MySQL by default; the official documentation provides installation using APT, Yum and SLES package managers, as well as RPM and Debian packages Details; different MySQL versions are available from the snap store.

Install MySQL on macOS:

MySQL can be installed on macOS 10.13 and later by downloading the native installer.dmg disk image. Double-click to mount the image, and then double-click the .pkg file to start the installation wizard.

Install MySQL on Windows:

MySQL can be installed on 64-bit versions of Windows 10 and Windows 2012 Server R2 and later. You also need the following runtime: .NET 4.5.2, Visual C Redistributable. The MSI installer provides servers and MySQL Workbench and other tools. (The smaller "web" MSI installer will download the package when selected.)

Other options and FAQs of MySQL (The FAQs part of the original text is omitted here because the article is too long and has weak correlation with the core content. If necessary, you can propose it separately Question. )

In short, which method of MySQL installation is chosen depends on your specific needs and technical level. For beginners, cloud services or all-in-one software packages may be easier to get started; for large projects or developers who need more granular control, Docker or direct installation is more appropriate.

The above is the detailed content of How to Install MySQL. 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