Mongodb 部署安装

Summary: Author: 张亚飞 | 阅读时间: 4 minute read | Published: 2016-08-08
Filed under Categories: DevOpsTags: Linux, Server, Software, DevOps,

安装 MySQL

  • 安装编译源码需要的包

CentOS

sudo yum -y install gcc gcc-c++ openssl openssl-devel ncurses ncurses-devel cmake bison
  1. CMake 官网
  2. GCC: A working ANSI C++ compiler. GCC 4.2.1 or later 官网
  3. bison,2.1 or newer 官网
  4. m4 : 官网
  5. tar : 官网
  • 手动下载 boost 库
mkdir -p /usr/local/boost
cd /usr/local/boost
wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
tar zxvf boost_1_59_0.tar.gz
  • 第一步,添加 mysql 用户
groupadd mysql
useradd -r -g mysql mysql
  • 下载最新的源码包

MySQL5.7.22

sudo wget https://github.com/mysql/mysql-server/archive/mysql-5.7.22.tar.gz
tar zxvf mysql-5.7.22.tar.gz
cd mysql-5.7.22/

MySQL自5.5版本以后,就开始使用 CMake 编译工具了,因此,你在安装源文件中找不到configure文件是正常的.很多人下到了新版的MySQL,因为找不到configure文件,不知道该怎么继续下去. 有没有一篇可供参考的文章呢?其实在 http://forge.mysql.com 网站上有一篇文章,专门介绍了如何用CMake工具进行新版MySQL的编译安装.

  • 最后的 cmake 编译配置选项
mkdir build
cd build
sudo cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
    -DMYSQL_DATADIR=/data/home/coam/data/mysql \
    -DSYSCONFDIR=/etc/mysql \
    -DMYSQL_TCP_PORT=3306 \
    -DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
    -DINSTALL_LIBDIR=/usr/lib/mysql \
    -DINSTALL_PLUGINDIR=/usr/lib/mysql/plugin \
    -DWITH_SYSTEMD=1 \
    -DENABLE_DOWNLOADS=1 \
    -DDOWNLOAD_BOOST=1 \
    -DDOWNLOAD_BOOST=1 \
    -DWITH_BOOST=/usr/local/boost/boost_1_59_0 \
    -DWITH_EMBEDDED_SERVER=1 \
    -DENABLED_LOCAL_INFILE=1 \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_EXTRA_CHARSETS=complex \
    -DWITH_PARTITION_STORAGE_ENGINE=1 \
    -DWITH_FEDERATED_STORAGE_ENGINE=1 \
    -DWITH_MYISAM_STORAGE_ENGINE=1 \
    -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
    -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
    -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci \
    -DEXTRA_CHARSETS=all \
    -DWITH_SSL=system \
    -DWITH_READLINE=1 \
    -DENABLE_PROFILING=0

-DWITH_EXTRA_CHARSETS:STRING=utf8,gbk
由于 -DWITH_DEBUG 默认就是 OFF 状态,所以也无需特别指定此参数.

说明:把 mysql 安装到 ~/usr/local/mysql 路径下

  • 执行 make 编译
coam@coam:/opt/data/mysql-5.7.12$ sudo make

编译过程在云服务器上花费了整整2个小时!!如果这个步骤出现问题,找出问题原因并解决后先make clean,再重新make

注:阿里云低配[1Core-1G]服务器编译到[36%]的时候,总是不通过,是因为内存不足,需要分配swap交换内存后并调优

cat /proc/sys/vm/swappiness
// [swappiness:0~100] 临时性的修改 60
sudo sysctl vm.swappiness=60

注:上述 cmake 编译出现如下错误:

CMake Error at cmake/boost.cmake:81 (MESSAGE):
  You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>  =============> 需要boost支持,这一点跟5.6版本的不一样

  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:451 (INCLUDE)


-- Configuring incomplete, errors occurred!
See also "/opt/data/mysql-5.7.12/CMakeFiles/CMakeOutput.log".

make install 后会自动创建如下目录文件

  • /usr/local/mysql
  • /usr/lib/mysql
  • /usr/lib/mysql/plugin

第七步,创建 MySQL 的数据目录

cd /data/home/data
mkdir -p /etc/mysql/conf.d
chown -R mysql.mysql /etc/mysql

配置可直接使用已经在生产环境的 /etc/mysql/my.cnf 替换

  • MySQL初始化安装 - /data/home/data/mysql 需要清空
coam@coam:/usr/local/mysql/support-files$ sudo mkdir /data/home/data/mysql
coam@coam:/usr/local/mysql/support-files$ sudo chown mysql.mysql /data/home/data/mysql
coam@coam:/usr/local/mysql/support-files$ sudo /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/home/data/mysql --basedir=/usr/local/mysql --socket=/var/run/mysqld/mysqld.sock
2016-05-18T00:49:36.484135Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-05-18T00:49:36.484233Z 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.
2016-05-18T00:49:36.484237Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2016-05-18T00:49:37.099361Z 0 [Warning] InnoDB: New log files created, LSN=45790
2016-05-18T00:49:37.177877Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-05-18T00:49:37.239010Z 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: 65571e41-1c92-11e6-813b-5600001238f3.
2016-05-18T00:49:37.240470Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2016-05-18T00:49:37.737636Z 0 [Warning] CA certificate ca.pem is self signed.
2016-05-18T00:49:37.935969Z 1 [Note] A temporary password is generated for root@localhost: N+?GkPFyA9iW
  • 添加 MySQL 服务

/lib/systemd/system/mysqld.service

* [Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

* [Install]
WantedBy=multi-user.target

* [Service]
User=mysql
Group=mysql

Type=forking

PIDFile=/var/run/mysqld/mysqld.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Execute pre and post scripts as root
PermissionsStartOnly=true

# Needed to create system tables
ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd

# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 5000

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false
  • 启动错误检索 mysql 错误日志查找问题 /data/home/data/mysql/coam.err

    Can’t create/write to file ‘/var/run/mysqld/mysqld.pid [ERROR] Could not create unix socket lock file /var/run/mysqld/mysqld.sock.lock.

  • 手动添加以下目录

mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld

但由于 mysql 用户没有直接创建 /var/run/mysqld 的权限,而 /var/run/* 下的文件在服务器重启后会删除 最后利用 tmpfiles.d 在系统重启时添加临时文件夹 /var/run/mysqld/

/etc/tmpfiles.d/mysql.conf

# systemd tmpfile settings for mysql
# See tmpfiles.d(5) for details

d /var/run/mysqld 0755 mysql mysql -
  • 刚安装的 Mysql 使用命令行登陆,提示如下错误
coam@coam:~$ mysql -u root -p
MySQL -Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

因为默认 [mysql] 使用 ‘/var/run/mysqld/mysqld.sock’ 但是配置文件仅指定了[mysqld]而没有指定[mysql] 两者 socket 配置应为同一个 mysql.sock 文件 ,于是在 /etc/mysql/my.cnf 添加如下内容重启 Mysql 服务器即可

* [client]

port = 3306
host = 127.0.0.1
socket = /var/run/mysqld/mysqld.sock
  • 出现 The server quit without updating PID file (/data/home/coam/data/mysql/coam.pid) 错误
coam@coam:/usr/local/mysql/support-files$ sudo service mysql restart
 * MySQL server PID file could not be found!
Starting MySQL
. * The server quit without updating PID file (/data/home/coam/data/mysql/coam.pid).
  1. 由于没有进行 sudo /usr/local/mysql/bin/mysqld –initialize… 初始化命令,所以直接启动 mysql 出现此问题 - 清空 dataDir:/data/home/data/mysql 下所有文件

  2. 如果使用其它服务器拷贝的旧的 mysql 数据文件到 /data/home/data/mysql ,则需要使用如下命令修改此目录下的所有用户属主为 mysql

sudo chown mysql.mysql /data/home/data/mysql
sudo chown -R mysql.mysql /data/home/data/mysql/*
  1. /var/log/mysql 日志文件目录不存在

/etc/mysql/my.cnf 中配置了 log_error 错误日志文件 /var/log/mysql 没有自动创建

log_error = /var/log/mysql/error.log

使用 ps -ef 查看默认log_error 目录为 –log-error=/data/home/data/mysql/coam.err

Mon May 16 17:59:13 coam@coam:/var/log$ sudo ps -ef | grep mysql
root     15391     1  0 17:59 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/home/data/mysq  --pid-file=/data/home/data/mysql/coam.pid
mysql    15623 15391  6 17:59 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/home/data/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/data/home/data/mysql/coam.err --pid-file=/data/home/data/mysql/coam.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
coam   15686   581  0 17:59 pts/0    00:00:00 grep --color=auto mysql

解决方案,手动创建 /var/log/mysql 目录 和 /var/log/mysql/error.log 文件;或者注释掉 /etc/mysql/my.cnf 中的 log_error 日志配置

Comments

Cor-Ethan, the beverage → www.iirii.com