创建mysql 用户组和用户
groupadd mysqluseradd -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.7chmod -R 755 /usr/local/mysql5.7
创建 MySQL 相关目录
mkdir -p /usr/local/mysql5.7/{data,logs,tmp}
更改文件夹所属
chown -R mysql.mysql /usr/local/mysql5.7/datachown -R mysql.mysql /usr/local/mysql5.7/logschown -R mysql.mysql /usr/local/mysql5.7/tmp
创建 MySQL 配置文件 my.cnf
[client]port = 3306socket = /usr/local/mysql5.7/tmp/mysql.sock[mysqld]user = mysqlbasedir = /usr/local/mysql5.7datadir = /usr/local/mysql5.7/dataport = 3306socket = /usr/local/mysql5.7/tmp/mysql.sockpid-file = /usr/local/mysql5.7/tmp/mysqld.pidtmpdir = /usr/local/mysql5.7/tmpskip_name_resolve = 1symbolic-links=0max_connections = 2000group_concat_max_len = 1024000sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTIONlower_case_table_names = 1log_timestamps=SYSTEMcharacter-set-server = utf8interactive_timeout = 1800wait_timeout = 1800max_allowed_packet = 32Mbinlog_cache_size = 4Msort_buffer_size = 2Mread_buffer_size = 4Mjoin_buffer_size = 4Mtmp_table_size = 96Mmax_heap_table_size = 96Mmax_length_for_sort_data = 8096#logsserver-id = 1003306log-error = /usr/local/mysql5.7/logs/error.logslow_query_log = 1slow_query_log_file = /usr/local/mysql5.7/logs/slow.loglong_query_time = 3log-bin = /usr/local/mysql5.7/logs/binlogbinlog_format = rowexpire_logs_days = 15log_bin_trust_function_creators = 1relay-log = /usr/local/mysql5.7/logs/relay-binrelay-log-recovery = 1relay_log_purge = 1#innodbinnodb_file_per_table = 1innodb_log_buffer_size = 16Minnodb_log_file_size = 256Minnodb_log_files_in_group = 2innodb_io_capacity = 2000innodb_io_capacity_max = 4000innodb_flush_neighbors = 0innodb_flush_method = O_DIRECTinnodb_autoinc_lock_mode = 2innodb_read_io_threads = 8innodb_write_io_threads = 8innodb_buffer_pool_size = 2G
配置 mysql.server
cd /usr/local/mysql5.7/support-filescp mysql.server /etc/init.d/mysqlvi /etc/init.d/mysql
修改目录位置
basedir=/usr/local/mysql5.7datadir=/usr/local/mysql5.7/data
注册开机启动服务
chkconfig --add mysqlchkconfig --list
添加 mysql 到环境变量
把/usr/local/mysql5.7/bin添加到PATH环境变量中
添加的内容export PATH=$PATH:/usr/local/mysql5.7/bin
vi /etc/profilesource /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 password2022-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 startStarting MySQL... SUCCESS!# 使用初始密码登录mysql服务 并修改密码[root@localhost ~]# mysql -uroot -palter 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