CentOS7安装Mysql

安装Mysql8.0.16

  1. 下载并安装cmake
    如果安装过不需要安装
cd /http/package/
sudo wget https://github.com/Kitware/CMake/releases/download/v3.14.5/cmake-3.14.5.tar.gz
sudo tar -zxvf cmake-3.14.5.tar.gz
sudo ./bootstrap
sudo gmake
sudo gmake install

查看版本
cmake -version

  1. 下载并安装ncurses
cd /http/package/
sudo wget ftp://ftp.invisible-island.net/ncurses/ncurses-6.1.tar.gz
sudo tar -zxvf ncurses-6.1.tar.gz

cd ncurses-6.1
sudo ./configure --prefix=/http/package/ncurses
sudo make
sudo make install
  1. 安装libaio库
    sudo yum -y install libaio*
    安装libaio和libaio-devel这两个库

  2. 为Mysql添加用户及组
    为了保证操作系统的安全,这里为mysql工作进程创建专用的用户

sudo groupadd mysql
sudo useradd -r -g mysql mysql
  1. 下载并安装

下载解压

cd /http/package/
sudo wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz
sudo tar -xvf mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz
sudo mv mysql-8.0.16-linux-glibc2.12-x86_64 /http/server/mysql

这里使用的是mysql官方提供的解压缩版的安装方式,不需要进行make操作。将mysql文件夹移动到安装路径后,需要修改mysql安装文件夹的权限,执行以下命令
sudo chown -R mysql.mysql /http/server/mysql/
后续如果在该目录及其子目录下创建了任何文件和文件夹,均需要再次执行该命令,以确保mysqld进程对该文件夹具有完全的读写权限

  1. 设置mysql配置文件
    mysql的配置文件是放在/etc/目录下的,名字是my.cnf,具体配置如下
[client]
port            = 3306
socket          = /http/server/mysql/tmp/mysql.sock

[mysqld]
basedir=/http/server/mysql
datadir=/http/server/mysql/data
socket=/http/server/mysql/tmp/mysql.sock

#skip-external-locking
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in [http://fedoraproject.org/wiki/Systemd](http://fedoraproject.org/wiki/Systemd)
log-error=/http/logs/mysql/error.log
pid-file=/http/logs/mysql/mysql.pid

[mysqld_safe]
log-error=/http/logs/mariadb/error.log
pid-file=/http/logs/mariadb/mariadb.pid

#
# include all files from the config directory
#

!includedir /etc/my.cnf.d

这里需要注意,配置文件最后一行引入了新的配置文件,下面列出my.cnf.d目录下名为mysql-clients.cnf的配置文件内容

#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#
[mysql]

[mysql_upgrade]

[mysqladmin]

[mysqlbinlog]

[mysqlcheck]

[mysqldump]

[mysqlimport]

[mysqlshow]

[mysqlslap]

在my.cnf配置文件中,需要注意几个配置,其中basedir指的是mysql的安装目录,datadir指的是mysql数据文件的存储目录,其他几项配置项,感兴趣可以自行查看官方解释。还需要特别说明的一个问题是,所有在配置文件中出现的路径、文件均需要具有可读写的权限,前面为MySQL创建了用户及组,所以上述配置文件中涉及到的路径和文件,均需要将其所有者及组指定为mysql用户,这样MySQL服务才能正常启动。

  1. 创建必要目录和文件

创建目录

sudo mkdir -p /http/server/mysql/data/ /http/server/mysql/tmp/ /http/logs/mysql/ /http/logs/mariadb/

创建文件

sudo touch /http/server/mysql/tmp/mysql.sock /http/logs/mysql/error.log /http/logs/mysql/mysql.pid /http/logs/mariadb/error.log /http/logs/mariadb/mariadb.pid

更改所有者

sudo chown -R mysql.mysql /http/server/mysql/data/ /http/server/mysql/tmp/ /http/logs/mysql/ /http/logs/mariadb/

  1. 初始化MySQL

因本文采用的是MySQL 8.0.16版本,所以不能使用mysql_install_db命令,而是需要使用mysqld –initialize命令对数据库进行初始化,具体命令如下

sudo /http/server/mysql/bin/mysqld --initialize --user=mysql --basedir=/http/server/mysql/ --datadir=/http/server/mysql/data/

error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
原因:
yum 安装的libnuma.so.1,但安装时默认安装的是32的,但db2需要的是64位的
解决办法:
如果已经安装了libnuma.so.1,先sudo yum remove libnuma.so.1
sudo yum -y install numactl.x86_64

若前面配置文件中的各配置项均正确,各路径、文件均存在并可读写,该命令将执行成功

  1. 使用安全模式启动MySQL
    sudo /http/server/mysql/bin/mysqld_safe --user=mysql &

  2. 设置MySQL服务开机自启
    创建配置文件
    sudo vim /lib/systemd/system/mysql.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
ExecStart=/http/server/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
#Restart=on-failure
#RestartPreventExitStatus=1
#PrivateTmp=false

systemctl start mysql 启动mysql
systemctl enable mysql 设置mysql开机启动

  1. 重置密码
    sudo vim /etc/my.cnf
    [mysqld]里面增加
    skip-grant-tables
    重启mysql
    systemctl restart mysql
    使用用户无密码登录
    /http/server/mysql/bin/mysql -uroot -p (直接点击回车,密码为空)
    选择数据库
    use mysql;
    将root密码置空
    update user set authentication_string = '' where user = 'root';
    刷新权限
    flush privileges;
    退出
    exit
    删除第1部增加的配置信息
    skip-grant-tables
    重启mysql
    systemctl restart mysql
    更改新的密码
    ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
    由于MySQL 8默认使用了新的密码验证插件:caching_sha2_password,如不支持请使用下面方式更改密码
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

你可能感兴趣的:(CentOS7安装Mysql)