安装MySQL5.7系列

机器环境: Centos 7 64位
安装目录: /usr/local/mysql
启动脚本: /etc/init.d/mysqld
数据目录: /data/mysql
安装版本: 5.7.17
安装过程: 源码

1.安装依赖

#yum install –y autoconf automake imake libxml2-devel expat-devel cmake gcc gcc-c++ libaio libaio-devel bzr bison libtool ncurses5-devel ncurses-devle

2.检查你所用的Linux下有没有安装过mysql,没有卸载干净

#rpm -qa|grep -i mysql
mysql-5.7.13-linux-glibc2.5-x86_64

*可见已经安装了库文件,应该先卸载,不然会出现覆盖错误。注意卸:载时使用了--nodeps选项,忽略了依赖关系:
#rpm -e mysql-5.7.13-linux-glibc2.5-x86_64 --nodeps

当然你的也可能不止这一个文件,也可能有多个,那么你就依次 rpm -e xx --nodeps 卸载,卸载完了再检查一下,若确定删除干净后,在经行下面步骤。

  1. 创建mysql的用户组/用户, data目录及其用户目录在这步之前一定要先确保你所建的用户以及用户组没有存在,要不然在后面的过程中会报错,删除时候要先删除用户在删除用户组名。
# groupadd mysql                                          # 创建一个名为mysql的用户组       
#useradd -r -g mysql -s /bin/false mysql       # 在用户组下创建用户
#mkdir -p /data/mysql                   #创建data目录
#chown -R mysql.mysql /data/mysql       #给data目录授权

4.安装boost库|检查编译环境|编译安装

将文件上传到服务器上
#tar -zxvf mysql-boost-5.7.17.tar.gz   -C /usr/local/src  # 解压boost库文件(这里解压出来的目录名和mysql-5.7.17.tar.gz解压出来的目录名一样)
#cp –a /usr/local/src/mysql-5.7.17/ /usr/local/boost
# cmake -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost             # 编译boost库

#tar -zxvf mysql-5.7.17.tar.gz #解压数据库文件
#cd  mysql-5.7.17 
执行cmake命令
#cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DCOMPILATION_COMMENT='Zhaofx for mysqltest' \
-DWITH_READLINE=ON \
-DSYSCONFDIR=/data/mysql \
-DWITH_BOOST=/usr/local/boost \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock

在cmake完成后,执行下面的命令进行编译与安装:
# make  && make install       ---开始编译 && 编译安装
编译安装后,可以确认mysql软件目录:
# chown -R mysql:mysql /usr/local/mysql/
# ls -la /usr/local/mysql/
#编译过程中出错,清除临时文件重新编译:
#make clean
#rm CMakeCache.txt


安装完后把可执行文件路径加到系统环境变量:
#echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
#source /etc/profile
创建软链接
# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
#使用默认的配置文件(好像没有默认配置文件,略过)
#cd /usr/local/mysql/support-files
#cp my-default.cnf ../my.cnf

5.初始化数据库
#/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql #执行初始化

[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2016-04-08T01:47:59.945537Z 0
[Warning] InnoDB: New log files created, LSN=45790 2016-04-08T01:48:00.333528Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2016-04-08T01:48:00.434908Z 0
[Warning] No existing UUID has been found, so we assume that this is the first timethat this server has been started. Generating a new UUID: ece26421-fd2b-11e5-a1e3-00163e001e5c. 2016-04-08T01:48:00.440125Z 0
[Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2016-04-08T01:48:00.440904Z 1
[Note] A temporary password is generated for root@localhost: **mjT,#x_5sW

mysqld --initialize安装的MySQL部署默认是安全的,以下更改已作为默认部署特征实现:
· 安装过程中只创建一个root账号,'root'@'localhost'自动为该账户生成随机密码,并标记密码为已过期状态,MySQL管理员必须用root的随机密码进行连接并设置新密码。(服务器将随机密码写入错误日志)
· 安装不会创建匿名用户账号
· 安装不会创建test数据库
关于error log的位置,如果安装的是RPM包,则默认是/var/log/mysqld.log。

6.检测下是否能启动mysql服务

复制启动脚本:
#cd /usr/local/mysql/support-files
#cp mysql.server /etc/init.d/mysql
直接启动
#mysqld_safe --user=mysql &
或
#/etc/init.d/mysqld start
或
#service mysql start
Starting MySQL.. OK!
这是正常的情况下。

如果安全脚本用不了则跳过,进入数据库进行更改密码
启动后调用安全脚本mysql_secure_installation,实现:更改root密码,删除测试库,禁止root远程登录。通过这几项的设置能够提高mysql库的安全
运行mysql_secure_installation会执行几个设置:
a)为root用户设置密码|Y,设置密码
b)删除匿名账号|Enter
c)取消root用户远程登录|Y
d)删除test库和对test库的访问权限|Enter
e)刷新授权表使修改生效|Enter

7.创建配置文件

#设置编码,可按需修改新的配置文件选项, 不修改配置选项, mysql则按默认配置参数运行. 
如下是我修改配置文件/etc/my.cnf, 设置编码为utf8以防乱码
# vim /etc/my.cnf
 
[mysqld]
basedir = /usr/local/mysql
datadir = /data/mysql
character_set_server=utf8
init_connect='SET NAMES utf8'
max_connections=1000
  
[client]
default-character-set=utf8

参数说明:
character_set_server=utf8 //设置编码为utf8以防乱码
max_connections=16384 //设置最大连接数为16384(数据库最大限制)

8.配置mysql服务开机自动启动

#cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql # 拷贝启动文件到/etc/init.d/下并重命令为mysqld
# chmod 755 /etc/init.d/mysqld                                      # 增加执行权限
# chkconfig --list mysqld                                           # 检查自启动项列表中没有mysqld这个
# chkconfig --add mysqld                                            # 如果没有就添加mysqld
# chkconfig mysqld on                                               # 用这个命令设置开机启动

服务的启动/重启/停止

service mysqld start # 启动服务
service mysqld restart # 重启服务
service mysqld stop # 停止服务

9.初始化mysql用户root的密码

[root@linux mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.7.19 Zhaofx for mysqltest

Copyright (c) 2000, 2017, 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>use mysql;               ---会提示没有权限
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.    
mysql> alter user 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.04 sec)
 
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> update user set authentication_string = PASSWORD('123456') where user = 'root';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

10.设置远程登录授权

[root@linux ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.13 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2016, 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> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql>
或者
grant all on *.* to 'root'@'%' identified by '123456'; 

如果不设置远程连接,用mysql数据库连接工具可是连接不上的

11.用数据库连接工具来测试你刚刚的安装是不是成功了
首先要知道,远程连接的Linux系统的ip,确保本机已经能够ping通的情况下再连接。
...
到这里说明mysql安装成功!

你可能感兴趣的:(安装MySQL5.7系列)