Home > Database > Mysql Tutorial > Mysql安装(UsingGenericBinaries)_MySQL

Mysql安装(UsingGenericBinaries)_MySQL

WBOY
Release: 2016-06-01 13:04:22
Original
1285 people have browsed it

本次 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
Copy after login

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
Copy after login

方式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
Copy after login

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

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

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

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

4)配置 my.cnf

$ sudo vi /usr/local/mysql/my.cnf 
Copy after login

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

  basedir = /usr/local/mysql
  datadir = /data/mysql
  port = 3306
Copy after login

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

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

5)启动 Mysql 服务

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

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> 
Copy after login

二、开机启动

$ sudo chkconfig --add mysqld
Copy after login

三、环境变量

1)编辑 profile 文件

$ sudo vi /etc/profile
Copy after login

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

export PATH=$PATH:/usr/local/mysql/bin
Copy after login

3)立即生效配置文件

$ source /etc/profile
Copy after login

四、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)
Copy after login

五、防火墙开启

Centos 7 默认启用 firewall 管理端口

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

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

2)临时性开启 3306 端口

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

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: 
Copy after login

六、问题整理

问题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
Copy after login

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

$ sudo yum install perl
$ sudo yum install perl-Data-Dumper.x86_64
Copy after login

问题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
Copy after login

解决:

$ sudo yum install libaio.x86_64
Copy after login

问题3:

$ sudo support-files/mysql.server start
Starting MySQL. ERROR! The server quit without updating PID file (/data/mysql/localhost.localdomain.pid).
Copy after login
解决:这个问题特别实在首次启动时会提示,简单说就是找不到 my.cnf 文件,将配置完毕 my.cnf 复制到 /etc/ 或 建立软连接到 /etc/ 目录下!
$ sudo cp my.cnf /etc/
Copy after login
这里我有点疑惑,表面现象 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)

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template