Linux系统安装mysql 5.7

文章目录

为什么要写这个呢?虽然网上这类文章多如牛毛,但是没有一篇能让我很顺畅的安装成功。找了好几台linux都尝试了一下,按照这个方式,非常顺畅,所以记录一下。

下载

  • 网站链接:https://dev.mysql.com/downloads/mysql
  • 选择版本5.7
    在这里插入图片描述
  • 然后选择一系列选项,最后选择64位的点击下载,下载界面选择No thanks, just start my download.
    在这里插入图片描述
    在这里插入图片描述

开始安装

先说明几点

  • 为了方便安装,安装目录是/usr/local/mysql
  • 先使用xftp等工具把tar包上传在/usr/local 目录
  1. 解压并移动目录(在/usr/local目录下)
tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.25-linux-glibc2.12-x86_64/ /usr/local/mysql
  1. 创建data目录
mkdir mysql/data
  1. 新增mysql用户及用户组,赋予权限
groupadd mysql
useradd mysql -g mysql
chmod -R 755 mysql
  1. 修改mysql目录的所属用户以及用户组(进入mysql目录)
cd mysql
chown -R mysql .
chgrp -R mysql .
  1. 安装libaio插件
yum install libaio
  1. 修改/etc/my.cnf文件
    需要修改一下配置文件,模板如下,可以直接使用:
[client]
no-beep
socket =/usr/local/mysql/mysql.sock
# pipe
# socket=0.0
port=3306
[mysql]
default-character-set=utf8
[mysqld]
lower_case_table_names =1
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port=3306
pid-file=/usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
character-set-server=utf8
default-storage-engine=INNODB
explicit_defaults_for_timestamp = true
# Server Id.
server-id=1
max_connections=2000
query_cache_size=0
table_open_cache=2000
tmp_table_size=246M
thread_cache_size=300
#限定用于每个数据库线程的栈大小。默认设置足以满足大多数应用
thread_stack = 192k
key_buffer_size=512M
read_buffer_size=4M
read_rnd_buffer_size=32M
innodb_data_home_dir =/usr/local/mysql/data
innodb_flush_log_at_trx_commit=0
innodb_log_buffer_size=16M
innodb_buffer_pool_size=256M
innodb_log_file_size=128M
innodb_thread_concurrency=128
innodb_autoextend_increment=1000
innodb_buffer_pool_instances=8
innodb_concurrency_tickets=5000
innodb_old_blocks_time=1000
innodb_open_files=300
innodb_stats_on_metadata=0
innodb_file_per_table=1
innodb_checksum_algorithm=0
back_log=80
flush_time=0
join_buffer_size=128M
max_allowed_packet=1024M
max_connect_errors=2000
open_files_limit=4161
query_cache_type=0
sort_buffer_size=32M
table_definition_cache=1400
binlog_row_event_max_size=8K
sync_master_info=10000
sync_relay_log=10000
sync_relay_log_info=10000
#批量插入数据缓存大小,可以有效提高插入效率,默认为8M
bulk_insert_buffer_size = 64M
interactive_timeout = 120
wait_timeout = 120
log-bin-trust-function-creators=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld_safe]
log-error=/usr/local/mysql/data/error.log
pid-file=/usr/local/mysql/mysqld.pid
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

可以直接把你本地的替换。注意一个配置:lower_case_table_names =1,这是数据库表不区分大小写,默认是0,区分大小写。

  1. 初始化数据库(进入bin目录)
cd bin/
./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data

这边启动的话,可能最新版本推荐使用如下语句:

./mysqld  --initialize	--user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data

但是这个在我本地经常性会报错。所以就用了过时的,而且在执行过程当中,也确实有很多警告,但是也没有关系,可以初始化成功。

  1. 启动(进入support-files目录)
    稍微注意一下,如果安装目录不是/usr/local/mysql的话,需要修改一下mysql.server文件,至于修改什么,打开之后,非常明显,当然,不推荐使用其余目录。
cd /usr/local/mysql/support-files
./mysql.server start
  1. 使用密码登陆
    上述初始化命令,会生成一个随机密码,在/root/.mysql_secret文件中:
cat /root/.mysql_secret

可以看到最后的字符串就是密码:

# Password set for user 'root@localhost' at 2019-03-25 08:21:57 
Iasl7qwStWH9

然后使用密码登陆,在/usr/local/mysql/bin目录下:

./mysql -u root -p

登陆成功。

  1. 修改密码,然后重新登陆一下
    使用随机密码登陆后,必须先修改一下密码:
set password = password('newPassword')

然后使用quit,退出重新登陆一下。

  1. 创建用户并分配权限
    一般也不会使用root去连接数据库,所以我们这边创建一个新用户,分配所有权限:
create user test identified by 'password';
grant all privileges on *.* to 'test';
flush privileges;
  1. 注册服务并以服务方式启动
    退出来,把mysql停掉:
./mysql.server stop

然后注册成服务,进行启动、停止和重启

cp mysql.server /etc/init.d/mysql
service mysql start/stop/restart

参考文章:
https://www.cnblogs.com/shizhongyang/p/8464876.html
https://www.cnblogs.com/luckycn/p/7289995.html

猜你喜欢

转载自blog.csdn.net/ywg_1994/article/details/88806711
今日推荐