一 安装前准备
1、检查是否已经安装过mysql,执行命令
rpm -qa | grep mysql
如果已存在,则执行删除命令 后边为Mysql目录
rpm -e --nodeps mysql-xxxx
2、查询所有Mysql对应的文件夹
whereis mysql
find / -name mysql
删除相关目录或文件
rm -rf /usr/bin/mysql /usr/include/mysql /data/mysql /data/mysql/mysql
验证是否删除完毕
whereis mysql
find / -name mysql
3、检查mysql用户组和用户是否存在,如果没有,则创建
cat /etc/group | grep mysql
cat /etc/passwd |grep mysql
groupadd mysql
useradd -r -g mysql mysql
4、从官网下载是用于Linux的Mysql安装包
下载命令: wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
也可以直接到mysql官网选择其他版本进行下载。然后再上传到linux系统
二 安装Mysql
1、在执行wget命令的目录下或你的上传目录下找到Mysql安装包:mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
执行解压命令:
tar -xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
解压完成后,可以看到当前目录下多了一个解压文件,移动该文件到/usr/local/下,并将文件夹名称修改为mysql。
如果/usr/local/下已经存在mysql,请将已存在mysql文件修改为其他名称,否则后续步骤可能无法正确进行。
执行命令如下:
mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql
2、在/data目录下创建mysql目录用于存放数据库文件
[root@localhost /]# mkdir /data/mysql
3、创建mysql用户
useradd mysql -s /sbin/nologin
4、更改mysql相关目录下所有的目录及文件夹所属的用户组和用户,以及权限
chown -R mysql:mysql /data/ /usr/local/mysql
5、配置环境变量
vim /etc/profile
添加一行 export PATH=/usr/local/mysql/bin:$PATH
使环境变量生效 source /etc/profile
验证环境变量是否配置成功
mysql -V
6、初始化
查询依赖环境是否存在 rpm -qa|grep libaio
不存在则安装 yum -y install libaio-devel
初始化 mysql --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
7、设置配置文件
vim /etc/my.cnf
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set=utf8
[mysql]
prompt="MySQL [\d]> "
no-auto-rehash
default-character-set=utf8
[mysqld]
port = 3306
socket = /tmp/mysql.sock
# 设置mysql的安装目录
basedir = /usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir = /data/mysql
pid-file = /data/mysql/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
skip-name-resolve
#skip-networking
back_log = 300
# 允许最大连接数
max_connections = 1211
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 8M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7
log_error = /data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/mysql/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
# lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
character-set-server=utf8
collation-server=utf8_general_ci
[mysqldump]
quick
max_allowed_packet = 500M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
8、启动
准备启动文件 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
添加到系统服务 chkconfig --add mysqld
设置自启动 chkconfig mysqld on
启动 service mysqld start
9、设置密码
mysql进入mysql
use mysql
update user set authentication_string=pssword('密码') where user='root';
flush privileges;
10、关闭防火墙或者设置端口白名单
systemctl start filewalld
firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --reload
firewall-cmd --list-all
11、设置root允许远程连接
update user set host='%' where user='root' and host='localhost';
flush privileges;