创建mysql 用户组和用户

groupadd mysql
useradd -r -g mysql mysql

从官网下载 MySQL

wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz

解压

tar -zxvf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz

将解压的文件重命名,并移动到/usr/local目录

mv mysql-5.7.36-linux-glibc2.12-x86_64 /usr/local/mysql5.7

更改 mysql5.7 目录下所有文件夹所属的用户、用户组、以及权限

chown -R mysql:mysql /usr/local/mysql5.7
chmod -R 755 /usr/local/mysql5.7

创建 MySQL 相关目录

mkdir -p /usr/local/mysql5.7/{data,logs,tmp}

更改文件夹所属

chown -R mysql.mysql /usr/local/mysql5.7/data
chown -R mysql.mysql /usr/local/mysql5.7/logs
chown -R mysql.mysql /usr/local/mysql5.7/tmp

创建 MySQL 配置文件 my.cnf

[client]
port = 3306
socket = /usr/local/mysql5.7/tmp/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql5.7
datadir = /usr/local/mysql5.7/data
port = 3306
socket = /usr/local/mysql5.7/tmp/mysql.sock
pid-file = /usr/local/mysql5.7/tmp/mysqld.pid
tmpdir = /usr/local/mysql5.7/tmp
skip_name_resolve = 1
symbolic-links=0
max_connections = 2000
group_concat_max_len = 1024000
sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
lower_case_table_names = 1
log_timestamps=SYSTEM
character-set-server = utf8
interactive_timeout = 1800
wait_timeout = 1800
max_allowed_packet = 32M
binlog_cache_size = 4M
sort_buffer_size = 2M
read_buffer_size = 4M
join_buffer_size = 4M
tmp_table_size = 96M
max_heap_table_size = 96M
max_length_for_sort_data = 8096
#logs
server-id = 1003306
log-error = /usr/local/mysql5.7/logs/error.log
slow_query_log = 1
slow_query_log_file = /usr/local/mysql5.7/logs/slow.log
long_query_time = 3
log-bin = /usr/local/mysql5.7/logs/binlog
binlog_format = row
expire_logs_days = 15
log_bin_trust_function_creators = 1
relay-log = /usr/local/mysql5.7/logs/relay-bin
relay-log-recovery = 1
relay_log_purge = 1
#innodb
innodb_file_per_table = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group = 2
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_flush_neighbors = 0
innodb_flush_method = O_DIRECT
innodb_autoinc_lock_mode = 2
innodb_read_io_threads = 8
innodb_write_io_threads = 8
innodb_buffer_pool_size = 2G

配置 mysql.server

cd /usr/local/mysql5.7/support-files
cp mysql.server /etc/init.d/mysql
vi /etc/init.d/mysql

修改目录位置

basedir=/usr/local/mysql5.7
datadir=/usr/local/mysql5.7/data

注册开机启动服务

chkconfig --add mysql
chkconfig --list

添加 mysql 到环境变量
把/usr/local/mysql5.7/bin添加到PATH环境变量中
添加的内容export PATH=$PATH:/usr/local/mysql5.7/bin

vi /etc/profile
source /etc/profile

初始化 mysql

[root@localhost ~]# /usr/local/mysql5.7/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql5.7 --datadir=/usr/local/mysql5.7/data
# 临时密码保存在 errlog 中;获取临时密码
[root@localhost ~]# more /usr/local/mysql5.7/logs/error.log | grep password
2022-04-07T23:28:28.894994+08:00 1 [Note] A temporary password is generated for root@localhost: w(fYri(tk8Dj

启动 mysql 服务,并修改root用户密码

# 启动mysql服务
[root@localhost ~]# service mysql start
Starting MySQL... SUCCESS!
# 使用初始密码登录mysql服务 并修改密码
[root@localhost ~]# mysql -uroot -p
alter user 'root'@'localhost' identified by 'root';
flush privileges;

为了方便使用,可以创建软链接

ln -s /usr/local/mysql5.7/bin/mysql /usr/bin/mysql

mysql常用命令

#启动 
service mysql start
#关闭
service mysql stop
#重启
service mysql restart
GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐