Arch Linux下源码编译安装MySQL 5.7.11

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

# 此时目录
$ 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源码安装文档:

MySQL文档cmake选项:

# 第一次进入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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/45a24680139db12009c384ed175fc896.html