一、mysql 3 种安装方式(Linux):
- 通用二进制版: 解压即用(需要手动做一些配置:如:环境变量、软连接、初始化等)
- rpm, yum安装: 需要下载rpm包或者yum源,进行安装
- 源码包: 需编译安装,速度较慢,如果要研究mysql源码,可以使用此种方式(mysql是由c写成)
二、二进制包下载:
1、进入mysql官网:
- mysql官网地址:https://www.mysql.com/
- mysql二进制包下载地址:https://downloads.mysql.com/archives/community/
-
点击下载到本地
三、安装mysql软件:
1、环境准备:
1、虚拟机1台:
- 虚拟机软件:VMware
- Linux系统:centos 6.5
- 连接Linux工具:xshell
2、清理历史遗留:
- 检查命令:rpm -qa | grep [name] # 作用:检查是否有历史mysql,如果有需要先清理掉
- 实例1:
-
[root@localhost bin]# rpm -qa | grep mysql mysql-community-libs-5.7.9-1.el6.x86_64 mysql-community-client-5.7.9-1.el6.x86_64 mysql-community-server-5.7.9-1.el6.x86_64 mysql-community-common-5.7.9-1.el6.x86_64
-
- 实例1:
-
删除命令:rpm -ev --nodeps [name]
- 示例:
[root@localhost bin]# rpm -ev --nodeps mysql-community-client-5.7.9-1.el6.x86_64
- 示例:
3、创建用户和组
创建用户和组:
[root@localhost ~]# useradd mysql -s /sbin/nologin useradd: user 'mysql' already exists
验证是否成功:
[root@localhost ~]# id mysql uid=27(mysql) gid=27(mysql) groups=27(mysql)
4、创建相关目录:
1、国际标准目录:
一般地,mysql的相关目录,会创建在 /usr/local/ 下
2、自定义目录:
- 软件目录: mkdir -p mysql/database
- 数据目录: mkdir -p mysql/data/3306
- 日志目录: mkdir -p mysql/log/3306
ps: 实际工作中,最好是将数据、日志、软件分别放在不同的磁盘上,自己虚拟机玩的话,可以放在同一磁盘上。
5、设置权限:
- 添加权限: [root@localhost ~]# chown mysql.mysql -R mysql/data mysql/database mysql/log
- 查看权限:
[root@localhost ~]# ll mysql total 12 drwxr-xr-x. 3 mysql mysql 4096 Jan 10 10:02 data drwxr-xr-x. 2 mysql mysql 4096 Jan 10 09:57 database drwxr-xr-x. 3 mysql mysql 4096 Jan 10 10:02 log
2、上传【二进制安装包】:
- 二进制安装包:从mysql官网下载的二进制包,本次用的是window系统,所以,二进制包放在Windows磁盘中
- 上传工具:xshell (使用sftp传输,在此就不再赘述,百度下,有很多教程可用)
- ps: 上传的目的文件夹:mysql/database/ 【上传目录,可以视自己情况而定,只要能够找得到即可】
3、Linux下-解压二进制包:
[root@localhost database]# pwd /root/mysql/database [root@localhost database]# tar -xvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
解压过程... mysql-5.7.28-linux-glibc2.12-x86_64/support-files/mysql.server mysql-5.7.28-linux-glibc2.12-x86_64/docs/INFO_BIN mysql-5.7.28-linux-glibc2.12-x86_64/docs/INFO_SRC
解压结束... [root@localhost database]# [root@localhost database]# [root@localhost database]# ll total 707696 drwxr-xr-x. 9 root root 4096 Jan 10 10:32 mysql-5.7.28-linux-glibc2.12-x86_64 -rw-r--r--. 1 root root 724672294 Jan 10 10:27 mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
4、创建软链接:
- 命令: ln -s [源文件或目录][目标文件或目录]
- 示例:
[root@localhost database]# pwd /root/mysql/database [root@localhost database]# ln -s mysql-5.7.28-linux-glibc2.12-x86_64 mysql [root@localhost database]# ll total 707696 lrwxrwxrwx. 1 root root 35 Jan 10 10:40 mysql -> mysql-5.7.28-linux-glibc2.12-x86_64 drwxr-xr-x. 9 root root 4096 Jan 10 10:32 mysql-5.7.28-linux-glibc2.12-x86_64 -rw-r--r--. 1 root root 724672294 Jan 10 10:27 mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz [root@localhost database]#
5、设置环境变量:
- vim 到 环境变量文件: [root@localhost mysql]# vim /etc/profile
- 在文件末尾添加:
export PATH=/root/mysql/database/mysql/bin:$PATH
- 保存更改后退出 :wq
- 生效配置: source /etc/profile
- 检查mysql环境变量,设置是否成功:
[root@localhost mysql]# mysql -V mysql Ver 14.14 Distrib 5.7.28, for linux-glibc2.12 (x86_64) using EditLine wrapper
出现上述情况,表示环境变量设置成功。
四、初始化系统库表
1、初始化系统库表
- 初始化命令:
mysqld --initialize-insecure --user=mysql --basedir=/root/mysql/database/mysql --datadir=/root/mysql/data/3306
- 可能会遇到的报错:
- 用户权限问题:
[root@localhost mysql]# mysqld --initialize-insecure --user=mysql --basedir=/root/mysql/database/mysql --datadir=/root/mysql/data/3306 mysqld: Can't change dir to '/root/mysql/data/3306/' (Errcode: 13 - Permission denied) 2021-01-10T21:17:58.027014Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2021-01-10T21:17:58.028906Z 0 [ERROR] failed to set datadir to /root/mysql/data/3306/ 2021-01-10T21:17:58.028950Z 0 [ERROR] Aborting
出现此问题的根本原因:--user=mysql, mysql用户没有进入 /root/mysql/data/3306的权限。
- 解决方案:
- 将--user=mysql 改为 --user=root
- 在【环境准备步骤中】,将【datadir】的文件夹设置在根目录下/data/3306 ,并将该文件夹的属组设置为mysql ,具体【用户和组】,详见上述:【环境准备】-》【创建用户和组】和【添加权限】
- 解决方案:
- 依赖包缺失问题:
- 解决方案: yum install -y libaio-devel
- 用于存储数据的文件夹路径下,已经有文件存在:
解决方案:
- 删除指定文件夹下的多余文件:
rm -rf /root/mysql/data/3306/
- 删除指定文件夹下的多余文件:
- 用户权限问题:
- 执行成功:
[root@localhost mysql]# mysqld --initialize-insecure --user=root --basedir=/root/mysql/database/mysql --datadir=/root/mysql/data/3306 2021-01-10T21:21:18.198822Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2021-01-10T21:21:18.537972Z 0 [Warning] InnoDB: New log files created, LSN=45790 2021-01-10T21:21:18.587303Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2021-01-10T21:21:18.667374Z 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: c771aff5-5389-11eb-8d05-000c29f04ed2. 2021-01-10T21:21:18.668309Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2021-01-10T21:21:19.012897Z 0 [Warning] CA certificate ca.pem is self signed. 2021-01-10T21:21:19.096820Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
- 可能会遇到的报错:
-
扩展:
-
mysql-5.6 版本 初始化方法:
/root/mysql/database/mysql/scripts/mysql_install_db --initialize-insecure --user=mysql --basedir=/root/mysql/database/mysql --datadir=/root/mysql/data/3306
PS:
- mysql-5.7 版本 初始化方法:
- 方法1: mysqld --initialize --user=mysql --basedir=/root/mysql/database/mysql --datadir=/root/mysql/data/3306
- 使用mysql前,重置密码:
- 方法2:mysqld --initialize-insecure --user=mysql --basedir=/root/mysql/database/mysql --datadir=/root/mysql/data/3306
- 不会有临时密码,可以直接登录mysql
- 方法1: mysqld --initialize --user=mysql --basedir=/root/mysql/database/mysql --datadir=/root/mysql/data/3306
-
2、基础配置文件设置:
cat > /etc/my.cnf <<EOF [mysqld] user=root basedir=/root/mysql/database/mysql datadir=/root/mysql/data/3306 server_id=6 port=3306 socket=/tmp/mysql.sock [mysql] socket=/tmp/mysql.sock EOF
3、准备mysql启动脚本:
-
cd 到指定目录:
[root@localhost mysql]# cd /root/mysql/database/mysql/support-files/
- 拷贝mysql的启动脚本 至 Linux系统软件管理目录中:
[root@localhost support-files]# cp mysql.server /etc/init.d/mysqld
-
修改 mysqld :
修改内容: basedir=/opt/module/mysql/software/mysql_5.7.28 datadir=/opt/module/mysql/data/3306
4、启动mysql服务:
- centos6 and centos7 通用的启动mysql服务的方法:
[root@localhost support-files]# service mysqld start Starting MySQL.Logging to '/root/mysql/data/3306/localhost.localdomain.err'. . SUCCESS!
5、登录mysql并设置用户名及密码:
- 登录mysql:
[root@localhost support-files]# mysql Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 4 Server version: 5.7.28 MySQL Community Server (GPL) 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>
- 设置root用户的密码:
mysql> set password for 'root'@'localhost'=password('root'); Query OK, 0 rows affected, 1 warning (0.00 sec)
- 添加新用户及密码:
mysql> GRANT SELECT ON*.* TO 'test3'@localhost IDENTIFIED BY 'test3'; Query OK, 0 rows affected, 1 warning (0.00 sec)
实际演示案例:
mysql> GRANT SELECT ON*.* TO 'test3'@localhost IDENTIFIED BY 'test3'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> exit Bye [root@localhost support-files]# mysql -utest3 -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 7 Server version: 5.7.28 MySQL Community Server (GPL) 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec)