4

CentOS 7 下安装 MySQL 8.x - Grey Zeng

 1 year ago
source link: https://www.cnblogs.com/greyzeng/p/16756049.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

CentOS 7 下安装 MySQL 8.x

作者:Grey

原文地址:

博客园:CentOS 7 下安装 MySQL 8.x

CSDN:CentOS 7 下安装 MySQL 8.x

环境#

CentOS 版本:CentOS 7

下载地址:CentOS 7

安装方法见:Linux 安装,快照,克隆

MySQL 版本:MySQL 8.0.30

下载地址见:MySQL 下载

新建用户组#

#> groupadd mysql

创建 MySQL 用户并分配其用户组#

#> useradd -r -g mysql -s /bin/false mysql

准备必要文件夹并赋予相应的权限#

#> mkdir -p /usr/local/mysql /data/mysql/
#> chown -R mysql.mysql /usr/local/mysql/
#> chown -R mysql.mysql /data/mysql/
#> chmod -R 755 /data/mysql/
#> chmod -R 755 /usr/local/mysql/

注:其中/data/mysql是存放 MySQL 数据的目录。

安装必要依赖#

#> yum -y install wget gcc gcc-c++ ncurses ncurses-devel libaio-devel openssl openssl-devel git bison
#> yum install -y centos-release-scl-rh 
#> yum install -y centos-release-scl
#> yum install -y devtoolset-10-gcc 
#> yum install -y devtoolset-10-gcc-c++

如果安装比较慢,可以配置阿里云的镜像地址:CentOS 镜像

上传并解压 MySQL 安装包#

将下载好的 MySQL 安装包上传至服务器的/usr/local/目录下并解压,并将解压后的 mysql 安装目录中的内容放入/usr/local/mysql目录下

#> cd /usr/local/ 
#> tar -xf mysql-8.0.30-linux-glibc2.17-x86_64-minimal.tar.xz
#> mv mysql-8.0.30-linux-glibc2.17-x86_64-minimal/* mysql/

初始化 MySQL 数据库#

#> /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

初始化完毕,会打印如下日志信息

2022-10-05T09:24:34.206466Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2022-10-05T09:24:34.206531Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.30) initializing of server in progress as process 1533
2022-10-05T09:24:34.222068Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-10-05T09:24:34.868013Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-10-05T09:24:35.827528Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

修改 MySQL 配置参数#

MySQL 的配置文件默认位置是/etc/my.cnf,关于配置文件的说明见:option-files

修改配置之前,一个比较好的习惯的是先备份配置

#> cd /etc
#> cp my.cnf my.cnf.backup

/etc/my.cnf内容替换为如下内容:

[client]
port=3306
socket=/tmp/mysql.sock
default-character-set=utf8
#user=root
#password=123
[mysqld]
server-id=1
#skip-grant-tables
default_authentication_plugin=mysql_native_password
port=3306
user=mysql
log_bin=mysql-bin
binlog_format=mixed
max_connections=200
socket=/tmp/mysql.sock
basedir=/usr/local/mysql
datadir=/data/mysql
pid-file=/data/mysql/mysql.pid
init-connect='SET NAMES utf8'
character-set-server=utf8
default-storage-engine=INNODB
log_error=/data/mysql/mysql-error.log
slow_query_log_file=/data/mysql/mysql-slow.log
[mysqldump]
quick
max_allowed_packet=16M

my.cnf 文件的更多配置参数见:mysql-options

配置环境变量#

配置环境变量并应用环境变量

#> echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile
#> source /etc/profile

配置启动脚本【可选】#

#> cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

给启动脚本赋予可执行权限

#> chmod +x /etc/init.d/mysqld

这样就可以通过如下命令进行 MySQL 的启动了

#> service mysqld restart

配置 MySQL 开机启动【可选】#

#> chkconfig --level 345 mysqld on

新建一个用户并赋予权限【可选】#

安装完毕后,root用户默认没有密码,可以通过如下命令登录并修改root用户密码

#> mysql -u root -p

按回车,即可无密码登录,可以通过如下命令创建用户

创建一个用户名为 dump 的用户,密码初始化为 1234(可根据自定义需求修改)

mysql> create user 'dump'@'%' identified by '1234';

赋予权限,并配置可访问的 ip(可根据自定义需求修改)

mysql> grant all privileges on *.* to 'dump'@'%';
mysql> flush privileges;

开放 3306 端口#

如果要远程访问 MySQL,操作系统需要打开 MySQL 的默认 3306 端口的访问权限

#> firewall-cmd --zone=public --add-port=3306/tcp --permanent  
#> firewall-cmd --reload

接下来,可以通过 MySQL 客户端连接验证

推荐这一款免费开源的 MySQL 客户端:HeidiSQL

MySQL 的卸载#

如果要完全卸载 MySQL,可以通过如下的方式:

#> service mysqld stop
#> find / -name mysql

找到所有 mysql 相关的文件夹

/etc/selinux/targeted/active/modules/100/mysql
/usr/lib64/mysql
/usr/share/mysql
/usr/local/mysql
/usr/local/mysql/bin/mysql
/usr/local/mysql/include/mysql
/data/mysql
/data/mysql/mysql

然后执行rm -rf删掉这些文件即可。

当然你确保

#> find / -name mysql

出来的文件没有需要保留的文件,也可以执行

#> rm -rf `find / -name mysql`

批量删除。

参考文档#

Installing MySQL on Unix/Linux Using Generic Binaries


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK