Centos下二进制方式安装MySQL (Docker-Centos镜像环境)

环境:
MacOS
MySQL 5.7.14
CentOS

一:Docker环境下运行CentOS镜像

  1. docker pull index.docker.io/library/centos (拉取CentOS镜像)
  2. docker run -v /Users/arnoma2015/Downloads:/data/ --name mysql-study -i -t centos /bin/bash ( 启动容器,并挂载本机/Users/arnoma2015/Downloads目录,该目录存放了我下载的mysql二进制压缩包)

二:安装MySQL

  1. 下载MySQL二进制文件
    Centos下二进制方式安装MySQL (Docker-Centos镜像环境)_第1张图片
    我选择了第三个压缩包进行下载,下载后的文件存放至了/Users/arnoma2015/Downloads,CentOS OS容器挂载至/data/目录
  2. 解压
$ cp /data/mysql-5.7.14-linux-glibc2.5-x86_64.tar.gz /usr/local
$ cd /usr/local
$ tar zxvf mysql-5.7.14-linux-glibc2.5-x86_64.tar.gz
$ mv mysql-5.7.14-linux-glibc2.5-x86_64 mysql
  1. 创建用户和组
$ groupadd mysql
$ useradd -g mysql mysql
$ cd mysql
$ chown -R mysql:mysql .
  1. 初始化MySQL
$ cd support-files/
$ cp my-default.cnf /etc/my.cnf

#解释:mysqld启动时,会查找配置文件,顺序是/etc/my.cnf /etc/mysql/my.cnf   /usr/local/mysql/etc/my.cnf   ~/.my.cnf, 我们在/etc/my.cnf下进行相应配置

文件具体内容如下
=========================
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
datadir = /var/lib/mysql/data //数据存放位置
port = 3306 //端口号
server_id = 1 //服务辨识号
socket = /tmp/mysql.sock //其作用是程序与mysql server处于同一台机器,发起本地连接时可用

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

log-error=/var/log/mysqld.log //日志
pid-file=/var/run/mysqld/mysqld.pid //记录的是当前 mysqld 进程的 pid,pid 亦即 Process ID


sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

=========================
//注:详细配置请读者自行查阅

//接下来我们创建配置文件所需的文件夹并修改属主和属组
$ mkdir /var/lib/mysql/data
$ mkdir /var/run/mysqld
$ chown mysql:mysql /var/run/mysqld

//开始初始化 进入/usr/local/mysql目录下
$ ./bin/mysqld --initialize

//如果报错::./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory  该容器未安装libaio.so,使用yum源安装 
$ yum install libaio.x86_64

//安装完成后,再次初始化mysqld 成功

//初始化数据库完成后,改变数据库的属主和属组
$ chown -R mysql:mysql /var/lib/mysql 

//mysql链接
$ link  ./bin/mysql /usr/bin/mysql

//mysqld服务
$ cp ./support-files/mysql.server /etc/init.d/mysqld

三:运行MySQL

$ service mysqld start 
//如没有service命令,可使用/etc/init.d/mysqld start

$ mysql
//ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
//mysql启动是需要用户名密码的,但是初始化后并不知道root用户名和密码,解决方案如下: 1.修改/etc/my.cnf文件  
添加: 
skip-grant-tables
2.重启mysqld service mysqld restart, 然后可以无需密码直接登录

$ mysql -u root

Centos下二进制方式安装MySQL (Docker-Centos镜像环境)_第2张图片

此时我们需要重置root用户密码

mysql> use mysql
mysql> update user set authentication_string=password('yourpassword') where user='root';
mysql> exit;


重置完密码后请注释掉
#skip-grant-tables
重启服务 

$ mysql -u root -p 
键入密码登录

你可能感兴趣的:(Centos下二进制方式安装MySQL (Docker-Centos镜像环境))