解压安装MySQL

Sakura 发布于 2022-12-26 422 次阅读


下载解压包:https://dev.mysql.com/downloads/mysql/

创建自定义目录{log,etc,run,data,binlogs}

mkdir {log,etc,run,data,binlogs}

配置环境变量

vim etc/my.cnf

[client]
port=33066
socket=/data/software/mysql/run/mysql.sock

[mysqld]
port=33066
user=mysql
socket=/data/software/mysql/run/mysql.sock
datadir=/data/software/mysql/data
log_error=/data/software/mysql/logs/mysql_error.log
skip-external-locking
key_buffer_size=256M
max_allowed_packet=512M
table_open_cache=1024
sort_buffer_size=4M
net_buffer_length=8K
read_buffer_size=4M
read_rnd_buffer_size=512K
myisam_sort_buffer_size=64M
thread_cache_size=128
query_cache_size=128M
tmp_table_size=128M
performance_schema_max_table_instances=6000

explicit_defaults_for_timestamp=true
#skip-networking
max_connections=500
max_connect_errors=100
open_files_limit=65535

log-bin=mysql-bin
binlog_format=mixed
server-id=1
expire_logs_days=10
early-plugin-load=""

default_storage_engine=InnoDB
innodb_file_per_table=1
innodb_data_home_dir=/data/software/mysql/data
innodb_data_file_path=ibdata1:10M:autoextend
innodb_log_group_home_dir=/data/software/mysql/data
innodb_buffer_pool_size=1024M
innodb_log_file_size=256M
innodb_log_buffer_size=8M
innodb_flush_log_at_trx_commit=1
innodb_lock_wait_timeout=50

slow_query_log=1
slow_query_log_file=/data/software/mysql/logs/mysql_slow_query.log

[mysqldump]
quick
max_allowed_packet=16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size=256M
sort_buffer_size=4M
read_buffer_size=4M
write_buffer_size=2M

[mysqlhotcopy]
interactive-timeout

初始化MySQL

# 初始化MySQL
/data/software/mysql/bin/mysqld 
--defaults-file=/data/software/mysql/etc/my.cnf 
--initialize 
--user=mysql 
--basedir=/data/software/mysql 
--datadir=/data/software/mysql/data

查看密码

cat /data/software/mysql/logs/mysql_error.log |grep localhost

加入系统服务

vim /usr/lib/systemd/system/mysqld.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
[Service]
User=root
Group=root
Type=forking
TimeoutSec=0
PermissionsStartOnly=true
ExecStart=/data/software/mysql/mysql-5.7.40-linux-glibc2.12-x86_64/bin/mysqld --defaults-file=/data/software/mysql/mysql-5.7.40-linux-glibc2.12-x86_64/etc/my.cnf
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
[Install]
WantedBy=multi-user.target

systemctl daemon-reload
systemctl enable mysqld.service
systemctl start mysqld.service

配置环境变量

vim /etc/profile
    export MYSQL_HOME=/data/software/mysql/mysql-5.7.40-linux-glibc2.12-x86_64
    export PATH=$PATH:$MYSQL_HOME/bin
. /etc/profile

登陆MySQL

mysql -uroot -P33066 -p -S /data/software/mysql/mysql-5.7.40-linux-glibc2.12-x86_64/run/mysql.sock

修改root密码

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