Appearance
MySQL8.0二进制安装
官网下载地址:https://dev.mysql.com/downloads/mysql/ 下载 Community(社区版) 的 generic(通用版) 二进制包文件。
查看mariadb是否存在
bash
$ rpm -qa | grep mariadb # 查看是否存在mariadb安装
$ rpm -e --nodeps mariadb-libs # 卸载包。 --nodeps 不检验依赖bash
# 查看当前系统文件库版本:
$ ldd --version执行样例
bash
$ ldd --version
ldd (GNU libc) 2.17
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
由 Roland McGrath 和 Ulrich Drepper 编写。
解压并移动安装包
bash
# 解压 tar -zxvf 待解压目录
$ tar -xf mysql-8.0.35-linux-glibc2.17-x86_64.tar.xz
# 移动 mv 移动目录 移动后的目录
$ mv mysql-8.0.35-linux-glibc2.17-x86_64 /data/mysql创建数据目录
bash
# 进入MySQL目录
$ cd /data/mysql
# 创建data,logs文件夹(也可以根据那个磁盘大而定 --df -Th查看)
$ mkdir /data/mysql/{data,logs}
$ mkdir /etc/my.cnf.d不创建目录
/etc/my.cnf.d可能会报错:bashmysqld: Can't read dir of '/etc/my.cnf.d' (OS errno 2 - No such file or directory) mysqld: [ERROR] Stopped processing the 'includedir' directive in file /etc/my.cnf at line 52. mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
创建mysql用户权限
bash
# 创建mysql用户并添加到mysql组中,系统用户无登陆模式
$ useradd -s /sbin/nologin mysql
# 修改mysql目录的组以及权限
$ chown mysql:mysql -R /data/mysql修改MySQL配置文件
vi /etc/my.cnfbash
[mysqld]
character_set_server=utf8
user = mysql
port = 3306
basedir = /data/mysql
datadir = /data/mysql/data
socket = /tmp/mysql.sock
# 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
[mysqld_safe]
log-error=/data/mysql/logs/mysql.log
#pid-file=/data/mysql/data/mysqld.pid
pid-file=/var/run/mysql/mysql.pid
# 日志记录慢查询SQL
slow_query_log = true
slow_query_log_file = /data/mysql/logs/mysql_slow_query.log
long_query_time = 1
# 记录没有使用index的查询记录
log-queries-not-using-indexes
# 配置mysqlbinlog
server-id = 1
log-bin = mysql-bin
binlog_format = MIXED
gtid-mode = on
enforce-gtid-consistency = true
master-info-repository = TABLE
relay-log-info-repository = TABLE
# 并行复制工作线程数
slave-parallel-workers = 4
# 设置sql_mode行为规则
sql_mode = 'NO_ENGINE_SUBSTITUTION'
# 设置binlog过期时间,过期自动删除 binlog_expire_logs_seconds单位是秒
binlog_expire_logs_seconds = 604800
max_binlog_size = 100M
log_bin_trust_function_creators = 1
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d初始化MySQL
bash
# 初始化 --defaults-file为配置文件所在 ,--basedir为解压路径,--datadir为数据保存路径 --user表示MySQL所属用户
$ ./bin/mysqld --defaults-file=/etc/my.cnf --basedir=/data/mysql/ --datadir=/data/mysql/data --user=mysql --initialize执行样例
bash
$ ./bin/mysqld --defaults-file=/etc/my.cnf --basedir=/data/mysql/ --datadir=/data/mysql/data --user=mysql --initialize
2024-07-10T13:12:31.880977Z 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.
2024-07-10T13:12:31.881167Z 0 [System] [MY-013169] [Server] /data/mysql/bin/mysqld (mysqld 8.0.35) initializing of server in progress as process 8184
2024-07-10T13:12:31.883015Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2024-07-10T13:12:31.895450Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-07-10T13:12:32.345234Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-07-10T13:12:34.435243Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: u&_eNw!Fe6Ji初始化后会有密码产生,记录最后一行密码 u&_eNw!Fe6Ji
设置systemd启动文件
bash
$ vi /usr/lib/systemd/system/mysql.servicebash
[Unit]
Description=MySQL Server
Documentation=man:mysqld()
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
PIDFile=/data/mysql/data/mysqld.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Needed to create system tables
#ExecStartPre=/usr/bin/mysqld_pre_systemd
# Start main service
ExecStart=/data/mysql/bin/mysqld --daemonize --pid-file=/data/mysql/data/mysqld.pid
#注意这里要加上 --daemonize
# Use this to switch malloc implementation
#EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE =
Restart=on-failure
RestartPreventExitStatus=
PrivateTmp=false启动服务
bash
# 开机自启动
$ systemctl enable mysql.service
$ systemctl start mysql.service
设置环境变量
$ vi /etc/profile
export PATH=$PATH:/data/mysql/bin ## 最后一行添加
$ source /etc/profile修改密码
bash
## 登入MySQL
$ mysql -uroot -p'u&_eNw!Fe6Ji' #初始化密码
## 修改密码
mysql> alter user 'root'@'localhost' identified by '123456';
mysql> set password for root@localhost = '123456';
## 设置MySQL远程登入
mysql> use mysql;
msyql> update user set user.Host='%' where user.User='root';
mysql> flush privileges;
mysql> exit
$ mysql -uroot -p123456
mysql>