MySQL 二进制免编译安装

前言

系统环境:
本文使用的是Centos7+系统版本,其它版本安装方式大同小异。

MySQL版本:
国内各大开源镜像站,均可下载对应系统的MySQL二进制免编译包。MySQL的版本比较多,并不是使用最新的才更好,因为新版本难免会有一些BUG,所以等一段时间再用比较稳妥。
本文使用mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz版本,如需其它版本请自行下载。

安装 MySQL

root用户下,卸载系统自带的mysql(可能有):

rpm -qa |grep -q mysql && yum remove -y mysql

下载 MySQL 二进制免编译包:

cd /usr/local/src
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz

创建 MySQL 用户及相关目录:

useradd -s /sbin/nologin mysql
cd /usr/local/mysql
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql

将压缩包解压到/usr/local目录下并重命名为mysql,然后初始化:

tar zxf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz /usr/local/mysql
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

执行最后一条命令后,如果出现报错:

FATAL ERROR:please install the follwing Perl modules before executing
.scripts/mysql_install_db:Data::Dumper

这是因为缺少包Perl-Module-Install,解决方法:

yum install -y autoconf

报错:

Installing MySQL system tables..../bin/mysqld: error while loading
shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

解决方法:

yum install -y libaio

报错:

bash: ./scripts/mysql_install_db: /usr/bin/perl: bad interpreter: 没有那个文件或目录

解决方法:

yum -y install perl perl-devel

安装后再重新执行初始化最后一条命令:

./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

如果接下来看到两个OK,并且在/data/mysql目录下生成几个目录和文件,说明初始化完成;
或者执行:

echo $?

输出结果为0,说明初始化完成。

配置MySQL

首先复制配置文件:

cp support-files/my-default.cnf /etc/my.cnf

这里需要注意的是,MySQL版本不同,support-files目录下的配置文件也不相同,例如:

mysql-5.5版本

[root@mysql mysql-5.5.60-linux-glibc2.12-x86_64]# ll support-files/
total 96
-rwxr-xr-x. 1 7161 31415  1153 2月  26 21:22 binary-configure
-rw-r--r--. 1 7161 31415  4528 2月  26 21:22 config.huge.ini
-rw-r--r--. 1 7161 31415  2382 2月  26 21:22 config.medium.ini
-rw-r--r--. 1 7161 31415  1626 2月  26 21:22 config.small.ini
-rw-r--r--. 1 7161 31415   773 2月  26 21:02 magic
-rw-r--r--. 1 7161 31415  4691 2月  26 21:22 my-huge.cnf
-rw-r--r--. 1 7161 31415 19759 2月  26 21:22 my-innodb-heavy-4G.cnf
-rw-r--r--. 1 7161 31415  4665 2月  26 21:22 my-large.cnf
-rw-r--r--. 1 7161 31415  4676 2月  26 21:22 my-medium.cnf
-rw-r--r--. 1 7161 31415  2840 2月  26 21:22 my-small.cnf
-rwxr-xr-x. 1 7161 31415  1061 2月  26 21:22 mysqld_multi.server
-rwxr-xr-x. 1 7161 31415   839 2月  26 21:22 mysql-log-rotate
-rwxr-xr-x. 1 7161 31415 10565 2月  26 21:22 mysql.server
-rw-r--r--. 1 7161 31415  1326 2月  26 21:22 ndb-config-2-node.ini

关于MySQL配置文件my.cnf的配置说明:

  • my-medium.cnf

This is for a system with little memory (32M - 64M) where MySQL plays an important part, or systems up to 128M where MySQL is used together with other programs (such as a web server).

这对于一个内存很小(32M-64M)的系统来说是MySQL的重要组成部分,或者系统最高可达128M,其中MySQL与其他程序(如Web服务器)一起使用,

  • my-small.cnf

This is for a system with little memory (<= 64M) where MySQL is only used from time to time and it's important that the mysqld daemon doesn't use much resources.

这适用于只有少量内存(<= 64M)的系统,其中MySQL只是偶尔使用,重要的是mysqld守护进程不占用太多资源。

  • my-large.cnf

This is for a large system with memory = 512M where the system runs mainly MySQL.

这是一个内存= 512M的大系统,系统主要运行MySQL。

  • my-huge.cnf

This is for a large system with memory of 1G-2G where the system runs mainly MySQL.

这是一个内存大小为1G-2G的系统,系统主要运行MySQL。

  • my-innodb-heavy-4G.cnf

This is a MySQL example config file for systems with 4GB of memory running mostly MySQL using InnoDB only tables and performing complex queries with few connections.

这是一个MySQL示例配置文件,用于具有4GB内存的系统,主要运行仅使用InnoDB表的MySQL,并执行少数连接的复杂查询。

mysql-5.6版本

[root@mysql mysql-5.6.39-linux-glibc2.12-x86_64]# ll support-files/
total 32
-rwxr-xr-x 1 7161 31415  1153 Dec  9 15:53 binary-configure
-rw-r--r-- 1 7161 31415   773 Dec  9 15:33 magic
-rw-r--r-- 1 7161 31415  1126 Dec  9 15:53 my-default.cnf
-rwxr-xr-x 1 7161 31415  1061 Dec  9 15:53 mysqld_multi.server
-rwxr-xr-x 1 7161 31415   894 Dec  9 15:53 mysql-log-rotate
-rwxr-xr-x 1 7161 31415 10565 Dec  9 15:53 mysql.server

mysql-5.7版本

[root@mysql mysql-5.7.22-linux-glibc2.12-x86_64]# ll support-files/
total 24
-rw-r--r-- 1 7161 31415   773 Mar  4 21:40 magic
-rwxr-xr-x 1 7161 31415  1061 Mar  4 22:02 mysqld_multi.server
-rwxr-xr-x 1 7161 31415   894 Mar  4 22:02 mysql-log-rotate
-rwxr-xr-x 1 7161 31415 10576 Mar  4 22:02 mysql.server

系统默认存在my.cnf,提示是否覆盖,输入y确认。
然后打开配置文件,进行一些简单的配置:

vim /etc/my.cnf

取消下列注释,并修改参数。

innodb_buffer_pool_size = 128M
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
server_id = 128
socket = /tmp/mysql.sock
join_buffer_size = 128M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTTON,STRICT_TRANS_TABLES

sql_mode=NO_ENGINE_SUBSTITUTTON,STRICT_TRANS_TABLES在MySQL5.5版本中好像并不支持这个语法。

复制启动脚本,并修改其属性:

cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld

修改启动脚本:

vim /etc/init.d/mysqld

修改如下参数:

datadir = /data/mysql

把开机脚本加入系统服务项,设定开机启动并启动MySQL:

chkconfig --add mysqld
chkconfig mysqld on
service mysqld start

如果启动不了,请到/data/mysql/目录下查看错误日志,这个日志名通常是主机名.err。
检查MySQL是否启动:

ps aux |grep mysqld
netstat -lnp|grep 3306

安全部分

首次进入MySQL数据库是不需要密码的:

/usr/local/mysql/bin/mysql -uroot
……
msyql>

退出时输入quit或exit即可。
以上命令使用绝对路径登录,为化繁为简,把MySQL加入环境变量,并让它开机加载:

PATH=$PATH:/usr/local/mysql/bin
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile

再次登录MySQL数据库:

mysql -uroot
……
mysql>

修改密码:

mysqladmin -uroot password '密码'
Warning: Using a password on the command line interface can be insecure.

警告信息为在命令行下暴露了密码,如下所示,登录MySQL时:

mysql -uroot -p'密码'
……
mysql>

输入密码时要加-p,后面可以直接跟密码。
-p后不可跟空格,单引号可加可不加(密码为特殊字符时必须加)。
为安全起见,-p后不跟密码,使用与用户交互的方式输入密码:

mysql -uroot -p
Enter password:

标签: MySQL

添加新评论