在centos7.3中源码安装mysql-5.7.21

时间:2023-01-03 06:57:36

在centos7.3中源码安装mysql-5.7.21

获取mysql源码包 mysql-5.7.21.tar.gz

[root@bogon ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20.tar.gz
[root@bogon ~]# tar -xzvf mysql-5.7.21.tar.gz

下载boost_1_59_0.tar.gz

[root@bogon ~]# wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
[root@bogon ~]# tar -zxvf boost_1_59_0.tar.gz
[root@bogon ~]# mv boost_1_59_0 /usr/local/

准备环境

[root@bogon ~]# yum install -y git,cmake,gcc,gcc-c++,bison, ncurses,ncurses-devel

安装

# mysql数据库管理用户和组
[root@bogon ~]# groupadd mysql #添加mysql用户组
[root@bogon ~]# useradd -r -g mysql -s /bin/false mysql #添加mysql用户

# mysql安装程序目录和数据文件目录
[root@bogon ~]# mkdir -p /usr/local/mysql #mysql安装程序目录
[root@bogon ~]# mkdir -p /usr/local/mysql/data # mysql数据文件目录
[root@bogon ~]# chown -R mysql:mysql /usr/local/mysql # 文件夹权限赋予给mysql用户
[root@bogon ~]# chown -R mysql:mysql /usr/local/boost_1_59_0

# 预编译mysql配置:指定mysql程序安装目录和数据文件目录
[root@bogon ~]# cd mysql-5.7.21
[root@bogon mysql-5.7.21]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=/usr/local/boost_1_59_0 \ # 必须
-DSYSCONFDIR=/etc \ # mysql配置文件my.cnf所在路径`/etc/my.cnf`
-DENABLED_LOCAL_INFILE=1 \
-DEXTRA_CHARSETS=all

# 编译安装mysql程序(1线程近1个小时)
[root@bogon mysql-5.7.21]# make -j `grep processor /proc/cpuinfo | wc -l`
[root@bogon mysql-5.7.21]# make install

# mysql安装成功 /usr/local/mysql下的文件,此时data中是空的,还没有安装数据库实例
[root@bogon mysql]# ll
total 64
drwxr-xr-x. 2 mysql mysql 4096 Jan 20 00:07 bin
-rw-r--r--. 1 mysql mysql 17987 Dec 27 22:46 COPYING
-rw-r--r--. 1 mysql mysql 17987 Dec 27 22:46 COPYING-test
drwxr-xr-x. 5 mysql mysql 178 Jan 20 02:01 data
drwxr-xr-x. 2 mysql mysql 55 Jan 20 00:06 docs
drwxr-xr-x. 3 mysql mysql 4096 Jan 20 00:06 include
drwxr-xr-x. 4 mysql mysql 191 Jan 20 00:07 lib
drwxr-xr-x. 4 mysql mysql 30 Jan 20 00:06 man
drwxr-xr-x. 10 mysql mysql 4096 Jan 20 00:08 mysql-test
-rw-r--r--. 1 mysql mysql 2478 Dec 27 22:46 README
-rw-r--r--. 1 mysql mysql 2478 Dec 27 22:46 README-test
drwxr-xr-x. 28 mysql mysql 4096 Jan 20 00:08 share
drwxr-xr-x. 2 mysql mysql 90 Jan 20 00:08 support-files

# 与此同时,在/etc/文件夹下生成mysql的配置文件
[root@bogon mysql]# ll /etc/
-rw-r--r--. 1 root root 739 Jan 20 01:30 my.cnf # mysqld 配置文件
drwxr-xr-x. 2 root root 31 Jan 20 02:42 my.cnf.d # mysql client端文件配置


# 修改/etc/my.cnf文件和/etc/my.cnf.d/mysql-clients.cnf文件
[root@bogon mysql]# /etc/my.cnf
-----------------------my.cnf---------------------------------
[mysqld]
character-set-server = utf8 #指定server端字符集
collation-server = utf8_general_ci

# 解决问题:TIMESTAMP with implicit DEFAULT value is deprecated
explicit_defaults_for_timestamp=true

# mysql程序安装目录
basedir=/usr/local/mysql

# mysql数据文件目录
datadir=/usr/local/mysql/data

# 客户端连接socket!!![很重要]
socket=/var/lib/mysql/mysql.sock

log_error = /var/log/mysql/error.log
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
--------------------my.cnf---------------------------------

# 配置 /etc/my.cnf.d/mysql-clients.cnf
------------------------mysql-clients.cnf start-----------------------------------------
[root@bogon bin]# vi /etc/my.cnf.d/mysql-clients.cnf
#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#
# 在配置文件中添加“[client]”选项和“[mysql]”选项
# 并使用这两个选项下的“socket”参数值,与“[mysqld]”选项下的“socket”参数值,指向的socket文件路径完全一致

# 解决问题: connect to local MySQL server through socket /var/lib/mysql/mysql.sock

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

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

[mysql_upgrade]
[mysqladmin]
[mysqlbinlog]
[mysqlcheck]
[mysqldump]
[mysqlimport]
-------------------------mysql-clients.cnf end-------------------------------------


# 安装实例
[root@bogon mysql-5.7.21]# cd /usr/local/mysql/bin/
[root@bogon bin]# ./mysqld --initialize-insecure
--user=mysql
--basedir=/usr/local/mysql
--datadir=/usr/local/mysql/data # 不生成root密码
[root@bogon bin]# ./mysqld --initialize
--user=mysql
--basedir=/usr/local/mysql
--datadir=/usr/local/mysql/data # 生成root随机密码,在/root/.mysql_secret文件中

# mysql加入系统服务
[root@bogon bin]# cd /usr/local/mysql/support-files/
[root@bogon support-files]# cp mysql.server /etc/init.d/mysql
[root@bogon support-files]# chkconfig --add mysql # mysql加入系统服务
[root@bogon support-files]# chkconf ig mysql on # 开机启动
[root@bogon support-files]# service mysql start

# 查看mysql监听端口
[root@bogon support-files]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 8826 mysql 32u IPv6 53891 0t0 TCP *:mysql (LISTEN)

# mysql进程
[root@bogon support-files]# ps -ef|grep mysql

# mysql status
[root@bogon support-files]# service mysql status
Redirecting to /bin/systemctl status mysql.service
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: inactive (dead) since Sat 2018-01-20 02:01:27 EST; 1h 26min ago
Docs: man:systemd-sysv-generator(8)
Process: 8591 ExecStop=/etc/rc.d/init.d/mysqld stop (code=exited, status=0/SUCCESS)

Jan 20 00:49:06 bogon systemd[1]: Starting LSB: start and stop MySQL...
Jan 20 00:49:06 bogon mysqld[1925]: Starting MySQL SUCCESS!
Jan 20 00:49:06 bogon systemd[1]: Started LSB: start and stop MySQL.
Jan 20 02:01:27 bogon systemd[1]: Stopping LSB: start and stop MySQL...
Jan 20 02:01:27 bogon mysqld[8591]: ERROR! MySQL server PID file could not be found!
Jan 20 02:01:27 bogon systemd[1]: Stopped LSB: start and stop MySQL.

# 加入系统环境变量中
[root@bogon support-files] vim /etc/profile
[root@bogon support-files] export PATH=/usr/local/mysql/bin:$PATH
[root@bogon support-files] source /etc/profile

# 尝试登陆mysql
[root@bogon support-files]# mysql -u root -p
Enter password:
**Access denied for user 'root'@'localhost' (using password: NO)**

# 解决问题Access denied for user 'root'@'localhost....
[root@bogon support-files]# /etc/init.d/mysql stop
[root@bogon support-files]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
[root@bogon support-files]# mysql -u root mysql #使用mysql数据库
mysql> update user set authentication_string=PASSWORD("123456")where user="root"; # 已经没有password这个字段了..
mysql> flush privileges; #更新权限
mysql> quit #退出

# 尝试再次登陆
[root@bogon etc]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21

Copyright (c) 2000, 2018, 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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user 'root'@'localhost' identified by '111111';
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)

参考

参考: http://blog.csdn.net/xyang81/article/details/51792144