了解LAMP和安装mysql服务

LAMP=Linux+Apache+Mysql+PHP四种组成的一个系统 Apache和php必须在一台服务器上才可使用,mysql可以通过网络方式来提供服务 Apache不能直接存储数据到mysql,必须通过php Module的解析模块来往数据库中存储数据

安装mysql服务

安装mysql的先前操作

创建mysql运行的用户和组 groupadd mysql useradd -M -s /sbin/nologin -g mysql mysql

在官网或镜像站下载mysql的编译包

安装前首先安装运行编译的环境 yum install gcc cpp glibc glibc-devel gcc-c++ ncurses-devel 下载cmake编译包进行编译安装,附件中为3.12.0版本;官网地址:www.cmake.org ./configure;make;make install 安装cmake编译,所有安装路径默认即可

编译mysql报错boost不存在

mysql 5.7版本后需要安装一个boost包,并把包放在编译指定的目录下/usr/local/boost/

CMake Error at cmake/boost.cmake:81 (MESSAGE):
  You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>
 
  This CMake script will look for boost in <directory>.  If it is not there,
  it will download and unpack it (in that directory) for you.
 
  If you are inside a firewall, you may need to use an http proxy:
 
  export http_proxy=http://example.com:80
 
Call Stack (most recent call first):
  cmake/boost.cmake:238 (COULD_NOT_FIND_BOOST)
  CMakeLists.txt:455 (INCLUDE)
 
 
-- Configuring incomplete, errors occurred!

报错信息大概如上;

报错信息解决方案

先到官网下载的是boost_1_64_0.tar.gz,然后把压缩包放到了/usr/local/boost目录下,再次cmake但是报错继续。经过百度一番后,说不需要解压缩只需要把压缩包放到指定目录就可以了 把boost_1_64_0.tar.gz放到目录后,再次cmake后发现识别不到,他会再次下载另外一个版本,结果下载超时。 然后boost官网下载了需要的版本boost_1_59_0.tar.gz

boost的官网历史版本下载地址:http://www.boost.org/users/history/

然后把下载好的tar包放到/usr/local/boost目录下然后继续cmake就可以了

对mysql的编译参数参考和编译结果:

[root@localhost mysql-5.7.22]# cmake . -DCMALE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/var/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH-SYSTEMD=1 -DWITH_BOOST=/usr/local/boost

-----------------------省略过程----------------------
-- CMAKE_C_LINK_FLAGS: 
-- CMAKE_CXX_LINK_FLAGS: 
-- 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
-- Build files have been written to: /usr/local/src/mysql-5.7.22
[root@localhost mysql-5.7.22]# echo $?
0

如果结果跟我一样无报错的话,继续make编译,如有报错请看报错信息,大部分是没有安装某些软件包造成的,请将错误信息粘贴至搜索引擎查询


[root@localhost mysql-5.7.22]# make
Scanning dependencies of target abi_check
[ 0%] Built target abi_check
Scanning dependencies of target INFO_SRC
[ 0%] Built target INFO_SRC
Scanning dependencies of target INFO_BIN
[ 0%] Built target INFO_BIN
Scanning dependencies of target zlib
----------------省略过程------------------------
[ 99%] Building C object libmysqld/examples/CMakeFiles/mysql_client_test_embedded.dir/__/__/testclients/mysql_client_test.c.o
[ 99%] Linking CXX executable mysql_client_test_embedded
[ 99%] Built target mysql_client_test_embedded
Scanning dependencies of target my_safe_process
[100%] Building CXX object mysql-test/lib/My/SafeProcess/CMakeFiles/my_safe_process.dir/safe_process.cc.o
[100%] Linking CXX executable my_safe_process
[100%] Built target my_safe_process
[root@localhost mysql-5.7.22]# aecho $?
0

安装无报错,完成最后安装make install,编译结果成功

-----------------------过程省略---------------------
-- Installing: /usr/local/mysql/support-files/mysqld_multi.server
-- Installing: /usr/local/mysql/support-files/mysql-log-rotate
-- Installing: /usr/local/mysql/support-files/magic
-- Installing: /usr/local/mysql/share/aclocal/mysql.m4
-- Installing: /usr/local/mysql/support-files/mysql.server
[root@localhost mysql-5.7.22]# echo $?
0
基本的安装完成,接下来就是配置mysql的启动和配置项

递归指定mysql所属用户和所属组的权限

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

数据库的初始化

编译好的数据库进行初始化,其中mysql初始化信息中包含一个随机生成的密码

[root@localhost mysql-5.7.22]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2018-07-28T09:08:42.088874Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-07-28T09:08:42.590615Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-07-28T09:08:42.861362Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-07-28T09:08:42.955937Z 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: d342a57c-9245-11e8-bf5e-080027a7f9c2.
2018-07-28T09:08:42.965634Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-07-28T09:08:42.966858Z 1 [Note] A temporary password is generated for root@localhost: tHuQg=%M!12j  --这里是初始生成的密码

my.cnf和/etc/init.d/mysqld文件

拷贝服务启动文件,并修改my.cnf配置文件。指定mysql安装路径和存储数据的路径及添加系统服务

[root@localhost ]# cp /usr/local/src/mysql-5.7.22/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ]# vim /etc/init.d/mysqld 
[root@localhost ]# chmod +x /etc/init.d/mysqld 
[root@localhost ]# ls -l /etc/init.d/mysqld 
-rwxr-xr-x 1 root root 10609 7月 28 17:21 /etc/init.d/mysqld
[root@localhost ]# chkconfig --add mysqld
[root@localhost ]# chkconfig --list 

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 
      如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
      欲查看对特定 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld 0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole 0:关	1:关	2:关	3:关	4:关	5:关	6:关
network 0:关	1:关	2:开	3:开	4:开	5:开	6:关

编辑my.cnf的mysql配置文件

[root@localhost ]# less /etc/my.cnf

[mysqld]
basedir = /usr/local/mysql/  
datadir = /usr/local/mysql/data  
port = 3306  
character-set-server = utf8  
explicit_defaults_for_timestamp = true  
# socket = /var/run/mysqld/mysqld.sock 
# 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

mysql的启动过程

添加完成跟随系统启动后,启动mysql服务:

[root@localhost ]# /etc/init.d/mysqld start
Starting MySQL.2018-07-28T09:23:23.822700Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
 ERROR! The server quit without updating PID file (/usr/local/mysql/data/localhost.pid).

根据报错提示创建了mysql的日志文件,然后再尝试启动

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

再次尝试启动mysql服务,并启动成功

[root@localhost support-files]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! 
[root@localhost support-files]# ps -aux |grep mysql
root 21812 0.0 0.1 113312 1636 pts/0 S 17:28 0:00 /bin/sh /usr/local/mysql//bin/mysqld_safe --datadir=/usr/local/mysq/data --pid-file=/usr/local/mysql/data/localhost.pid
mysql 22000 1.7 16.3 1112372 166604 pts/0 Sl 17:28 0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql//lib/plugin --user=mysql --log-error=/var/log/mariadb/mariadb.log --pid-file=/usr/local/mysql/data/localhost.pid --port=3306
root 22030 0.0 0.0 112724 976 pts/0 R+ 17:28 0:00 grep --color=auto mysql

登入mysql,修改默认生成的密码

mysql在初始化的时候生成了一个随机高强度的密码,这个密码非我们自己设定的,接下来就是把默认密码修改成一个符合规定的密码,这里修改密码为pwd@123 mysql5.7和5.5及5.6不同,成功启动服务时就会携带一个随机密码,必须通过其他方式修改默认的密码,以方便以后来使用

[root@localhost ~]# /usr/local/mysql/bin/mysqladmin -u root -h localhost passwrod 'NOWpassword' -p
Enter password: 
mysqladmin: connect to server at 'localhost' failed
error: 'Your password has expired. To log in you must change it using a client that supports expired passwords.'

这种修改密码提示密码过期,需要在一个客户端上进行密码修改

以下是mysql密码修改的方法:

[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.22

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> alter user 'root'@'localhost' identified by 'pwd@123';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
官方对mysql5.7默认密码的解释

为了提高安全性 mysql5.7中user表的password字段已被取消,取而代之的事 authentication_string 字段,当然我们更改用户密码也不可以用原来的修改user表来实现了 网络上对mysql5.7密码修改的方法,适用于mysql5.7

方法一:
mysql> alter user 'root'@'localhost' identified by 'pwd@123';
方法二:
mysql> set password for 'root'@'localhost'=password('pwd@123');
修改完成后刷新数据库表内容
mysql> flush privileges;
mysql在没有启动文件的情况下启动服务
[root@localhost mysql]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my,cnf --user=mysql --detadir=/usr/local/mysql/data/mysql &
[1] 22873
[root@localhost mysql]# Logging to '/usr/local/mysql/data/localhost.err'.
2018-07-28T10:06:55.233375Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
2018-07-28T10:06:55.242274Z mysqld_safe mysqld from pid file /usr/local/mysql/data/localhost.pid ended

[1]+ 完成 /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my,cnf --user=mysql --detadir=/usr/local/mysql/data/mysql
[root@localhost mysql]# ps -aux |grep mysql