MySQL 介绍与二进制安装(5.7.20)
原创
©著作权归作者所有:来自51CTO博客作者xuqizhan的原创作品,请联系作者获取转载授权,否则将追究法律责任
一、mysql主流版本:
5.6,5.7,8.0
二、mysql如何下载:
官网下载地址:https://downloads.mysql.com/archives/community/
Product Version: ----->选择版本
Operating System: ------>选择操作系统
示例,如下图:
三、安装mysql数据库:
1,下载软件:
mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
2,上传到安装目录并解压:
mkdir /app
tar xf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
3,创建软连接:
cd /usr/local
ln -s /app/mysql-5.7.20-linux-glibc2.12-x86_64 mysql
4,设置环境变量:
vim /etc/profile 在文件末端添加以下内容:
export PATH=/usr/local/mysql/bin:$PATH
source /etc/profile (让设置生效)
5,创建mysql用户:
useradd -s /sbin/nologin -M (-s 指定shell,-M,不创建家目录)
6, 创建mysql数据目录和授权目录
mkdir -p /data/mysql
chown -R mysql.mysql /data/*
chown -R mysql.mysql /usr/local/mysql/
7, 初始化数据库:
第一种方法初始化:(默认创建管理账号密码)
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
[root@python app]# mysqld --initialize --user=mysql --basedir=/app/mysql --datadir=/data/mysql
2022-12-30T22:06:07.947423Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-12-30T22:06:09.314629Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-12-30T22:06:09.450709Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-12-30T22:06:09.643092Z 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: 2a663ec7-888e-11ed-b3cc-000c29a45835.
2022-12-30T22:06:09.644643Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-12-30T22:06:09.645568Z 1 [Note] A temporary password is generated for root@localhost: xrfzI/sT,0ie
出现以上信息,说明初始化成功!
如果出现以下报错:
[root@python app]# mysqld --initialize --user=mysql --basedir=/app/mysql --datadir=/data/mysql
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解决办法如下:
[root@python app]# yum install libaio-devel -y
重要说明:
5.7开始Mysql加入了全新的密码安全机制:
1.初始化完成后,会生成临时密码(显示到屏幕上,并且会往日志中记录一份)
2.密码复杂:长度超过12位,复杂度-字符混合组合
3.密码过期时间180天
注释:5.6初始化的区别
/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
第二种方法初始化:(管理账号空密码)
mysqld --initialize-insecure --user=mysql --basedir=/app/mysql --datadir=/data/mysql
[root@python mysql]# mysqld --initialize-insecure --user=mysql --basedir=/app/mysql --datadir=/data/mysql
2022-12-31T14:30:30.910937Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-12-31T14:30:32.173244Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-12-31T14:30:32.407972Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-12-31T14:30:32.631839Z 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: aeaf1c89-8917-11ed-828c-000c29a45835.
2022-12-31T14:30:32.634524Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-12-31T14:30:32.635882Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
8, 编辑mysql配置文件my.cnf,内容如下:
vim /etc/my.cnf
[mysqld]
user=mysql
basedir=/app/mysql
datadir=/data/mysql
server_id=6
port=3306
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock
prompt=3306[\\d]>
9,配置启动脚本,
第一种方法:(centos6,7)
cd /app/mysql/support-files
cp mysql.server /etc/init.d/mysqld
启动:
[root@python support-files]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/python.err'.
.. SUCCESS!
第二种方法:(centos7主要方法)
vim /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=mam:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/app/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE=5000
启动:
systemctl start mysqld.service
查看状态:
systemctl status mysqld.service
安装后给root超级管理员设置密码:
[root@python system]# mysqladmin -uroot -p password 123456
Enter password: (直接回车,因为之前为空密码)
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
查看用户基本信息:
5.6:
select user,host,password from mysql.user;
5.7:
select user,host,authentication_string from mysql.user;
desc mysql.user (可以查表所有的列信息:authentication_string)