CentOS下使用二进制发布包安装MySQL

2019-05-24  本文已影响0人  AlgoPeek

写在前面

最近需要搭建MySQL环境用于测试,本文主要描述使用mysql二进制发布安装包在CentOS下的搭建MySQL环境的步骤,供自己以后查阅,也供网友参考。

准备环境

shell> cat /etc/issue 
CentOS release 6.5 (Final)
mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

mysql安装包使用的是社区版MySQL Community Server,linux-glibc2.12-x86_64。下载地址

安装

以下操作会将mysql安装到/usr/local/mysql目录下,需要你拥有root权限。

shell>wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
shell> yum search libaio  # search for info
shell> yum install libaio # install library
shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql

由于mysql用户并不需要登录,使用-r,-s参数禁止mysql用户登录。

目录结构如下:

Directory Contents of Directory
bin mysqld server, client and utility programs
docs MySQL manual in Info format
man Unix manual pages
include Include (header) files
lib Libraries
share Error messages, dictionary, and SQL for database installation
support-files Miscellaneous support files
#可以解压到任意目录
shell>tar zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

#以安装包解压到/home/dev/opt/mysql-5.7.21-linux-glibc2.12-x86_64为例,软连接到/usr/local/mysql
shell>ln -s /home/dev/opt/mysql-5.7.21-linux-glibc2.12-x86_64 /usr/local/mysql
#在/home/dev/.bash_profile添加下面语句
export PATH=$PATH:/usr/local/mysql/bin
shell> cd /usr/local/mysql
shell> mkdir -p /var/lib/mysql
shell> chown mysql:mysql /var/lib/mysql
shell> chmod 750 /var/lib/mysql
shell> bin/mysqld --initialize-insecure --user=mysql --datadir=/var/lib/mysql
shell> bin/mysql_ssl_rsa_setup
shell>mysqld --verbose --help | grep -A 1 "Default options"
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf

默认情况下mysql会依次在以上目录查找配置文件,这里我将配置文件放到/usr/local/mysql/etc/my.cnf下:

shell>cd /usr/local/mysql
shell>mkdir etc
shell>touch etc/my.cnf

在my.cnf中写如以下内容,并且你最好了解每个配置项的含义:

[mysqld]
# GENERAL
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
default-storage-engine=InnoDB

[mysqld_safe]
log-error=/var/lib/mysql/mysql-error.log
pid-file=/var/lib/mysql/mysqld.pid

[client]
socket=/var/lib/mysql/mysql.sock
shell>cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
shell>chkconfig --add mysqld

检查是否已经配置成功:

shell> chkconfig --list | grep mysqld
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

3,4,5为on表示配置成功。

测试

shell>sudo service mysqld start
Starting MySQL.                                            [  OK  ]

使用service的方式启动mysql的前提是使用ckhconfig配置mysqld,也可以手动的方式启动mysql:

shell>cd /usr/local/mysql
shell>sh bin/mysqld_safe --defaults-file=/usr/local/mysql/etc/my.cnf --user=mysql &
shell>ps -ef | grep mysqld
shell>mysql -uroot
  1. 如果你在普通用户权限下启动mysql,可能会报权限错误,请加上sudo。
  2. 如果在启动过程中发现任何错误,在/var/lib/mysql/mysql-error.log也许能找到答案;
  3. 最后记得给root用户设置密码。

更多参考

  1. https://dev.mysql.com/doc/refman/8.0/en/binary-installation.html
  2. https://dev.mysql.com/doc/refman/8.0/en/starting-server-troubleshooting.html
  3. https://blog.csdn.net/qq_27298687/article/details/79740399
上一篇 下一篇

猜你喜欢

热点阅读