Centos7安装Mysql5.7实录(yum+rpm+源码)

Centos7.x版本下针对Mysql的安装和使用多少跟之前的Centos6之前版本有所不同的,废话就不多赘述了,下面介绍下在centos7.x环境里安装mysql5.7的几种方法

一、yum方式安装

从CentOS 7.0发布以来,yum源中开始使用Mariadb来代替MySQL的安装。即使你输入的是yum install -y mysql , 显示的也是Mariadb的安装内容。
使用源代码进行编译安装又太麻烦。因此,如果想使用yum安装MySQL的话,就需要去下载官方指定的yum源。
yum下载网址为:https://dev.mysql.com/downloads/repo/yum/,出现在页面下方的yum下载地址为最新的mysql8.0版本,不是我们想要的mysql5.7版本。
我们可以访问https://repo.mysql.com/yum/,这个页面中包含了mysql的很多版本(5.5,5.6,5.7,8.0),进入目录mysql-5.7-community/el/7/x86_64/中,来到页面最下方就找到了需要的mysql57-community-release-el7-10.noarch.rpm,直接点击下载即可。

1.下载安装MySQL的Yum资源库
[root@mysql ~]# yum localinstall -y https://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm

2.安装MySQLl5.7
[root@mysql ~]# yum install -y mysql-community-server

3.启动MySQL服务器和MySQL的自动启动
[root@mysql ~]# systemctl start mysqld.service
[root@mysql ~]# systemctl enable mysqld.service

4.密码问题
由于MySQL从5.7开始不允许首次安装后使用空密码进行登录!为了加强安全性,系统会随机生成一个密码以供管理员首次登录使用,
这个密码记录在/var/log/mysqld.log文件中,使用下面的命令可以查看此密码:
[root@mysql ~]# cat /var/log/mysqld.log|grep 'A temporary password'
2019-06-04T07:02:32.856059Z 1 [Note] A temporary password is generated for root@localhost: :gaIl>rst9r%
最后一行冒号后面的部分:gaIl>rst9r%就是初始密码。
使用此密码登录MySQL:
[root@mysql ~]# mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26

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

使用随机生产的密码登录到服务端后,必须马上修改密码,不然会报如下错误:
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

有两种方法解决上面的报错(如下的123456是修改后的密码):
mysql> set password=password("123456");
或者
mysql> alter user 'root'@'localhost' identified by '123456';

刷新权限
mysql> flush privileges;
=======================================================================

如果上面在执行set password=password("123456");命令后出现下面的报错:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

解决办法:
这个与Mysql 密码安全策略validate_password_policy的值有关,validate_password_policy可以取0、1、2三个值:
0 or LOW       Length
1 or MEDIUM    Length; numeric, lowercase/uppercase, and special characters
2 or STRONG    Length; numeric, lowercase/uppercase, and special characters; dictionary

默认的数值是1,符合长度,且必须含有数字,小写或大写字母,特殊字符。
所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。

有时候,只是为了自己测试,不想密码设置得那么复杂,譬如说,我只想设置root的密码为123456。
必须修改两个全局参数:
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

修改上面两个参数后,就可以解决这个报错了。
======================================================================

注意一点:
mysql5.7之后的数据库里mysql.user表里已经没有password这个字段了,password字段改成了authentication_string。
所以修改密码的命令如下:

mysql> update mysql.user set authentication_string=password('durant@123') where user='root';     
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

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

mysql> 
=======================================================================

查看mysql版本
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.26    |
+-----------+
1 row in set (0.00 sec)

mysql> 
=======================================================================

修改mysql5.7的编码由latin1为utf8

默认编码:
mysql> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.01 sec)

mysql> 

调整操作:
[root@mysql ~]# cat /etc/my.cnf
......
[mysqld]
......
character-set-server=utf8                //注意这个不能写成default-character-set=utf8,否则会导致5.7版本mysql无法打开

[client]
default-character-set=utf8

[root@mysql ~]# systemctl restart mysqld.service
[root@mysql ~]# mysql -p
......
mysql> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.02 sec)

+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

mysql> 

二、RPM包方式安装

1.卸载系统自带的 mysql和mariadb-lib
[root@mysql ~]# /bin/rpm -e $(/bin/rpm -qa | grep mysql|xargs) --nodeps
[root@mysql ~]# /bin/rpm -e $(/bin/rpm -qa | grep mariadb|xargs) --nodeps

2.下载mysql5.7.25rpm安装包
下载地址:https://downloads.mysql.com/archives/community/,在这个页面中可以根据系统版本选择需要的mysql版本,这里选择5.7.25
[root@mysql soft]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar
[root@mysql soft]# tar -xvf mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar
[root@mysql soft]# ll
total 1031284
-rw-r--r--. 1 root root  528015360 Jun  4 22:25 mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar
-rw-r--r--. 1 7155 31415  25402568 Dec 24 13:42 mysql-community-client-5.7.25-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415    280904 Dec 24 13:42 mysql-community-common-5.7.25-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415   3839640 Dec 24 13:42 mysql-community-devel-5.7.25-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415  46782224 Dec 24 13:42 mysql-community-embedded-5.7.25-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415  24079656 Dec 24 13:42 mysql-community-embedded-compat-5.7.25-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 130149460 Dec 24 13:42 mysql-community-embedded-devel-5.7.25-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415   2271668 Dec 24 13:42 mysql-community-libs-5.7.25-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415   2116628 Dec 24 13:42 mysql-community-libs-compat-5.7.25-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 173130520 Dec 24 13:42 mysql-community-server-5.7.25-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 119947432 Dec 24 13:43 mysql-community-test-5.7.25-1.el7.x86_64.rpm

依次执行(几个包有依赖关系,所以执行有先后)下面命令安装 
[root@mysql soft]# rpm -ivh mysql-community-common-5.7.25-1.el7.x86_64.rpm
[root@mysql soft]# rpm -ivh mysql-community-libs-5.7.25-1.el7.x86_64.rpm
[root@mysql soft]# rpm -ivh mysql-community-client-5.7.25-1.el7.x86_64.rpm
[root@mysql soft]# rpm -ivh mysql-community-server-5.7.25-1.el7.x86_64.rpm

=======================================================================
可能在安装mysql-community-server-5.7.25-1.el7.x86_64.rpm的时候会有如下报错:
[root@mysql soft]# rpm -ivh mysql-community-server-5.7.25-1.el7.x86_64.rpm 
warning: mysql-community-server-5.7.25-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
        libaio.so.1()(64bit) is needed by mysql-community-server-5.7.25-1.el7.x86_64
        libaio.so.1(LIBAIO_0.1)(64bit) is needed by mysql-community-server-5.7.25-1.el7.x86_64
        libaio.so.1(LIBAIO_0.4)(64bit) is needed by mysql-community-server-5.7.25-1.el7.x86_64
        
这个报错的意思是需要安装libaio包和net-tools包:

安装libaio-0.3.107-10.el6.x86_64.rpm 
[root@mysql soft]# wget http://mirror.centos.org/centos/6/os/x86_64/Packages/libaio-0.3.107-10.el6.x86_64.rpm
[root@mysql soft]# rpm -ivh libaio-0.3.107-10.el6.x86_64.rpm --force

安装net-tools 
[root@mysql soft]# yum install net-tools 
=======================================================================

使用rpm安装方式安装mysql,安装的路径如下:
数据库目录
/var/lib/mysql/
 
配置文件
/usr/share/mysql(mysql.server命令及配置文件)
/etc/my.cnf
 
相关命令
/usr/bin(mysqladmin mysqldump等命令)
 
启动脚本
/etc/rc.d/init.d/(启动脚本文件mysql的目录)

3.数据库初始化 
为了保证数据库目录为与文件的所有者为 mysql 登陆用户,如果你是以 root 身份运行 mysql 服务,需要执行下面的命令初始化
[root@mysql ~]# mysql_install_db --datadir=/var/lib/mysql      //必须指定datadir,执行后会生成~/.mysql_secret密码文件
[root@mysql ~]# mysqld --initialize --user=mysql    //新版的推荐此方法,执行生会在/var/log/mysqld.log生成随机密码。如果是以mysql身份运行,则可以去掉--user选项。

4.更改mysql数据库目录的所属用户及其所属组,然后启动mysql数据库
[root@mysql ~]# chown -R mysql:mysql /var/lib/mysql
[root@mysql ~]# systemctl start mysqld.service

5.根据第3步中的密码登录到mysql,更改root用户的密码,新版的mysql在第一次登录后更改密码前是不能执行任何命令的

另外--initialize 选项默认以“安全”模式来初始化,则会为 root 用户生成一个密码并将该密码标记为过期,登陆后你需要设置一个新的密码,
而使用--initialize-insecure命令则不使用安全模式,则不会为 root 用户生成一个密码。 

这里演示使用的--initialize初始化的,会生成一个 root 账户密码,密码在log文件里,如下最后的"K/mEs9*+qx3K"即为随即生成的root密码
[root@mysql ~]# cat /var/log/mysqld.log
......
2019-06-04T14:58:06.047280Z 1 [Note] A temporary password is generated for root@localhost: K/mEs9*+qx3K

[root@mysql ~]# mysql -p'K/mEs9*+qx3K'
mysql> set password=password('durant@123');
mysql> flush privileges;

三、编译方式安装

1.卸载系统自带的 mysql和mariadb-lib
[root@mysql ~]# /bin/rpm -e $(/bin/rpm -qa | grep mysql|xargs) --nodeps
[root@mysql ~]# /bin/rpm -e $(/bin/rpm -qa | grep mariadb|xargs) --nodeps

2.安装编译环境
[root@mysql ~]# yum -y install make gcc-c++ cmake bison-devel ncurses-devel

3.安装boost
[root@mysql ~]# mkdir -p /usr/local/boost
[root@mysql ~]# cd /usr/local/boost/
wget https://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
[root@mysql boost]# tar -zxvf boost_1_59_0.tar.gz

4.编译安装mysql5.7.25
[root@mysql ~]# groupadd mysql
[root@mysql ~]# useradd -g mysql mysql -M -s /sbin/nologin
[root@mysql ~]# cd /usr/local/src/
[root@mysql src]# wget http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.7/mysql-5.7.25.tar.gz
[root@mysql src]# tar -zxvf mysql-5.7.25.tar.gz
[root@mysql src]# cd mysql-5.7.25
/usr/bin/cmake -DCMAKE_INSTALL_PREFIX=/data/mysql -DMYSQL_DATADIR=/data/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=/usr/local/boost

[root@mysql mysql-5.7.25]#  make && make install

5.修改/data/mysql权限
[root@mysql mysql-5.7.25]# mkdir -p /data/mysql/data
[root@mysql mysql-5.7.25]# chown -R mysql:mysql /data/mysql

6.执行初始化配置脚本,创建系统自带的数据库和表
[root@mysql mysql]# /data/mysql/bin/mysqld --basedir=/data/mysql --datadir=/data/mysql/data --user=mysql --initialize
2019-06-05T07:34:25.320446Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-06-05T07:34:25.568345Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-06-05T07:34:25.597709Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-06-05T07:34:25.664702Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 58228129-8764-11e9-b105-000c29b1fba8.
2019-06-05T07:34:25.667573Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-06-05T07:34:25.672622Z 1 [Note] A temporary password is generated for root@localhost: L->08o4UgOcV

如上最后一行,初始化完成之后会生成一个临时的密码,密码为'L->08o4UgOcV',如果想初始化表空间,
在后面加上 --innodb_data_file_path=ibdata1:1G:autoextend即可。

7.配置my.cnf,仅供参考,可以不配,用默认的
vim /etc/my.cnf

[client]
default-character-set=utf8
socket = /data/mysql/var/mysql.sock

[mysqld]
port = 33306
socket = /data/mysql/var/mysql.sock

basedir = /data/mysql
datadir = /data/mysql/data
pid-file = /data/mysql/data/mysql.pid
user = mysql
innodb_buffer_pool_size = 1536M
innodb_log_file_size = 512M
query_cache_size = 0
slow_query_log = 1
long_query_time = 2
slow_query_log_file = /data/mysql/data/mysql-slow.log
log_error = /data/mysql/data/mysql-error.log
max_allowed_packet = 2048M
max_connections = 2000
open_files_limit = 10240
character-set-server=utf8
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
binlog_format = row
transaction-isolation = Read-Committed
skip-name-resolve
lower_case_table_names = 1

default-time-zone = '+8:00'
event_scheduler = ON
group_concat_max_len = 102400
local-infile=0
skip_symbolic_links=yes

[mysql]
default-character-set=utf8

8.启动mysql服务
[root@mysql ~]# cd /data/mysql
[root@mysql mysql]# mkdir var
[root@mysql mysql]# chown -R mysql:mysql var
[root@mysql mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@mysql mysql]# systemctl enable mysqld
[root@mysql mysql]# systemctl start mysqld

9.设置环境变量
[root@mysql mysql]# echo "export PATH=$PATH:/data/mysql/bin" >> /etc/profile
[root@mysql mysql]# source /etc/profile

10.设置mysql密码
上面提到的mysql初始密码为L->08o4UgOcV,需要先用这个密码进行登录(这个是临时密码),再进行密码的修改。
[root@mysql mysql]# mysql -p'L->08o4UgOcV'
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@mysql mysql]# mysql -p

mysql> set password=password('durant@123');
Query OK, 0 rows affected, 1 warning (0.01 sec)

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

参考文章:
https://www.cnblogs.com/kevingrace/p/8340690.html

你可能感兴趣的:(Mysql)