相比于传统行业,互联网浪潮中MySQL一直是数据库的主力军,无论是曾经的SUN,还是现在的Oracle,虽然可能商业策略不同,但发展方向还是平稳向前的,因此说自己不会MySQL,还真有些不好意思了。
我只能算是MySQL中的小小白,研究生毕设的时候,装过windows版本的MySQL,就像对Oracle一样,仅仅是用,但对于一门技术来说,没有深入理解运行的原理,就不算真正了解这门技术。又有一种说法,就是相似的技术是相通的,原理性质的知识,有些是可以复用学习和了解。
碰巧有一个旁支的项目,要用MySQL,借此机会逼着自己学习MySQL。为了学习MySQL,首先就要有一个MySQL的环境,第一步就是安装,这里说的安装,肯定不是Windows中“下一步”这种的安装。我们的环境是Linux 6.5,需要安装MySQL 5.7.19版本。
P.S. 以下只是我自己学习的体会,有不准确的地方,还请各位牛人指出来。
MySQL的安装主要有两种方式,一种就是利用源码,自行编译安装,这是开源相对于商业闭源软件,独特的风景线。另一种方式,就是使用二进制文件,又分为两种,一种是不针对特定平台,使用.tar.gz压缩文件来安装,一种是针对特定平台,使用.rpm文件安装。
官网中相应地有以上三种方式,对应的下载链接,其中源码安装,对应"Source Code",.tar.gz对应"Linux-Generic",.rpm则对应于"Red Hat Enterprise Linux/Oracle Linux",如下图所示,
这次我用rpm和源码,这两种方式进行安装。
1. rpm安装
首先需要选择对应平台,位数,下载完整版RPM Bundle,
解压缩安装包,
tar xvf mysql-5.7.19-1.el6.x86_64.rpm-bundle.tar
基本安装,只需要几个rpm包,但有顺序要求,
rpm -ivh mysql-community-common-5.7.19-1.el6.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.19-1.el6.x86_64.rpm(依赖于common)
rpm -ivh mysql-community-client-5.7.19-1.el6.x86_64.rpm(依赖于libs)
rpm -ivh mysql-community-server-5.7.19-1.el6.x86_64.rpm(依赖于client、common)
安装完成后,会自动创建mysql用户和组,
[root@RAC2 rb]# id mysql
uid=27(mysql) gid=11002(mysql) groups=11002(mysql)
启动mysqld服务,记住不是mysql,默认3306端口打开,
[root@RAC2 mysql]# service mysqld start
Initializing MySQL database: test
[ OK ]
Starting mysqld: [ OK ]
[root@RAC2 mysql]# service mysqld status
mysqld (pid 30775) is running...
[root@DEPRAC2 mysql]# netstat -anp | grep 3306
tcp 0 0 :::3306 :::* LISTEN 30775/mysqld
登录提示错误,
[root@RAC2 mysql]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
参考了周老师的书,这是因为MySQL5.7新版本中,调整了安全策略,默认情况下root不能没有密码,数据库启动的时候,会自动为root随机生成一个密码,从error日志中可以看见,注意这由于采用了默认安装,因此error日志默认路径是/var/log,/etc/my.cnf是默认的配置文件路径,
[root@RAC2 mysql]# cat /var/log/mysqld.log | more
2017-08-24T06:24:04.897029Z 1 [Note] A temporary password is generated for root@localhost: MDjfrXl;E9hp
临时强密码为“MDjfrXl;E9hp”。
再次登录,
[root@RAC2 mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.19
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>
执行报错,提示需要改密码才能继续,
mysql> show variables like "%sock%";
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
一般的密码策略,会认为是不符合规范,
mysql> alter user 'root'@'localhost' identified by 'mysql';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> alter user 'root'@'localhost' identified by 'Mysql@admin';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> alter user 'root'@'localhost' identified by 'My@38@sql';
Query OK, 0 rows affected (0.01 sec)
此时,就可以执行任何指令了,
mysql> select user from mysql.user;
+---------------+
| user |
+---------------+
| mysql.session |
| mysql.sys |
| root |
+---------------+
至此,完成了rpm安装。
若需要卸载,可以不按顺序执行以下指令,
rpm -e mysql-community-common-5.7.19-1.el6.x86_64 --nodeps
rpm -e mysql-community-client-5.7.19-1.el6.x86_64 --nodeps
rpm -e mysql-community-server-5.7.19-1.el6.x86_64 --nodeps
rpm -e mysql-community-libs-5.7.19-1.el6.x86_64 --nodeps
此时,/etc/my.cnf配置文件、mysql用户和组,都会被删除。
2. 源码安装
整个过程,对于第一次接触的人来说,还是比较曲折,尤其是中间碰见了各种坑,需要一一化解。
Source Code中一种是针对版本的src.rpm包,一种是tar.gz包,前者包含的组件比较完整,我们下载这包,
由于这种安装,不会像rpm自动完成配置,因此需要先创建用户,
[root@RAC2 mysql-5.7.19]# groupadd mysql
[root@RAC2 mysql-5.7.19]# useradd -r -g mysql mysql
其中-r表示用户是系统用户,不可登录系统。
源码安装前,还需要装一些辅助包,
yum install cmake
yum install bison
yum install libaio-devel*
安装src.rpm,
[root@RAC2 src]# rpm -ivh mysql-community-5.7.19-1.el6.src.rpm
warning: mysql-community-5.7.19-1.el6.src.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
1:mysql-community ########################################### [100%]
一开始我不清楚,本地目录下,未看见有解压的文件夹,再看上面有warning,以为是因为有错,实际并非如此。
问题1:warning: mysql-community-5.7.19-1.el6.src.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
这个错误是因为yum安装了旧版本的GPG keys造成,有帖子做法是rpm跟着--force --nodeps参数,或者rpm --import /etc/pki/rpm-gpg/RPM*再来安装。
可实际操作后,发现错误依旧。
参考官方
https://dev.mysql.com/doc/refman/5.7/en/checking-gpg-signature.html
,拷贝以下内容,作为mysql_pubkey.asc,
执行gpg --import指令,
[root@RAC2 src]# gpg --import mysql_pubkey.asc
gpg: directory `/root/.gnupg' created
gpg: new configuration file `/root/.gnupg/gpg.conf' created
gpg: WARNING: options in `/root/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: keyring `/root/.gnupg/pubring.gpg' created
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key 5072E1F5: public key "MySQL Release Engineering <mysql-build@oss.oracle.com>" imported
gpg: Total number processed: 1
gpg: imported: 1
gpg: no ultimately trusted keys found
执行导出gpg --export
[root@RAC2 src]# gpg --export -a 5072e1f5 > 5072e1f5.asc
之所以这么做,官方解释是,
If you are using RPM 4.1 and it complains about
(GPG) NOT OK (MISSING KEYS: GPG#5072e1f5)
, even though you have imported the MySQL public build key into your own GPGkeyring, you need to import the key into the RPM keyringfirst. RPM 4.1 no longer uses your personal GPG keyring (or GPG itself). Rather, RPM maintains a separate keyring becauseit is a system-wide application and a user's GPG publickeyring is a user-specific file. To import the MySQL public key into the RPM keyring, first obtain the key, then use rpm --import to import the key.
RPM包有内置的GPG签名和MD5校验,用户和系统层级范围的问题,需要手工export、import操作。
如果之前未导入pubkey,则此时执行export会报错,没有需要导出的信息,
[root@RAC2 src]# gpg --export -a 5072e1f5 > 5072e1f5.asc
gpg: WARNING: nothing exported
执行导入,
[root@RAC2 src]# rpm --import 5072e1f5.asc
如果之前未导入pubkey,则此时执行import会报错,没有需要导入的信息,
[root@RAC2 src]# rpm --import 5072e1f5.asc
error: 5072e1f5.asc: import read failed(0).
此时执行,不会报错了,
[root@RAC2 src]# rpm --checksig mysql-community-5.7.19-1.el6.src.rpm
mysql-community-5.7.19-1.el6.src.rpm: (sha1) dsa sha1 md5 gpg OK
[root@RAC2 src]# rpm -ivh mysql-community-5.7.19-1.el6.src.rpm
1:mysql-community ########################################### [100%]
问题2:rpm ...src.rpm,到底安装在哪了?
实际存储在用户家目录中,此处使用root,则为/root目录,文件夹结构为,
rpmbuild
| SOURCE
| SPECS
SOURCE路径内容为,
[root@RAC2 SOURCES]# ls
boost_1_59_0.tar.bz2 filter-provides.sh filter-requires.sh mysql-5.1.72.tar.gz mysql-5.7.19.tar.gz
解压mysql-5.7.19.tar.gz,执行jar -jxvf boost_1_59_0.tar.bz2解压文件,放置mysql-5.7.19路径下,创建debug文件夹,用于存储安装的中间文件,
接下来就要出现各种坑了。
参考周老师的书,通过CMake生成编译环境。
[root@RAC2 debug]# cmake .. -DBUILD_CONFIG=mysql_release \
> -DINSTALL_LAYOUT=STANDLONE \
> -DCMAKE_BUILD_TYPE=RelWithDebInfo \
> -DENABLE_DTRACE=OFF \
> -DWITH_EMBEDDED_SERVER=OFF \
> -DWITH_INNODB_MEMCACHED=ON \
> -DWITH_SSL=bundled \
> -DWITH_ZLIB=system \
> -DWITH_PAM=ON \
> -DCMAKE_INSTALL_PREFIX=/var/mysql \
> -DINSTALL_PLUGINDIR="/var/mysql/lib/plugin" \
> -DDEFAULT_CHARSET=utf-8 \
> -DDEFAULT_COLLATION=utf8_general_ci \
> -DWITH_EDITLINE=bundled \
> -DFEATURE_SET=community \
> -DCOMPILATION_COMMENT="MySQL Server(GPL)" \
> -DWITH_DEBUG=OFF \
> -DWITH_BOOST=..
CMake Error at CMakeLists.txt:21 (CMAKE_MINIMUM_REQUIRED):
CMake 2.8.2 or higher is required. You are running version 2.6.4
提示错误,CMake版本需要2.8.2以上,当前仅为2.6.4。
下载高版本cmake,http://www.cmake.org/cmake/resources/software.html,解压缩cmake-2.8.12.2.tar.gz,依次执行,
./bootstrap
make
make install
报了一些错误,
[root@RAC2 cmake-2.8.12.2]# ./bootstrap
---------------------------------------------
CMake 2.8.12.2, Copyright 2000-2012 Kitware, Inc.
---------------------------------------------
Error when bootstrapping CMake:
Cannot find appropriate C compiler on this system.
Please specify one using environment variable CC.
See cmake_bootstrap.log for compilers attempted.
---------------------------------------------
Log of errors: /root/cmake-2.8.12.2/Bootstrap.cmk/cmake_bootstrap.log
---------------------------------------------
提示需要cc的编译环境,咱就安装,
[root@RAC2 cmake-2.8.12.2]# yum install gcc gcc-c++ make automake
执行完成,
[root@RAC2 cmake-2.8.12.2]# ./bootstrap
...
Curses libraries were not found. Curses GUI for CMake will not be built.
-- Looking for elf.h
-- Looking for elf.h - found
-- Looking for a Fortran compiler
-- Looking for a Fortran compiler - NOTFOUND
-- Found unsuitable Qt version "" from NOTFOUND
-- Performing Test run_pic_test
-- Performing Test run_pic_test - Success
-- Configuring done
-- Generating done
-- Build files have been written to: /root/cmake-2.8.12.2
---------------------------------------------
CMake has bootstrapped. Now run gmake.
再执行指令make和make install,此时可以看出,已经升级为最新版本了,
[root@DEPRAC2 cmake-2.8.12.2]# ls -l /usr/local/bin/cmake
-rwxr-xr-x 1 root root 10204177 Aug 24 16:51 /usr/local/bin/cmake
[root@DEPRAC2 mysql-5.7.19]# /usr/local/bin/cmake -version
cmake version 2.8.12.2
再次执行上述cmake,又出现了几个错误,一个是STANDLONE拼错了,应该是STANDALONE,
-- Packaging as: mysql-5.7.19-Linux-x86_64
CMake Error at cmake/install_layout.cmake:107 (MESSAGE):
Invalid INSTALL_LAYOUT parameter:STANDLONE. Choose between
RPM;DEB;SVR4;FREEBSD;GLIBC;OSX;TARGZ;SLES;STANDALONE
Call Stack (most recent call first):
CMakeLists.txt:213 (INCLUDE)
一个是字符集utf-8,应该写为utf8(这个错误是安装完成后,初始化数据库时才报的),需要改正,重新执行cmake指令。
另一个是,
-- Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH)
CMake Error at cmake/readline.cmake:64 (MESSAGE):
Curses library not found. Please install appropriate package,
remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):
cmake/readline.cmake:107 (FIND_CURSES)
cmake/readline.cmake:197 (MYSQL_USE_BUNDLED_EDITLINE)
CMakeLists.txt:519 (MYSQL_CHECK_EDITLINE)
删除原始CMakeCache.txt,安装ncurses-devel,
mv CMakeCache.txt CMakeCache.txt.k
yum install ncurses-devel*
再次执行,出现Configuring done和Generating done,安装完成,
-- CMAKE_C_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
WITH_PAM
-- Build files have been written to: /root/rpmbuild/SOURCES/mysql-5.7.19/debug
接下来执行make -j 30,启动30个线程,进行编译,否则编译慢,-j含义,
-j [jobs], --jobs[=jobs]
Specifies the number of jobs (commands) to run simultaneously. If there is more than one -j option, the last one is effective. If the -j option is given without an argument, make will not limit the number of jobs that can run simultaneously.
碰见了另一个坑,就是磁盘空间满了,
Linking CXX static library libsql.a
/usr/bin/ar: libsql.a: No space left on device
make[2]: *** [sql/libsql.a] Error 1
make[1]: *** [sql/CMakeFiles/sql.dir/all] Error 2
因为默认放在了/root下,将其mv至空闲磁盘中,再次执行,提示100%完成,
[100%] Building CXX object sql/CMakeFiles/udf_example.dir/udf_example.cc.o
Linking CXX shared module udf_example.so
[100%] Built target udf_example
[100%] Built target pfs_connect_attr-t
执行make install,
-- Installing: /var/mysql/mysql-test/lib/My/SafeProcess/Base.pm
-- Installing: /var/mysql/support-files/mysqld_multi.server
-- Installing: /var/mysql/support-files/mysql-log-rotate
-- Installing: /var/mysql/support-files/magic
-- Installing: /var/mysql/share/aclocal/mysql.m4
-- Installing: /var/mysql/support-files/mysql.server
数据库默认安装于/var/mysql,
编辑配置文件my.cnf,
[mysqld]
port=3306
datadir=/DATA/mysql/data_3306
log_error=/DATA/mysql/data_3306/error.log
basedir=/DATA/mysql
创建数据库,指定使用mysql用户,
[root@RAC2 mysql]# /var/mysql/bin/mysqld --defaults-file=/DATA/mysql/my.cnf --initialize --user=mysql
此时可以看出,/DATA/mysql/data_3306目录属主是mysql,
启动数据库,注意不是用mysqld,而是mysql,
[root@RAC2 mysql]# /var/mysql/bin/mysql --defaults-file=/DATA/mysql/my.cnf --user=mysql
查看进程、端口,
[root@RAC2 data_3306]# ps -ef | grep mysql
mysql 26428 23453 0 18:56 pts/2 00:00:00 /var/mysql/bin/mysqld --defaults-file=/DATA/mysql/my.cnf --user=mysql
root 26527 29090 0 19:08 pts/1 00:00:00 grep mysql
[root@DEPRAC2 data_3306]# netstat -anp | grep 3306
tcp 0 0 :::3306 :::* LISTEN 26428/mysqld
接下来和rpm安装相同,从error日志中找出临时密码,登录改密码,完成数据库初始化工作。注意此时error日志,存储于配置文件定义的log_error=/DATA/mysql/data_3306/error.log了。
总结:
1. rpm方式的安装,类似于Windows下安装,确实比较简单,容易上手。
2. 源码安装,过程比较繁琐,而且碰见不少坑,但这种方式,可以接触更多,学到不少东西,还是值得推荐。
当然,我这儿只是学了些皮毛,MySQL还是比较精深,需要学习的地方还有不少,一步一步来吧,有可能机会就在眼前了。
参考文献:
1. 《MySQL运维内参》
2. 《MySQL官方参考》
https://dev.mysql.com/doc/refman/5.7/en/checking-rpm-signature.html
https://dev.mysql.com/doc/refman/5.7/en/linux-installation-rpm.html
https://dev.mysql.com/doc/refman/5.7/en/installing-source-distribution.html
如果您觉得此篇文章对您有帮助,欢迎关注微信公众号:bisal的个人杂货铺,您的支持是对我最大的鼓励!共同学习,共同进步:)