共计 2759 个字符,预计需要花费 7 分钟才能阅读完成。
MySQL 版本:5.7.11
源码编译安装并使用 systemd 管理
由于种种原因,现住主流的 linux 发行版默认的 mysql 实现都采取了 MariaDB
Archlinux:
Note: MariaDB is now officially Arch Linux’s default implementation of MySQL. It is recommended for all users to upgrade to MariaDB.
Oracle MySQL was dropped to the AUR. See the announcement.
大致安装过程
# 下载源代码(源码有两个版本,一个是附带 boost 的,一个是不附带 boost 的,建议下载前者)
$ wget http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.11.tar.gz
# 此时目录
$ pwd
/usr/local
# 增加用户组和用户
$ groupadd mysql
$ useradd -r -g mysql -s /bin/false mysql
# 解压缩
$ tar -zxvf mysql-boost-5.7.11.tar.gz
# 进入目录
$ cd mysql-boost-5.7.11
$ mkdir build
$ cd build
# configure the distribution
# 此处仅修改字符编码,WITH_BOOST 是知道 boost 目录(下载包附带的),WITH_SYSTEMD 是安装 systemd 支持脚本
# -DWITH_SYSTEMD=bool
# Whether to enable installation of systemd support files. By default, this option is disabled. When enabled,
# systemd support files are installed, and scripts such as mysqld_safe and the System V initialization script
# are not installed. On platforms where systemd is not available, enabling WITH_SYSTEMD results in an error
# from CMake.
$ cmake .. -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_BOOST=/usr/local/mysql-5.7.11/boost -DWITH_SYSTEMD=ON
# 编译安装
$ make -j 4 && make install
# 进入目录
$ cd /usr/local/mysql
# 更改权限
$ chown -R mysql .
$ chgrp -R mysql .
# 初始化(此处会随机生成一个 root 用户的密码,要记录)
$ bin/mysqld –initialize –user=mysql
# SSH 连接
$ bin/mysql_ssl_rsa_setup
$ chown -R root .
$ chown -R mysql data
# 复制当前目录下的 mysqld.service 到 /usr/lib/systemd/system
$ cp ./mysqld.service /usr/lib/systemd/system/
# systemd 启动 mysql
$ systemctl start mysqld
Job for mysqld.service failed because the control process exited with error code. See “systemctl status mysqld.service” and “journalctl -xe” for details.
# 查看错误原因(自己挑重点看,大致原因是找不到 /var/run/mysqld 这个目录)
$ journalctl -xe
Apr 11 15:08:07 localhost mysqld[15304]: 2016-04-11T07:08:07.435947Z 0 [ERROR] /usr/local/mysql/bin/mysqld: Can’t create/write to file ‘/var/run/mysqld/mysqld.pid’ (Errcode: 2 – No such file or directo
Apr 11 15:08:07 localhost mysqld[15304]: 2016-04-11T07:08:07.435959Z 0 [ERROR] Can’t start server: can’t create PID file: No such file or directory
# 新建一个
$ mkdir /var/run/mysqld
# 更改权限
$ chown -R mysql /var/run/mysqld
# 启动和开机启动
$ systemctl start mysqld
$ systemctl enable mysqld
# 使用 mysql(密码为 mysql 初始化时自动生成的)
$ ./bin/mysql -uroot -p
相关链接
MySQL 源码安装文档:
http://dev.mysql.com/doc/refman/5.7/en/installing-source-distribution.html
MySQL 文档 cmake 选项:
http://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html
附
# 第一次进入 mysql 是要求更改密码的
mysql> ALTER USER USER() IDENTIFIED BY ‘123456’;
# 授权 root 用户,任意主机远程连接
mysql> GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’ IDENTIFIED BY ‘123456’ WITH GRANT OPTION;
# 建立链接,方便使用(可以修改 profile 文件的 PATH 环境变量)
$ ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
# mysqld.pid 文件所在目录,/var/run/mysqld 在每次系统重启的时候都会自动删除,导致每次重启系统,自动启动失败
# 方案:每次系统启动的时候,自动建立文件夹
# 修改~/.bash_profile 文件,添加下面内容
# MYSQLD_DIR=/var/run/mysqld
# if [! -d “$MYSQLD_DIR”]; then
# mkdir “$MYSQLD_DIR”
# chown -R mysql:mysql “$MYSQLD_DIR”
# fi
$ vim ~/.bash_profile
本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-05/144082.htm