search
HomeDatabaseMysql TutorialMysql安装(UsingGenericBinaries)_MySQL

本次 Mysql 为Community 5.6.21 版本,安装方式为通用Linux安装方式,即大多数Linux平台都可以采用该方式进行安装。

一、安装步骤

1、安装环境

1)Centos 7.0.1406 X86_64

2、下载 Mysql 、解压缩、创建软连接,与官方提供解压缩路径有些许。

$ wget http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.21-linux-glibc2.5-x86_64.tar.gz
$ sudo tar zxvf mysql-5.6.21-linux-glibc2.5-x86_64.tar.gz -C /usr/local/src/
$ sudo ln -s /usr/local/src/mysql-5.6.21-linux-glibc2.5-x86_64/ /usr/local/mysql

3、安装方式分为两种。

方式1 参考 Mysql 官方指导建议进行安装,多用在Mysql学习环境,下列命令来源Mysql官方,原文参见 Installing MySQL on Unix/Linux Using Generic Binaries

shell> groupadd mysql
shell> useradd -r -g mysql mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server

方式2 自定义 Mysql 数据保存路径,多用在部署环境。

1)创建Mysql用户以及用户组

$ sudo groupadd mysql
$ sudo useradd -r -g mysql mysql
$ cd /usr/local/src/
$ sudo chown -R mysql:mysql mysql-5.6.21-linux-glibc2.5-x86_64/ #定义mysql用户以及组
$ ll
总用量 4
drwxr-xr-x. 13 mysql mysql 4096 11月  5 08:10 mysql-5.6.21-linux-glibc2.5-x86_64

2)创建Mysql数据文件保存位置,若使用数据盘则自行挂载。

$ sudo mkdir -p /data/mysql #定义mysql数据文件保存地址

3)初始化 Mysql,可能会出现的问题参见文章底部问题处理方法

$ sudo /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/ #初始化 mysql

4)配置 my.cnf

$ sudo vi /usr/local/mysql/my.cnf 

本示例仅保证可以正常运行,所以配置如下三项即可。

  basedir = /usr/local/mysql
  datadir = /data/mysql
  port = 3306

建立软连接 my.cnf 到 /etc/ 目录

$ sudo ln -s /usr/local/mysql/my.cnf /etc/my.cnf

5)启动 Mysql 服务

$ sudo /usr/local/mysql/support-files/mysql.server start
Starting MySQL. SUCCESS! 

6)登陆 Mysql

$ /usr/local/mysql/bin/mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.21 MySQL Community Server (GPL)

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

二、开机启动

$ sudo chkconfig --add mysqld

三、环境变量

1)编辑 profile 文件

$ sudo vi /etc/profile

2)添加下列信息到 profile 底部

export PATH=$PATH:/usr/local/mysql/bin

3)立即生效配置文件

$ source /etc/profile

四、Mysql远程连接

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.01 sec)

mysql> grant all privileges  on *.* to root@'%' identified by "你的密码";
Query OK, 0 rows affected (0.00 sec)

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

五、防火墙开启

Centos 7 默认启用 firewall 管理端口

1)查看端口开启情况,若之前没有配过会显示 no 说明 3306 端口未开放,反之 yes 说明已开放直接可用 Mysql 客户端远程访问!

$ sudo firewall-cmd --query-port=3306/tcp
no

2)临时性开启 3306 端口

$ sudo firewall-cmd --add-port=3306/tcp
success

3)永久性开启 3306 端口

$ sudo firewall-cmd --permanent --zone=public --add-port=3306/tcp
success
$ sudo firewall-cmd --reload #重新加载配置
success
[john@localhost ~]$ sudo firewall-cmd --zone=public --list-all #查看添加结果
public (default, active)
  interfaces: eth0
  sources: 
  services: dhcpv6-client ssh
  ports: 3306/tcp 22/tcp
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 

六、问题整理

问题1:

$ sudo scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/
sudo: unable to execute scripts/mysql_install_db: No such file or directory

解决:解决这个问题纯属巧合,去掉 sudo 提示 Perl 解析器有问题,重新安装下

$ sudo yum install perl
$ sudo yum install perl-Data-Dumper.x86_64

问题2:

$ sudo scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/
Installing MySQL system tables.../usr/local/mysql//bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

解决:

$ sudo yum install libaio.x86_64

问题3:

$ sudo support-files/mysql.server start
Starting MySQL. ERROR! The server quit without updating PID file (/data/mysql/localhost.localdomain.pid).
解决:这个问题特别实在首次启动时会提示,简单说就是找不到 my.cnf 文件,将配置完毕 my.cnf 复制到 /etc/ 或 建立软连接到 /etc/ 目录下!
$ sudo cp my.cnf /etc/
这里我有点疑惑,表面现象 mysql 启动依赖 /etc/my.cnf 文件,但实际第一次正常启动 mysql 后可以删除 /etc/my.cnf 文件,第二次启动可以正常加载 /usr/local/mysql/my.cnf 文件!

参考文章:

Centos7 安装Mysql 5.6.19

CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14方法分享

RHEL7中防火墙firewalld的配置(1)

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
How to solve the problem of mysql cannot open shared libraryHow to solve the problem of mysql cannot open shared libraryMar 04, 2025 pm 04:01 PM

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

Reduce the use of MySQL memory in DockerReduce the use of MySQL memory in DockerMar 04, 2025 pm 03:52 PM

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

How do you alter a table in MySQL using the ALTER TABLE statement?How do you alter a table in MySQL using the ALTER TABLE statement?Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Run MySQl in Linux (with/without podman container with phpmyadmin)Run MySQl in Linux (with/without podman container with phpmyadmin)Mar 04, 2025 pm 03:54 PM

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

What is SQLite? Comprehensive overviewWhat is SQLite? Comprehensive overviewMar 04, 2025 pm 03:55 PM

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

Running multiple MySQL versions on MacOS: A step-by-step guideRunning multiple MySQL versions on MacOS: A step-by-step guideMar 04, 2025 pm 03:49 PM

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

How do I configure SSL/TLS encryption for MySQL connections?How do I configure SSL/TLS encryption for MySQL connections?Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.