LNMP 之 MySQL 8.0

下载带 boost 的源码安装包:

MySQL 5.7 之后,需要 BOOST库的支持,MySQL源码包分为带 boost 和不带 boost 的两个版本,不带 boost 库的需要单独安装 boost

[root@localhost software]#
[root@localhost software]# wget https://cdn.mysql.com/Downloads/MySQL-8.0/mysql-boost-8.0.18.tar.gz
--2019-11-11 09:41:32--  https://cdn.mysql.com/Downloads/MySQL-8.0/mysql-boost-8.0.18.tar.gz
正在解析主机 cdn.mysql.com (cdn.mysql.com)... 23.1.245.43
正在连接 cdn.mysql.com (cdn.mysql.com)|23.1.245.43|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:194953221 (186M) [application/x-tar-gz]
正在保存至: “mysql-boost-8.0.18.tar.gz”

100%[==========================================================>] 194,953,221 1.44MB/s 用时 2m 12s

2019-11-10 19:43:45 (1.41 MB/s) - 已保存 “mysql-boost-8.0.18.tar.gz” [194953221/194953221])

[root@localhost software]#

安装编译依赖包

[root@localhost software]# yum -y install gcc gcc-c++ ncurses  ncurses-devel  libaio-devel  openssl openssl-devel
[root@localhost software]#

源码安装 cmake3
编译安装 MySQL 时会要求使用 cmake3进行编译,yum 安装 cmake3,提示需要 python3.4,CentOS默认安装的版本是 python2.7,直接使用源码安装

[root@localhost software]# wget https://github.com/Kitware/CMake/releases/download/v3.15.5/cmake-3.15.5.tar.gz
[root@localhost software]# 
[root@localhost software]# tar -zxf cmake-3.15.5.tar.gz
[root@localhost software]#
[root@localhost software]# chmod -R 777 cmake-3.15.5
[root@localhost software]# cd cmake-3.15.5
[root@localhost cmake-3.15.5]# ./bootstrap
[root@localhost cmake-3.15.5]# make && make install

查看版本

[root@localhost cmake-3.15.5]# cmake -version
-bash: /usr/bin/cmake: 没有那个文件或目录
[root@localhost cmake-3.15.5]#

重新打开 terminal

[root@localhost ~]# cmake -version
cmake version 3.15.5

CMake suite maintained and supported by Kitware (kitware.com/cmake).
[root@localhost ~]#

升级 gcc 版本,yum 默认安装 gcc 为 4.8.5,MySQL编译时需要 gcc5.3及以上版本

[root@localhost ~]# yum -y install centos-release-scl-rh centos-release-scl
[root@localhost ~]# yum -y install devtoolset-4-gcc devtoolset-4-gcc-c+
[root@localhost ~]# source /opt/rh/devtoolset-4/enable
[root@localhost ~]# 
[root@localhost ~]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-4/root/usr/libexec/gcc/x86_64-redhat-linux/5.3.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-4/root/usr --mandir=/opt/rh/devtoolset-4/root/usr/share/man --infodir=/opt/rh/devtoolset-4/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --disable-libgcj --with-default-libstdcxx-abi=gcc4-compatible --with-isl=/builddir/build/BUILD/gcc-5.3.1-20160406/obj-x86_64-redhat-linux/isl-install --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 5.3.1 20160406 (Red Hat 5.3.1-6) (GCC)
[root@localhost ~]#

此时 gcc 只对当前 terminal 有效,如果需要永久生效:
echo "source /opt/rh/devtoolset-4/enable" >> /etc/bashrc
source /etc/bashrc

创建 mysql 用户

[root@localhost software]# groupadd mysql && useradd -r -g mysql -s /bin/false mysql
[root@localhost software]#

创建 mysql 安装目录:

[root@localhost software]# mkdir -p /usr/local/mysql
[root@localhost software]# chown mysql:mysql /usr/local/mysql/
[root@localhost software]#

创建 mysql 数据存储目录:

[root@localhost software]# mkdir -p /data/mysql
[root@localhost software]# chown mysql:mysql /data/mysql/
[root@localhost software]#

编译 MySQL:

[root@localhost software]# tar -zxf mysql-boost-8.0.18.tar.gz
[root@localhost software]# 
[root@localhost software]# cd mysql-8.0.18/
[root@localhost mysql-8.0.18]#
[root@localhost mysql-8.0.18]#
[root@localhost mysql-8.0.18]# cd build/
[root@localhost build]#
[root@localhost build]# cmake ../ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DSYSCONFDIR=/etc -DMYSQL_TCP_PORT=3306 -DWITH_BOOST=../boost -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_USER=mysql
[root@localhost build]#
[root@localhost build]# make -j4
[root@localhost build]# make install

make -j4 允许多个编译命令并行执行,避免 IO瓶颈,有效利用 CPU 的资源。-j 其指定的参数值不宜过多,一般为 CPU 核心数的两倍为宜。

安装后 mysql 目录下生成的文件所属用户为 root,修改为 mysql

[root@localhost build]# chown -R mysql:mysql /usr/local/mysql/
[root@localhost build]#

修改配置文件:

[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock

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

创建日志及 pid 文件保存目录,并修改属主为 mysql

[root@localhost build]# mkdir -p /var/log/mariadb /var/run/mariadb
[root@localhost build]#
[root@localhost build]# chown -R mysql:mysql /var/log/mariadb/
[root@localhost build]# chown -R mysql:mysql /var/run/mariadb/
[root@localhost build]#

初始化 MySQL 数据库

[root@localhost build]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql
2019-11-11T09:58:25.558766Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2019-11-11T09:58:25.559047Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.18) initializing of server in progress as process 23382
2019-11-11T09:58:27.621827Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: FdStGgQle4>k
[root@localhost build]#

注意查看最后的 root@localhost: FdStGgQle4>k,这是初始化生成的默认密码,后面需要使用该密码进行登录

将 mysql 加入到服务,并设置开机自启动

[root@localhost build]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost build]#
[root@localhost build]# chmod 755 /etc/init.d/mysql
[root@localhost build]# service mysql status
MySQL is not running                                       [FAILED]
[root@localhost build]#
[root@localhost build]# chkconfig mysql on
[root@localhost build]#
[root@localhost build]# chkconfig --list|grep mysql

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysql          	0:关	1:关	2:开	3:开	4:开	5:开	6:关
[root@localhost build]#

启动 MySQL

[root@localhost build]# service mysql start
Starting MySQL..                                           [  OK  ]
[root@localhost build]#

登录 MySQL,修改密码,并设置允许远程登录

[root@localhost build]# /usr/local/mysql/bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.18

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>
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
Query OK, 0 rows affected (0.01 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>
mysql> UPDATE user SET host = '%' WHERE user = 'root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql>

此时可以使用远程可视化工具进行连接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值