常见的,Linux上安装软件有如下几种方式:
以上几种方式的便捷性依次递减,但通用性依次上升,比如直接下载压缩包进行解压,这种方式一般需要自己做一些额外的配置工作,但只要掌握了方法,各个平台基本都适用,YUM虽然简单,但是平台受限,网络受限,必要的时候还需要增加一些特定YUM源。
几种安装方式最好都能掌握,原则上能用简单的就用简单的:YUM>RPM>tar.gz>源码
本文是介绍MySQL在CentOS上的安装,主要步骤都是参考了MySQL官方文档:https://dev.mysql.com/doc/refman/8.0/en/installing.html
下面针对掌握程度的难易程度来依次讲解各个安装方式:
shell> rpm -qa|grep mariadb
如果不存在(上面检查结果返回空)则跳过步骤
shell> rpm -e --nodeps mariadb-server
shell> rpm -e --nodeps mariadb
shell> rpm -e --nodeps mariadb-libs
其实yum方式安装是可以不用删除mariadb的,安装MySQL会覆盖掉之前已存在的mariadb
shell> rpm -qa|grep mysql
shell> rpm -e --nodeps xxx
从CentOS 7开始,MariaDB成为Yum源中默认的数据库安装包。也就是说在CentOS 7及以上的系统中使用yum安装MySQL默认安装的会是MariaDB(MySQL的一个分支)。如果想安装官方MySQL版本,需要使用MySQL提供的Yum源。
查看系统版本:
shell> cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
选择对应的版本进行下载,例如CentOS 8当前在官网查看最新Yum源的下载地址为:https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
shell> wget https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
若提示“bash: wget: command not found”时,需先安装wget包:
shell> yum -y install wget
shell> sudo rpm -Uvh platform-and-version-specific-package-name.rpm
例如CentOS8当前最新MySQL源安装:
shell> sudo rpm -Uvh mysql80-community-release-el8-1.noarch.rpm
执行成功后会在/etc/yum.repos.d/
目录下生成两个repo文件mysql-community.repo
及mysql-community-source.repo
并且通过yum repolist可以看到mysql相关资源
shell> yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community MySQL Connectors Community 42
mysql-tools-community MySQL Tools Community 19
mysql80-community MySQL 8.0 Community Server 49
使用MySQL Yum Repository安装MySQL,默认会选择当前最新的稳定版本,例如通过上面的MySQL源进行安装的话,默安装会选择MySQL 8.0版本,如果就是想要安装该版本,可以直接跳过此步骤,如果不是,比如我这里希望安装MySQL5.7版本,就需要“切换一下版本”:
shell> yum repolist all | grep mysql
shell> sudo yum-config-manager --disable mysql80-community
shell> sudo yum-config-manager --enable mysql57-community
除了使用yum-config-manager之外,还可以直接编辑/etc/yum.repos.d/mysql-community.repo
文件
enabled=0禁用
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/8/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
enabled=1启用
# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
shell> yum repolist enabled | grep mysql
如果同时启用了多个仓库,安装时会选择最新版本
shell> sudo yum install mysql-community-server
该命令会安装MySQL服务器 (mysql-community-server) 及其所需的依赖、相关组件,包括mysql-community-client、mysql-community-common、mysql-community-libs等
如果带宽不够,这个步骤时间会比较长,请耐心等待~
注意:特别是在下载mysql-community-libs-xxx.rpm安装包时可能会出现 “Timeout was reached for http://repo.mysql.com/yum/mysql-8.0-community/el/8/x86_64/mysql-community-libs-xxx.rpm [Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds]”,可在/etc/yum.conf文件中添加timeout=120,默认30,执行如下命令:
shell> echo "timeout=120" >> /etc/yum.conf
shell> sudo systemctl start mysqld.service
CentOS 6:
shell> sudo service mysqld start
shell> sudo systemctl status mysqld.service
CentOS 6:
shell> sudo service mysqld status
shell> sudo systemctl stop mysqld.service
CentOS 6:
shell> sudo service mysqld stop
shell> sudo systemctl restart mysqld.service
CentOS 6:
shell> sudo service mysqld restart
MySQL第一次启动后会创建超级管理员账号root@localhost
,初始密码存储在日志文件中:
shell> sudo grep 'temporary password' /var/log/mysqld.log
shell> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
出现上面的提示是因为密码太简单了,解决方法如下:
# 修改密码复杂程度,0最低
set global validate_password.policy=0;
# 修改密码长度,默认为8位,可不修改
set global validate_password.length=6;
补充知识:
1:查询mysql的密码策略,mysql5.7之后会自动安装此插件
mysql> SHOW VARIABLES LIKE 'validate_password%';
2、查询mysql路径
mysql> SHOW VARIABLES LIKE '%dir%';
修改完成后再次进行root密码修改即可成功
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
对于允许远程访问,有两种角色,一种是我们的root用户,还有一种是非root用户,即普通用户,从安全的角度,远程访问最后不要用root用户。
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
1、切换数据库
mysql> use mysql;
2、创建用户(user:用户名;%:任意ip,也可以指定;123456:登录密码;root默认就是localhost)
mysql> CREATE USER 'user'@'%' IDENTIFIED BY '123456';
3、授权,默认创建的用户权限是usage,就是无权限,只能登录而已,(all:所有权限,这里有select,update等等权限;后面的*.*:指定数据库.指定表,这里是所有;to后面就是你刚才创建的用户)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' IDENTIFIED BY '123456';
注意:用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,可执行以下命令(添加’WITH GRANT OPTION’):
mysql> GRANT ALL PRIVILEGES ON databasename.tablename TO 'username'@'host' IDENTIFIED BY 'password' WITH GRANT OPTION;
若出现报错:ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘IDENTIFIED BY ‘123456’ WITH GRANT OPTION’ at line 1
情况1:确保IDENTIFIED之前 ‘username’@'host’参数与创建用户参数保持一致,特别的例如root:
mysql> UPDATE USER SET Host = '%' where User = 'root'
情况2:Mysql8.0之后创建用户和赋予权限需要分离操作
# 创建用户
mysql> CREATE USER 'user'@'%' IDENTIFIED BY '123456';
#赋予权限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' WITH GRANT OPTION;
情况3:检查mysql语句的正确性
4、刷新权限
mysql> FLUSH PRIVILEGES;
注意:当使用Navicat等客户端连接mysql时出现:2059 - Authentication plugin ‘caching_sha2_password’ cannot be loaded: dlopen(…/Frameworks/caching_sha2_password.so, 2): image not found,这里的错误信息就是不支持身份认证方式,mysql 8.0默认使用的是caching_sha2_password身份验证机制,而在mysql 5.7之前的版本使用的是mysql_native_password验证机制,为此可执行以下命令进行修改:
mysql> ALTER USER 'user'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
mysql> SHOW VARIABLES LIKE 'character%';
编辑/etc/my.cnf
,[mysqld]节点增加以下代码:
[mysqld]
character_set_server=utf8
init-connect='SET NAMES utf8'
shell> systemctl enable mysqld
shell> systemctl daemon-reload
除安装过程外,其他步骤和yum方式安装相同,不再赘述
略
下载地址:https://dev.mysql.com/downloads/mysql/
shell> wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.19-1.el8.x86_64.rpm-bundle.tar
shell> tar -xvf mysql-8.0.19-1.el8.x86_64.rpm-bundle.tar
mysql-community-embedded-devel-8.0.19-1.el8.x86_64.rpm
mysql-community-libs-8.0.19-1.el8.x86_64.rpm
mysql-community-embedded-8.0.19-1.el8.x86_64.rpm
mysql-community-test-8.0.19-1.el8.x86_64.rpm
mysql-community-embedded-compat-8.0.19-1.el8.x86_64.rpm
mysql-community-common-8.0.19-1.el8.x86_64.rpm
mysql-community-devel-8.0.19-1.el8.x86_64.rpm
mysql-community-client-8.0.19-1.el8.x86_64.rpm
mysql-community-server-8.0.19-1.el8.x86_64.rpm
我们主要安装的是这四个(如果有需要也可以一并安装其它的):
mysql-community-libs-8.0.19-1.el8.x86_64.rpm
mysql-community-common-8.0.19-1.el8.x86_64.rpm
mysql-community-client-8.0.19-1.el8.x86_64.rpm
mysql-community-server-8.0.19-1.el8.x86_64.rpm
如果不想下载rpm-bundle,官网也提供单独的rpm下载链接
各rpm包是有依赖关系的,所以需要按照一定顺序进行安装,安装期间如果提示缺少哪些依赖也要先安装相应的包:
shell> rpm -ivh mysql-community-common-8.0.19-1.el8.x86_64.rpm
shell> rpm -ivh mysql-community-libs-8.0.19-1.el8.x86_64.rpm
shell> rpm -ivh mysql-community-client-8.0.19-1.el8.x86_64.rpm
shell> rpm -ivh mysql-community-server-8.0.19-1.el8.x86_64.rpm
还有一种简单的方式,可以自动处理各个包之间的依赖关系并自动下载缺少的依赖:
shell> yum install mysql-community-{server,client,common,libs}-*
注意:上面的yum install命令需要在tar解压之后的各个rpm包所在目录内执行,否则就变成yum方式安装了,需要配置MySQL的yum源并且速度很慢,还要当前机器支持外网访问
略
略
下载地址:https://dev.mysql.com/downloads/mysql/
查看系统是64位还是32位
shell> getconf LONG_BIT
shell> getconf WORD_BIT
32位的系统中int类型和long类型一般都是4字节,而64位的系统中int类型是4字节,但是long类型已变成了8字节。
在Linux系统中可用"getconf WORD_BIT"和"getconf LONG_BIT"获得word和long的位数。64位系统中分别得到32和64,而32位系统中分别得到32和32
shell> wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
MySQL依赖libaio库,如果没有先安装一下:
shell> yum install libaio
不需要登录的一个系统账号,启动MySQL服务时会使用该账号
shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-8.0.19-linux-glibc2.12-x86_64.tar.xz
shell> ln -s mysql-8.0.19-linux-glibc2.12-x86_64/ mysql
这一步并不是必须的,可以设置secure_file_priv的值指向该目录(用于限制数据导入导出操作的目录)
shell> cd mysql
shell> mkdir mysql-files
shell> chown mysql:mysql mysql-files
shell> chmod 750 mysql-files
shell> bin/mysqld --initialize --user=mysql
注意:如果初始化时报错如下:error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory,是因为libnuma没有安装(或者默认安装的是32位),我们这里需要64位的:
shell> yum install numactl.x86_64
执行完后重新初始化即可
初始化成功后返回结果中有一行包含初始密码,第一次登录时要用到它:
A temporary password is generated for root@localhost: 8M0ary878s*U
shell> bin/mysql_ssl_rsa_setup
shell> bin/mysqld_safe --user=mysql &
查看进程可以看到一些默认参数,可以在配置文件中修改这些参数
shell> ps -ef | grep mysql
root 14604 12719 0 00:03 pts/0 00:00:00 /bin/sh bin/mysqld_safe --user=mysql
mysql 14674 14604 0 00:03 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=VM_2_24_centos.err --pid-file=VM_2_24_centos.pid
避免每次执行mysql命令都要加上路径,在/etc/profile中添加:
export PATH=$PATH:/usr/local/mysql/bin
shell> cp support-files/mysql.server /etc/init.d/mysqld
shell> service mysqld start|stop|restart|status
shell> chkconfig --add mysqld
shell> chkconfig --list mysqld
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
其他配置与yum、rpm相同,不再赘述
待更新吧!!!
待更新吧!!!
待更新吧!!!