MySQL是一个开源的数据库,在互联网行业应用的很广泛,下面来记录一下从源码编译安装的步骤,当然,MySQL也有其他安装方式,比如,使用yum下载安装rpm包,或者二进制方式安装,如果机器比较多,可以自己搭建yum源,然后定制rpm包,这样更方便于使用ssh多机自动安装
这里源码安装的mysql版本为5.7.11,使用cmake编译安装,所需安装包的下载链接:cmake and mysql
下面开始记录安装步骤吧
//解压 mysql 压缩包
//yum 安装 cmake 的 rpm 包
[root@server1 ~]# ls
cmake-2.8.12.2-4.el6.x86_64.rpm mysql-boost-5.7.11.tar.gz
[root@server1 ~]# tar zxf mysql-boost-5.7.11.tar.gz
[root@server1 ~]# ls
cmake-2.8.12.2-4.el6.x86_64.rpm mysql-5.7.11 mysql-boost-5.7.11.tar.gz
[root@server1 ~]# yum install -y cmake-2.8.12.2-4.el6.x86_64.rpm
//进入到mysql解压后的目录下,进行cmake,参数祥见cmake --help
//添加所需要的内容,此处添加的内容代表意思如下:
/*
-DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql #安装目录
-DMYSQL_DATADIR=/usr/local/lnmp/mysql/data #数据库存放目录
-DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock #Unix socket 文件路径
-DWITH_MYISAM_STORAGE_ENGINE=1 #安装myisam存储引擎 -DWITH_INNOBASE_STORAGE_ENGINE=1 #安装innodb存储引擎
-DDEFAULT_CHARSET=utf8 #使用utf8字符
-DDEFAULT_COLLATION=utf8_general_ci #校验字符
-DEXTRA_CHARSETS=all #安装所有扩展字符集
*/
[root@server1 ~]# cd mysql-5.7.11/
[root@server1 mysql-5.7.11]# ls
boost config.h.cmake extra libevent mysys scripts testclients
BUILD configure.cmake include libmysql mysys_ssl sql unittest
client COPYING INSTALL libmysqld packaging sql-common VERSION
cmake dbug INSTALL-SOURCE libservices plugin storage vio
CMakeLists.txt Docs libbinlogevents man README strings win
cmd-line-utils Doxyfile-perfschema libbinlogstandalone mysql-test regex support-files zlib
[root@server1 mysql-5.7.11]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql -DMYSQL_DATADIR=/usr/local/lnmp/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all
注意:cmake过程中出现的Error 必须解决,Warning 可以忽略,最好是解决
这里由于我是直接用的新的虚拟机搭建LNMP架构,我将我遇到的Error列出如下(附解决方法):
[root@server1 mysql-5.7.11]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql -DMYSQL_DATADIR=/usr/local/lnmp/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=boost/boost_1_59_0/
cmake 完成后
[root@server1 mysql-5.7.11]# make
[root@server1 mysql-5.7.11]# make install
[root@server1 mysql-5.7.11]# cd
[root@server1 ~]# ls /usr/local/lnmp/mysql/support-files/
magic my-default.cnf mysqld_multi.server mysql-log-rotate mysql.server
[root@server1 ~]# cp /usr/local/lnmp/mysql/support-files/my-default.cnf /etc/my.cnf
cp: overwrite `/etc/my.cnf'? y
[root@server1 ~]# vim /etc/my.cnf
##修改内容如下(也可不做任何修改)
18 basedir = /usr/local/lnmp/mysql
19 datadir = /usr/local/lnmp/mysql/data
20 port = 3306
21 # server_id = .....
22 socket = /usr/local/lnmp/mysql/data/mysql.sock
[root@server1 ~]# cp /usr/local/lnmp/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@server1 ~]# ll /usr/local/lnmp/mysql
total 56
drwxr-xr-x 2 root root 4096 Aug 5 10:17 bin
-rw-r--r-- 1 root root 17987 Feb 2 2016 COPYING
drwxr-xr-x 2 root root 4096 Aug 5 10:16 docs
drwxr-xr-x 3 root root 4096 Aug 5 10:16 include
drwxr-xr-x 4 root root 4096 Aug 5 10:17 lib
drwxr-xr-x 4 root root 4096 Aug 5 10:16 man
drwxr-xr-x 10 root root 4096 Aug 5 10:17 mysql-test
-rw-r--r-- 1 root root 2478 Feb 2 2016 README
drwxr-xr-x 28 root root 4096 Aug 5 10:17 share
drwxr-xr-x 2 root root 4096 Aug 5 10:17 support-files
##添加mysql组、创建mysql用户
[root@server1 mysql]# groupadd -g 27 mysql
[root@server1 mysql]# useradd -u 27 -g 27 -M -d /usr/local/lnmp/mysql/data -s /sbin/nologin mysql
[root@server1 ~]# vim ~/.bash_profile
10 PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin
[root@server1 ~]# source ~/.bash_profile
##~/.bash_profile:每个用户都可使用该文件输入专用于自己使用的shell信息,
##当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量执行用户的.bashrc文件
[root@server1 ~]# cd /usr/local/lnmp/mysql ##进行mysql的初始化
[root@server1 mysql]# mysqld --initialize --user=mysql
2018-08-05T03:08:09.684310Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-05T03:08:09.684400Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2018-08-05T03:08:09.684405Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2018-08-05T03:08:11.964546Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-05T03:08:12.422641Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-05T03:08:12.590273Z 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: c9dcc6d4-985c-11e8-adb3-52540004503d.
2018-08-05T03:08:12.673131Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-08-05T03:08:12.687518Z 1 [Note] A temporary password is generated for root@localhost: XFdyFIO2A*q%
## XFdyFIO2A*q% 最后的这个字符串是初始化后mysql的初始密码
[root@server1 mysql]# ls ##初始化后,生成data目录
bin COPYING data docs include lib man mysql-test README share support-files
测试脚本启动
[root@server1 mysql]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
[root@server1 mysql]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!
[root@server1 mysql]#
[root@server1 mysql]# chkconfig --list mysqld ##查看mysql服务是否在开机自启项中
service mysqld supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add mysqld')
[root@server1 mysql]# ll data/
drwxr-x--- 5 mysql mysql 4096 Aug 5 11:10 data
[root@server1 mysql]# chgrp root data/ -R ##修改目录data的所在组
[root@server1 mysql]# ll data/
drwxr-x--- 5 mysql root 4096 Aug 5 11:10 data
[root@server1 mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@server1 mysql]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: ##输入刚才初始化的密码(复制粘贴)
The existing password for the user account root has expired. Please set a new password.
##修改密码
New password:
Re-enter new password:
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No:
##是否使用VALIDATE PASSWORD PLUGIN,这个看自己
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
##是否删除匿名用户,生产环境建议删除
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
##是否禁止root远程登录
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
##是否删除test数据库
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
##是否重新加载权限表
Success.
All done!
[root@server1 mysql]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.11 Source distribution
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> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> QUIT
Bye
[root@server1 mysql]#