Linux环境下安装MySQL详细步骤

Linux安装MySQL详细步骤

安装前准备

  • 检查是否已经安装过mysql,执行命令:
rpm -qa | grep mysql
  • 如果有,则执行如下命令删除:
rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.x86_64
  • 查询所有Mysql对应的文件夹,如果存在则删除。
[root@i-2pcc1lv7 ~]# whereis mysql
mysql: /usr/lib64/mysql /usr/share/mysql
[root@i-2pcc1lv7 ~]# rm -rf /usr/lib64/mysql
[root@i-2pcc1lv7 ~]# rm -rf /usr/share/mysql
[root@i-2pcc1lv7 ~]# whereis mysql
mysql:[root@i-2pcc1lv7 ~]# 

[root@i-2pcc1lv7 ~]# find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/usr/lib/ocf/resource.d/heartbeat/mysql
/usr/share/resource-agents/ocft/configs/mysql
  • 检查mysql用户组和用户是否存在,如果没有,则创建
[root@i-2pcc1lv7 ~]# groupadd mysql
[root@i-2pcc1lv7 ~]# useradd -r -g mysql mysql
  • 从官网下载是用于Linux的Mysql安装包,或者手动到官网下载上传至服务器。
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

安装

  • 在执行wget命令的目录下或你的上传目录下找到Mysql安装包:mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
    执行解压命令:
[root@i-2pcc1lv7 local]# pwd
/usr/local
[root@i-2pcc1lv7 local]# ll
total 629816
drwxr-xr-x. 2 root root        49 Aug 27 16:42 bin
drwxr-xr-x. 2 root root         6 Nov  5  2016 etc
drwxr-xr-x. 2 root root         6 Nov  5  2016 games
drwxr-xr-x. 2 root root         6 Nov  5  2016 include
drwxr-xr-x. 2 root root         6 Nov  5  2016 lib
drwxr-xr-x. 2 root root         6 Nov  5  2016 lib64
drwxr-xr-x. 2 root root         6 Nov  5  2016 libexec
-rw-r--r--  1 root root 644930593 Aug 31 19:08 mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
drwxr-xr-x  7 root root        76 Sep  5  2019 python3
drwxr-xr-x. 2 root root         6 Nov  5  2016 sbin
drwxr-xr-x. 5 root root        49 Mar 29  2018 share
drwxr-xr-x. 2 root root         6 Nov  5  2016 src
[root@i-2pcc1lv7 local]# tar xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
  • 解压完成后,将其改名为mysql
[root@i-2pcc1lv7 local]# mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql
[root@i-2pcc1lv7 local]# ll
total 629816
drwxr-xr-x. 2 root root        49 Aug 27 16:42 bin
drwxr-xr-x. 2 root root         6 Nov  5  2016 etc
drwxr-xr-x. 2 root root         6 Nov  5  2016 games
drwxr-xr-x. 2 root root         6 Nov  5  2016 include
drwxr-xr-x. 2 root root         6 Nov  5  2016 lib
drwxr-xr-x. 2 root root         6 Nov  5  2016 lib64
drwxr-xr-x. 2 root root         6 Nov  5  2016 libexec
drwxr-xr-x  9 root root       129 Aug 31 20:01 mysql
-rw-r--r--  1 root root 644930593 Aug 31 19:08 mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
drwxr-xr-x  7 root root        76 Sep  5  2019 python3
drwxr-xr-x. 2 root root         6 Nov  5  2016 sbin
drwxr-xr-x. 5 root root        49 Mar 29  2018 share
drwxr-xr-x. 2 root root         6 Nov  5  2016 src
  • 在**/usr/local/mysql**目录下创建data目录
[root@i-2pcc1lv7 mysql]# mkdir data
[root@i-2pcc1lv7 mysql]# ls
bin  COPYING  data  docs  include  lib  man  README  share  support-files
  • 更改mysql目录下所有的目录及文件夹所属的用户组和用户,以及权限
[root@i-2pcc1lv7 mysql]# chown -R mysql:mysql /usr/local/mysql
[root@i-2pcc1lv7 mysql]# chmod -R 755 /usr/local/mysql
  • 编译安装并初始化mysql,**务必记住初始化输出日志末尾的密码(数据库管理员临时密码)**编译安装并初始化mysql,务必记住初始化输出日志末尾的密码(数据库管理员临时密码)
[root@i-2pcc1lv7 mysql]# cd /usr/local/mysql/bin
[root@i-2pcc1lv7 bin]# ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
2020-08-31T12:05:17.393228Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-08-31T12:05:17.679400Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-08-31T12:05:17.728911Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-08-31T12:05:17.787713Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 3c486de7-eb82-11ea-96cd-525401e3f0a7.
2020-08-31T12:05:17.789865Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-08-31T12:05:17.790726Z 1 [Note] A temporary password is generated for root@localhost: cr#5<Ftky%Y1
  • 编辑配置文件***/etc/my.cnf***,添加配置如下
[mysqld]
datadir=/usr/local/mysql/data
port = 3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
symbolic-links=0
max_connections=400
innodb_file_per_table=1
#表名大小写不明感,敏感为
lower_case_table_names=1
  • 启动mysql服务器
[root@i-2pcc1lv7 bin]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.2020-08-31T12:10:10.685085Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
The server quit without updating PID file (/usr/local/mysql[FAILED]2pcc1lv7.pid).

#遇到如上报错,因为没有路径也没有权限,所以创建此路径并授权给mysql用户

[root@i-2pcc1lv7 bin]# mkdir /var/log/mariadb
[root@i-2pcc1lv7 bin]# touch /var/log/mariadb/mariadb.log
[root@i-2pcc1lv7 bin]# chown -R mysql:mysql /var/log/mariadb

#再次启动MySQL服务器
[root@i-2pcc1lv7 bin]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL.                                            [  OK  ]
  • 添加软连接,并重启mysql服务
[root@i-2pcc1lv7 bin]# ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
[root@i-2pcc1lv7 bin]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
[root@i-2pcc1lv7 bin]# service mysql restart
Shutting down MySQL..                                      [  OK  ]
Starting MySQL.                                            [  OK  ]
  • 登录mysql,修改密码
[root@i-2pcc1lv7 bin]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password for root@localhost = password('126315');
Query OK, 0 rows affected, 1 warning (0.00 sec)
  • 开放远程连接
mysql> use mysql;
Database changed
mysql> update user set user.Host='%' where user.User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值