Linux以压缩包形式安装MySQL

一、下载压缩包

文件名称为mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz,下载完成后上传到Linux服务器自己存放临时安装包文件的目录。

二、解压缩并移动到安装目录

tar -zxvf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.39-linux-glibc2.12-x86_64/ /usr/local/
cd /usr/local/
mv mysql-5.7.39-linux-glibc2.12-x86_64/ mysql

三、创建mysql用户及组

groupadd mysql && useradd -r -g mysql mysql

四、创建数据存放目录并给mysql用户授权

mkdir -p /mysql/data
chown mysql:mysql -R /mysql/data

五、编辑(创建)my.cnf

/etc目录找到my.cnf文件,修改其中内容为:

[mysqld]
port        = 3306
socket      = /tmp/mysql.sock
user=mysql
basedir=/usr/local/mysql
datadir = /mysql/data
character_set_server=utf8mb4
default_storage_engine = InnoDB
performance_schema_max_table_instances = 400
table_definition_cache = 400
skip-external-locking
key_buffer_size = 128M
max_allowed_packet = 100G
table_open_cache = 512
sort_buffer_size = 2M
net_buffer_length = 4K
read_buffer_size = 2M
read_rnd_buffer_size = 256K
myisam_sort_buffer_size = 32M
thread_cache_size = 64
query_cache_size = 64M
tmp_table_size = 64M
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

explicit_defaults_for_timestamp = true
#skip-name-resolve
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
slow_query_log=1
slow-query-log-file=/mysql/data/mysql-slow.log
long_query_time=3
#log_queries_not_using_indexes=on
early-plugin-load = ""
lower_case_table_names=1

innodb_data_home_dir = /mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /mysql/data
innodb_buffer_pool_size = 512M
innodb_log_file_size = 256M
innodb_log_buffer_size = 64M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 4
innodb_write_io_threads = 4

六、初始化MySQL

cd mysql/bin/
./mysqld --defaults-file=/etc/my.cnf --user=mysql --initialize

记录下输出的最后一行,MySQL的初始密码,登录要用到。

七、创建mysql软连接

为了在任意目录都能执行mysql命令,需要将软连接放入到/usr/bin

ln -s /usr/local/mysql/bin/mysql /usr/bin

八、将MySQL添加到服务,开机启动

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chkconfig --add mysql

查看服务是否已添加到开机启动

chkconfig --list

九、启动MySQL

service mysql start

十、修改初始密码

使用之前初始化时,输出的初始密码,登入MySQL

[root@localhost mysql]# mysql -u root -p
Enter password: (粘进来初始密码)
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root@123456';

改完后,可执行一些基础SQL命令查询测试一下。

十一、验证

退出MySQL,重启服务器,开机后使用修改后的密码登入MySQL,验证是否自启动成功,以及是否在任意目录都能执行mysql命令。

拓展:查看数据库字符集

select SCHEMA_NAME,DEFAULT_CHARACTER_SET_NAME,DEFAULT_COLLATION_NAME,SQL_PATH from information_schema.SCHEMATA;

十二、开启防火墙端口(可选)

有需要从其它机器上访问已装好的MySQL的话,需要配置开放下防火墙端口(数据库服务器未开启防火墙的可忽略这一步)

请参考另一篇关于防火墙端口配置

posted @ 2023-09-02 23:52  洗洗早睡啦  阅读(401)  评论(0编辑  收藏  举报