Linux下CentOS7安装MySQL

计划搭建MyCat服务,先搭建好数据库环境,共在VMwar上安装了三台Linux主机,系统为CentOS7,主机名分别为:itcast-01;itcast-02;itcast-nginx
这里以itcast-02为例搭建数据库的全流程

博客编写不易,看过请点赞

安装配置Mysql大体步骤为三步

  1. 下载数据库的压缩包或二进制包,可以在linux用wget或yum下载,也可以外网下载再传到Linux
  2. 数据库初始化及环境配置
  3. 登陆数据库修改密码并实现远程连接

步骤一: 下载解压mysql数据库
1.1 mysql安装文件,我这里用的是5.7x版本,目录为/usr/local/

mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz

1.2 解压Mysql安装文件,执行命令

tar -xvf mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz
## 解压后
drwxr-xr-x. 9 root root       160 82 2019 apache-tomcat-8.5.32
-rw-r--r--. 1 root root   9584807 82 2019 apache-tomcat-8.5.32.tar.gz
drwxr-xr-x. 2 root root         6 115 2016 bin
drwxr-xr-x. 2 root root         6 115 2016 etc
drwxr-xr-x. 2 root root         6 115 2016 games
drwxr-xr-x. 2 root root         6 115 2016 include
drwxr-xr-x. 8   10  143       255 329 2018 jdk1.8.0_172
-rw-r--r--. 1 root root 190921804 82 2019 jdk-8u172-linux-x64.tar.gz
drwxr-xr-x. 2 root root         6 115 2016 lib
drwxr-xr-x. 2 root root         6 115 2016 lib64
drwxr-xr-x. 2 root root         6 115 2016 libexec
drwxr-xr-x. 9 root root       129 223 11:25 mysql-5.7.15-linux-glibc2.5-x86_64
-rw-r--r--. 1 root root 641556368 223 11:22 mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz
drwxr-xr-x. 2 root root         6 115 2016 sbin
drwxr-xr-x. 5 root root        49 81 2019 share
drwxr-xr-x. 2 root root         6 115 2016 src

1.3 修改解压后的文件名为mysql

mv mysql-5.7.15-linux-glibc2.5-x86_64 ./mysql
文件名修改后
drwxr-xr-x. 9 root root       160 82 2019 apache-tomcat-8.5.32
-rw-r--r--. 1 root root   9584807 82 2019 apache-tomcat-8.5.32.tar.gz
drwxr-xr-x. 2 root root         6 115 2016 bin
drwxr-xr-x. 2 root root         6 115 2016 etc
drwxr-xr-x. 2 root root         6 115 2016 games
drwxr-xr-x. 2 root root         6 115 2016 include
drwxr-xr-x. 8   10  143       255 329 2018 jdk1.8.0_172
-rw-r--r--. 1 root root 190921804 82 2019 jdk-8u172-linux-x64.tar.gz
drwxr-xr-x. 2 root root         6 115 2016 lib
drwxr-xr-x. 2 root root         6 115 2016 lib64
drwxr-xr-x. 2 root root         6 115 2016 libexec
drwxr-xr-x. 9 root root       129 223 11:25 mysql
-rw-r--r--. 1 root root 641556368 223 11:22 mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz
drwxr-xr-x. 2 root root         6 115 2016 sbin
drwxr-xr-x. 5 root root        49 81 2019 share
drwxr-xr-x. 2 root root         6 115 2016 src

1.4 创建组和用户

[root@itcast-02 local]# groupadd mysql
[root@itcast-02 local]# useradd -r -g mysql mysql

1.5 将安装目录的权限改为mysql

[root@itcast-02 local]# chown -R mysql.mysql /usr/local/mysql
## 查看mysql目录结构
drwxr-xr-x.  2 mysql mysql  4096 223 11:26 bin
-rw-r--r--.  1 mysql mysql 17987 825 2016 COPYING
drwxr-xr-x.  2 mysql mysql     6 223 11:29 data
drwxr-xr-x.  2 mysql mysql    55 223 11:26 docs
drwxr-xr-x.  3 mysql mysql  4096 223 11:25 include
drwxr-xr-x.  5 mysql mysql   229 223 11:26 lib
drwxr-xr-x.  4 mysql mysql    30 223 11:25 man
-rw-r--r--.  1 mysql mysql  2478 825 2016 README
drwxr-xr-x. 28 mysql mysql  4096 223 11:26 share
drwxr-xr-x.  2 mysql mysql   112 223 11:26 support-files

步骤二:数据库初始化及环境配置
2.1 设置mysql数据库data目录,在mysql安装目录下创建

[root@itcast-02 mysql]# mkdir data

2.2 初始化mysql数据库,注意最后一行的初始化密码

[root@itcast-02 mysql]# /usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
2020-02-23T03:30:05.117588Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-02-23T03:30:05.370286Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-02-23T03:30:05.504257Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-02-23T03:30:05.566668Z 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: c8ad537b-55ec-11ea-a3bc-000c29936866.
2020-02-23T03:30:05.568472Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-02-23T03:30:05.569995Z 1 [Note] A temporary password is generated for root@localhost: +2m)Q)g6Ks9/

2.3 修改mysql配置文件my.cnf

[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
user=mysql
port=3306
character-set-server=utf8
# 取消密码验证
#skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# skip-grant-tables
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

2.4 设置开机启动

[root@itcast-02 var]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
[root@itcast-02 var]# chkconfig mysql on
## 查看
[root@itcast-02 var]# chkconfig

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysql          	0:1:2:3:4:5:6:关
netconsole     	0:1:2:3:4:5:6:关
network        	0:1:2:3:4:5:6:

2.5 启动mysql服务,成功提示SUCCESS

[root@itcast-02 var]# service mysql start
Starting MySQL. SUCCESS! 
[root@itcast-02 var]# netstat -nltp;
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1525/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1113/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1111/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1344/master         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1113/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1111/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1344/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      3675/mysqld         
[root@itcast-02 var]# lsof -i:3306
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  3675 mysql   20u  IPv6  41045      0t0  TCP *:mysql (LISTEN)

步骤三:修改密码设置远程连接
3.1 登录mysql

[root@itcast-02 var]# mysql -uroot -p;
Enter password:

如果提示: bash: mysql: 未找到命令…
原因:
因为系统默认会查找/usr/bin下的命令连接,由于mysql没有在这个目录下,所以出现not found。因此需要做一个软连接到/usr/bin目录下。
解决:
ln -s /usr/local/mysql/bin/mysql /usr/bin

3.2 登录之后必须先修改初始密码,否则无法进行数据库操作

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user 'root'@'localhost' identified by 'root123';
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

3.3 切换到mysql数据库,修改root用户信息,开放远程连接服务

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;

mysql默认用户表 user是用户名 host是地址(正常显示localhost(本地),如果允许其它访问需更改成%)

3.4 给用户授权

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root123' WITH GRANT OPTION; 
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

示例说明:
GRANT privileges ON databasename.tablename TO ‘username’@‘host’ IDENTIFIED BY ‘username’ WITH GRANT OPTION;
privileges:表示要授予什么权力,例如可以有 select , insert ,delete,update等,如果要授予全部权力,则填 ALL
databasename.tablename:表示用户的权限能用在哪个库的哪个表中,如果想要用户的权限很作用于所有的数据库所有的表,则填 .,*是一个通配符,表示全部。
’username‘@‘host’:表示授权给哪个用户。
其中两个username 分别代表 “用户名”和“密码”

最后通过Navicat工具远程连接itcast02的Mysql数据库

********************
*General Information
********************
服务器类型: MySQL
连接名: itcast02
主机名或 IP 地址: 192.168.79.131
端口: 3306
用户名: root
保存密码: True

********************
*Advanced Information
********************
设置位置: C:\Users\张江丰\Documents\Navicat\MySQL\servers\itcast02
编码: 65001 (UTF-8)
保持连接间隔 (): N/A
使用 MySQL 字符集: True
使用压缩: False
自动连接: False
使用高级连接: False

********************
*SSL Information
********************
使用 SSL: False
使用验证: False
客户端密钥: 
客户端证书: 
CA 证书: 

********************
*SSH Information
********************
使用 SSH 通道: False
主机名或 IP 地址: 
端口: 22
用户名: 
验证方法: 密码
保存密码: False

********************
*HTTP Information
********************
使用 HTTP 通道: False
通道地址: 
用 base64 编码传出查询: False
使用密码验证: False
用户名: 
保存密码: False
使用证书验证: False
客户端密钥: 
客户端证书: 
CA 证书: 
Use Proxy: False
代理服务器 主机: 
代理服务器 端口: 0
代理服务器 用户名: 
代理服务器 保存密码: False

********************
*Other Information
********************
服务器版本: 5.7.15
通讯协定: 10
信息: 192.168.79.131 via TCP/IP

发布了32 篇原创文章 · 获赞 53 · 访问量 2466

猜你喜欢

转载自blog.csdn.net/qq_41714882/article/details/104458056