linux二进制安装mysql 5.7.33

二进制安装mysql

centos7 二进制安装mysql 5.7.33

官方文档:

https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html

下载地址:

https://dev.mysql.com/downloads/mysql/5.7.html

版本选择linux通用二进制x64:

# Select Operating System:
linux-Generic

# Select OS Version:
linux-Generic-(glibc2.12), (x86, 64-bit)

下载二进制包

wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz -P /tmp

卸载默认mariadb

rpm -qa | grep mariadb | xargs yum remove -y

安装mysql依赖

yum install -y libaio numactl-devel

创建mysql用户和用户组

groupadd mysql
useradd -r -g mysql -s /bin/false mysql

解压二进制包到/opt/mysql目录

mkdir -p /opt/mysql
tar -zxvf /tmp/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C /opt/mysql --strip=1

配置环境变量,将/opt/mysql/bin目录添加到path变量中

echo 'export PATH=$PATH:/opt/mysql/bin' > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
mysql --version

创建数据存储路径并授权

mkdir -p /opt/mysql/{data,logs,etc}
mkdir -p /opt/mysql/logs/binlog

创建mysql配置文

cat > /opt/mysql/etc/my.cnf <<EOF
[client]
port=3306
socket=/tmp/mysql.sock

[mysqld]
port = 3306
socket=/tmp/mysql.sock
user = mysql
server-id = 1
basedir = /opt/mysql
datadir = /opt/mysql/data
pid-file = /opt/mysql/data/mysqld.pid

default_storage_engine = InnoDB
max_allowed_packet = 512M
max_connections = 2048
open_files_limit = 65535
explicit_defaults_for_timestamp = 1

skip-name-resolve
lower_case_table_names=1

character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'

innodb_buffer_pool_size = 512M
innodb_log_file_size = 1024M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 0

key_buffer_size = 64M

log-error = /opt/mysql/logs/error.log
log-bin = /opt/mysql/logs/binlog/mysql-bin

binlog_format = mixed
expire_logs_days = 10

slow_query_log = 1
slow_query_log_file = /opt/mysql/logs/slow.log
long_query_time = 1

[mysqldump]
quick
EOF

配置目录权限

chown -R mysql.mysql /opt/mysql

初始化mysql

mysqld --defaults-file=/opt/mysql/etc/my.cnf --initialize

创建systemd启动文件

cat > /etc/systemd/system/mysqld.service << 'EOF'
[Unit]
Description=MySQL Server
Documentation=man:mysqld(7)
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

Type=forking
PIDFile=/opt/mysql/data/mysqld.pid
TimeoutSec=0

ExecStart=/opt/mysql/bin/mysqld --defaults-file=/opt/mysql/etc/my.cnf --daemonize --pid-file=/opt/mysql/data/mysqld.pid $MYSQLD_OPTS 
EnvironmentFile=-/etc/sysconfig/mysql

LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
EOF

启动mysql服务

systemctl enable --now mysqld

查看mysql服务运行状态

[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/etc/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-03-17 16:24:43 CST; 9min ago
     Docs: man:mysqld(7)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 7021 ExecStart=/opt/mysql/bin/mysqld --defaults-file=/opt/mysql/etc/my.cnf --daemonize --pid-file=/opt/mysql/data/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
 Main PID: 7023 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─7023 /opt/mysql/bin/mysqld --defaults-file=/opt/mysql/etc/my.cnf --daemonize --pid-file=/opt/mysql/data/mysqld.pid

Mar 17 16:24:42 localhost.localdomain systemd[1]: Starting MySQL Server...
Mar 17 16:24:43 localhost.localdomain systemd[1]: Started MySQL Server.

在日志文件里会提示一个临时密码,记录这个密码

password=$(grep 'temporary password' /opt/mysql/logs/error.log | awk '{print $NF}')

登录和重置root密码

mysql -uroot -p$password

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
flush privileges;

配置支持远程连接

use mysql;
update user set host = '%' where user = 'root';
flush privileges;

重新登录进行验证

mysql -uroot -p123456

mysql -uroot -p123456 -h 192.168.92.20

你可能感兴趣的:(database)