Liunx:MySQL和Redis安装

405 阅读7分钟

1 MySQL

1.1 安装MySQL需要依赖包和编译软件

查询当前操作系统版本:

[root@ray /]# cat /etc/redhat-release  #查询当前内核版本
CentOS Linux release 7.6.1810 (Core) 
[root@ray /]# uname -r  #查询当前系统位数
3.10.0-957.21.3.el7.x86_64
[root@ray /]# uname -m
x86_64

1.1.1 安装MySQL需要的依赖包

[root@ray /]# yum install ncurses-devel libaio-devel -y  #下载
[root@ray /]# rpm -qa ncurses-devel libaio-devel	#安装

1.1.2 安装编译MySQL需要的软件

[root@ray /]# yum -y install cmake #下载
[root@ray /]# rpm -qa cmake		   #安装

1.1.3 建立MySQL用户账户

[root@ray /]# useradd -s /sbin/nologin -M mysql #默认创建和mysql同名的用户组
[root@ray /]# id mysql  #查询mysql的用户ID、组ID
uid=1003(mysql) gid=1003(mysql) groups=1003(mysql)

1.2 采用编译安装MySQL

[root@ray /]# mkdir -p /home/oldboy/tools #习惯性安装目录
[root@ray /]# cd /home/oldboy/tools/ #切换到安装目录
[root@ray tools]# rz  #用rz命令将MySQL官网下载的mysql-5.6.44.tar.gz上传到该目录下(推荐使用源码安装包、二进制包过大不利于上传下载)
[root@ray tools]# ls -lh #查询是否上传成功

1.3 解压并配置MySQL

[root@ray tools]# tar xf mysql-5.6.44.tar.gz #解压
[root@ray tools]# ls -lh   #查询是否解压成功
total 345M
drwxr-xr-x 34 7161 31415 4.0K Jul 14 14:18 mysql-5.6.44
-r--------  1 root root  314M Jul 14 13:38 mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz
-r--------  1 root root   32M Jul 14 14:13 mysql-5.6.44.tar.gz
[root@ray tools]# cd mysql-5.6.44 #进入解压后的目录
[root@ray mysql-5.6.44]# 
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.36 \  #安装目录
-DMYSQL_DATADIR=/application/mysql-5.6.36/data \            ##数据存放目录
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.36/tmp/mysql.sock \  #sock的文件路径
-DDEFAULT_CHARSET=utf8 \                 #默认字符集,不加默认为拉丁文
-DDEFAULT_COLLATION=utf8_general_ci \    #新建数据库默认字符集,不加默认为拉丁文
-DWITH_EXTRA_CHARSETS=all \              ##安装所有扩展字符集
-DWITH_INNOBASE_STORAGE_ENGINE=1 \       ##安装 innodb 存储引擎
-DWITH_FEDERATED_STORAGE_ENGINE=1 \      ##安装 FEDERATED 存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \      #安装 blackhole 存储引擎
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \     #安装 EXAMPLE 存储引擎
-DWITH_ZLIB=bundled \                    #服务器可支持压缩zlib库
-DWITH_SSL=bundled \                     #支持 SSL
-DENABLED_LOCAL_INFILE=1 \               #是否允许本地文件导入
-DWITH_EMBEDDED_SERVER=1 \               #是否建立了嵌入式服务器
-DENABLE_DOWNLOADS=1 \                   #是否下载任意文件
-DWITH_DEBUG=0                           #是否包括调试支持

复制上面的内容并执行

1.3.1 编译并安装MySQL

[root@ray mysql-5.6.44]# make
[root@ray mysql-5.6.44]# echo $? #校验是否make成功,若返回0则成功
0
[root@ray mysql-5.6.44]# make install #这里不建议将make && make install合起来使用,若出现问题则比较难排查
[root@ray mysql-5.6.44]# echo $?
0

1.3.2 为MySQL安装路径设置不带版本号的软连接/application/mysql

[root@ray mysql-5.6.44]# ln -s /application/mysql-5.6.44/ /application/mysql #这里如果有安装过数据文件和启动程序,最好停掉或删除,以免发生冲突
[root@ray mysql-5.6.44]# ls -l /application/ #查询软连接是否设置成功
total 4
lrwxrwxrwx  1 root root   26 Jul 14 14:59 mysql -> /application/mysql-5.6.44/
drwxr-xr-x 13 root root 4096 Jul 14 14:58 mysql-5.6.44
[root@ray mysql-5.6.44]# ls /application/mysql #查询MySQL的安装路径
bin      data  include  man         README   share      support-files
COPYING  docs  lib      mysql-test  scripts  sql-bench

1.3.3 创建MySQL数据库配置文件并对数据库目录授权

[root@ray mysql-5.6.44]# ll support-files/*.cnf
-rw-r--r-- 1 root root 1126 Jul 14 14:18 support-files/my-default.cnf
[root@ray mysql-5.6.44]# mv /etc/my.cnf.rpmsave /etc/my.cnf.rpmsave.bak #在启动MySQL服务时,会按照一定的顺序搜索my.cnf,先在/etc目录下找,若找不到则搜索"$basedir/my.cnf"
[root@ray mysql-5.6.44]# cp support-files/my-default.cnf /etc/my.cnf #生成新的my.cnf文件
cp:是否覆盖"/etc/my.cnf"? y
[root@ray mysql-5.6.44]# chown -R mysql.mysql /application/mysql #授权MySQL用户管理MySQL的安装目录,此步骤不做会导致启动服务报错

1.3.4 初始化MySQL数据库文件

[root@ray mysql-5.6.44]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data --user=mysql #初始化MySQL核心命令
2019-07-14 15:16:09 11331 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK   #出现两个OK是初始化成功的标志
...省略若干行
2019-07-14 15:16:11 11440 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK   #出现两个OK是初始化成功的标志
...省略若干行
New default config file was created as /application/mysql//my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings
#由上得知MySQL默认的配置文件已经变更到/application/mysql//my.cnf下
WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

从上说明中可以看到数据库启时会读取/etc/my.cnf,因此有可能会导致无法启动,避免方法就是使用mysqld_safe,启动服务时采用“--defaults-file”参数指定配置文件,前面已经将/etc/my.cnf改名了并进行了文件替换,所以不需要指定参数了

注意:此步骤必须初始化成功,否则后面会出现登录不了数据库等各种问题。

1.3.5 初始化后查看数据目录

[root@ray mysql-5.6.44]# ls -l /application/mysql/data/
total 110604
-rw-rw---- 1 mysql mysql 12582912 Jul 14 15:16 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Jul 14 15:16 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Jul 14 15:16 ib_logfile1
drwx------ 2 mysql mysql     4096 Jul 14 15:16 mysql                #用于存放管理MySQL的数据
drwx------ 2 mysql mysql     4096 Jul 14 15:16 performance_schema   #增加内部性能库
drwxr-xr-x 2 mysql mysql     4096 Jul 14 14:57 test                 #用于测试的test库
[root@ray mysql-5.6.44]# tree /application/mysql/data/
/application/mysql/data/
├── ibdata1
├── ib_logfile0
├── ib_logfile1
├── mysql
│   ├── columns_priv.frm
│   ├── columns_priv.MYD
│   ├── columns_priv.MYI
│   ├── db.frm
│   ├── db.MYD
│   ├── db.MYI
│   ├── event.frm
│   ├── event.MYD
│   ├── event.MYI
│   ├── func.frm
│   ├── func.MYD
...省略

1.4、配置并启动MySQL数据库

1.4.1设置MySQL启动脚本

[root@ray mysql-5.6.44]# pwd
/home/oldboy/tools/mysql-5.6.44
[root@ray mysql-5.6.44]# cp support-files/mysql.server /etc/init.d/mysqld #将MySQL启动脚本复制到MySQL的命令路径
[root@ray mysql-5.6.44]# chmod 700 /etc/init.d/mysqld #使脚本可执行,注意权限要小一些
[root@ray mysql-5.6.44]# ls -l /etc/init.d/mysqld
-rwx------ 1 root root 10619 Jul 14 15:27 /etc/init.d/mysqld

1.4.2启动MySQL数据库

[root@ray mysql-5.6.44]# /etc/init.d/mysqld start #启动数据库
Starting MySQL.Logging to '/application/mysql-5.6.44/data/ray.err'.
190714 16:42:44 mysqld_safe Directory '/application/mysql-5.6.44/tmp' for UNIX socket file don't exists.
 ERROR! The server quit without updating PID file (/application/mysql-5.6.44/data/ray.pid).
[root@ray mysql-5.6.44]# mkdir -p /application/mysql-5.6.44/tmp  #创建编译时指定的socket路径
[root@ray mysql-5.6.44]# chown -R mysql.mysql /application/mysql/ #赋权
[root@ray mysql-5.6.44]# /etc/init.d/mysqld start #启动
Starting MySQL.Logging to '/application/mysql-5.6.44/data/ray.err'.
 SUCCESS! 
[root@ray mysql-5.6.44]# netstat -lntup|grep mysql  #查看是否启动
tcp6       0      0 :::3306                 :::*                    LISTEN      32058/mysqld        

1.4.3查看报错日志

[root@ray mysql-5.6.44]# tail -10f /application/mysql/data/ray.err 
2019-07-14 16:44:03 32058 [Note] InnoDB: Waiting for purge to start
2019-07-14 16:44:03 32058 [Note] InnoDB: 5.6.44 started; log sequence number 1625987
2019-07-14 16:44:03 32058 [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: 8881fdd7-a613-11e9-9dd0-00163e10802e.
2019-07-14 16:44:03 32058 [Note] Server hostname (bind-address): '*'; port: 3306
2019-07-14 16:44:03 32058 [Note] IPv6 is available.
2019-07-14 16:44:03 32058 [Note]   - '::' resolves to '::';
2019-07-14 16:44:03 32058 [Note] Server socket created on IP: '::'.
2019-07-14 16:44:03 32058 [Note] Event Scheduler: Loaded 0 events
2019-07-14 16:44:03 32058 [Note] /application/mysql-5.6.44/bin/mysqld: ready for connections.
Version: '5.6.44'  socket: '/application/mysql-5.6.44/tmp/mysql.sock'  port: 3306  Source distribution

1.4.4设置MySQL开机自启动

[root@ray mysql-5.6.44]# chkconfig --add mysqld
[root@ray mysql-5.6.44]# chkconfig --list mysqld

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

注意:此外,将启动命令/etc/init.d/mysqld start 放到/etc/rc.local里面也可以实现开机自启动

1.5 将MySQL相关命令加入全局路径

1.5.1 确认MySQL所在的路径

[root@ray /]# ls /application/mysql/bin/mysql
/application/mysql/bin/mysql

1.5.2 在path变量前面增加/application/mysql/bin路径,并追加/etc/profile文件中:

[root@ray /]# echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile  #echo命令直接添加到环境变量中
[root@ray /]# tail -1 /etc/profile # 查看最后一行是否添加到位
export PATH=/application/mysql/bin:$PATH
[root@ray /]# source /etc/profile  #source使配置生效
[root@ray /]# echo $PATH   # 执行echo $PATH,若有/application/mysql/bin输出则表示配置成功
/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

另外一种配置方法:

ln -s /application/mysql/bin/* /usr/local/sbin

把MySQL命令所在的路径链接到全局路径/usr/local/sbin的下面

1.6 登陆MySQL测试

[root@ray /]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.44 Source distribution

Copyright (c) 2000, 2019, 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> 

1.6.1

提示:你还可以使用如下三种写法登陆MySQL(mysql安装完成后,管理员root是无密码的)

mysql -uroot -p 
mysql -uroot
mysql -uroot -p '密码'

1.6.2

出现问题:若出现“ERROR 1045(28000):Access denied for user ‘root’@‘localhost’(using password:NO)” 解决方法:重新初始化数据库即可,此问题一般都是数据库初始化问题或者数据库文件损坏及目录权限问题。

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

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> 

1.6.3设置root用户密码

[root@ray /]# mysqladmin -u root password 'oldboy123'
Warning: Using a password on the command line interface can be insecure.
[root@ray /]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@ray /]# mysql -uroot -poldboy123
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.44 Source distribution

Copyright (c) 2000, 2019, 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> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
|      | localhost |
| root | localhost |
|      | ray       |
| root | ray       |
+------+-----------+
6 rows in set (0.01 sec)

1.6.4设置远程连接

新增远程链接%用户

mysql> update mysql.user set Host='%' where Host='127.0.0.1';  
#若没有%的host,修改一个为远程链接用户设置密码
mysql> update user set Password=password('oldboy123') where User='root' and Host='%';
#使配置立即生效
mysql> flush privileges;

#使用远程链接Navicat工具链接,链接成功!

grant all on *.* to 'root'@'%' identified by 'oldboy123' with grant option;
#其中:xxx代表创建的数据库; password为用户密码,在此为root的密码。

数据库连接失败问题解决思路:

  1. 在mysql的安装目录下,修改my.ini配置文件,在[mysqld]下面加上skip-grant-tables,添加后保存,重启数据库
  2. 连接数据库,此时不需要密码就可以登录数据库了
  3. 查看mysql库user表中信息:这是我之前修改后的状态
  4. 将127.0.0.1修改为localhost,然后注释掉第一步中添加的内容
  5. 重启数据库,再连接数据库,账户root,密码为空
  6. 直接输入命令:mysql回车
  7. 切换到mysql库:
mysql> use mysql;
select user,host,password from mysql.user;
update mysql.user set host='%' where host='127.0.0.1';
#7.给root用户添加密码oldboy123
mysql> update mysql.user set password=password('oldboy123') where user='root' and host='localhost';
#8.将远程连接的密码设置为oldboy123
mysql> update user set password=password('oldboy123') where user='root' and host='%';
#9.使配置立即生效
mysql> flush privileges;

二、 navicat远程连接报错:1130 - Host '218.19.46.219' is not allowed to connect to this MySQL server

1.先切换库
mysql> use mysql;
2.再查询表
mysql> select host,user,password from user where user='root';
+-----------+------+
| host      | user |
+-----------+------+
| 127.0.0.1 | root |
| ::1       | root |
| localhost | root |
| ray       | root |
+-----------+------+
4 rows in set (0.00 sec)

三.使配置立即生效

mysql> flush privileges;


修改mysql配置文件:
vim /etc/my.cnf
在配置文件最后一行加上 skip-grant-tables
表示可以跳过权限直接用mysql登陆
跳过权限登陆则无法查看mysql库


update mysql.user set host='%' where host='127.0.0.1';

2 Redis

2.1 Redis和Memcached的区别

  1. Redis不仅支持简单的k/v类型的数据,同时还支持list、set、zset(sorted set)、hash等数据结构的存储,使得它拥有更广阔的应用场景。
  2. Redis最大的亮点是支持数据持久化,它在运行的时候可以将数据备份在磁盘中,断电或重启后,缓存数据可以再次加载到内存中,只要Redis配置的合理,基本上不会丢失数据。
  3. Redis支持主从模式的应用。
  4. Redis单个value的最大限制是1GB,而Memcached则只能保存1MB内的数据。
  5. Memcache在并发场景下,能用cas保证一致性,而Redis事务支持比较弱,只能保证事务中的每个操作连续执行。
  6. 性能方面,根据网友提供的测试,Redis在读操作和写操作上是略领先Memcached的。
  7. Memcached的内存管理不像Redis那么复杂,元数据metadata更小,相对来说额外开销就很少。Memcached唯一支持的数据类型是字符串string,非常适合缓存只读数据,因为字符串不需要额外的处理。 从上面这些看出,Redis的优势比Memcached大,不过Memcached也还是有它用武之地的。要是只选择装其中一种的话,我会选择Redis。