2. MYSQL 数据库的介绍安装
2.1 MySQL 5.7 二进制版本安装
2.1.1 下载并上传软件至/opt
2.1.2 解压软件
cd /opt && \
tar xf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
ln -s /opt/mysql-8.0.20-linux-glibc2.12-x86_64 /usr/local/mysql
2.1.3 用户的创建处理原始环境
rpm -qa |grep mariadb
yum remove mariadb-libs-5.5.65-1.el7.x86_64 -y
gzip /etc/my.cnf
yum install libaio-devel -y
useradd -s /sbin/nologin mysql
2.1.4 设置环境变量
cat >>/etc/profile<<EOF
export PATH=/usr/local/mysql/bin:$PATH
EOF
#加载环境变量
source /etc/profile
#check
mysql -V
mysql Ver 8.0.20 for Linux on x86_64 (MySQL Community Server - GPL)
2.1.5 授权
mkdir -p /data/3306/data
chown -R mysql. /data
2.1.6 准备配置文件
cat >/etc/my.cnf<<'EOF'
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/3306/data
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock
EOF
*2.1 7 初始化数据(创建系统数据)
(适用于mysql 5.7 8.0)
1.初始化方式:
[root@db01 ~]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3306/data
2.报错:
2.1报错信息1
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
2.1.1解决:
[root@db01 ~]# yum install -y libaio-devel
2.2报错信息2
2020-12-14T10:25:50.587761Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
2.2.1解决:
数据目录非空, 初始化会报错。 (执行第二遍数据库时,数据目录下非空,不能初始化,需要将数据目录清空)
[root@db01 /data/mysql/data]# \rm -rf /data/3306/data/*
[root@db01 ~]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql--datadir=/data/3306/data/
3.正确的输出:
[root@db01 ~]# mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/3306/data/
2019-06-13T04:21:27.706564Z 1 [Note] A temporary password is generated for root@localhost: =mrV)_16is4U
5.6- 初始化方式
/usr/local/mysql_install_db
2.1.8 准备启动脚本
#方式一加入servcie启动
[root@db01 ]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
/etc/init.d/mysqld start|stop|restart 或 service mysqld restart
#方式二配置systemd启动
[root@db01 ]# systemctl enable mysqld #开机自启动能(能将service启动方式 一键加systemctl启动)
#或者手动配置
cat >/etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man: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=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
EOF
2.1.9 启动错误
5.1 without updating PID 类似错误
#查看日志:
在哪?
/data/mysql/data/主机名.err
[ERROR] 上下文
#可能情况:
-/etc/my.cnf 配置文件错误
-/tmp/mysql.sock文件修改过 或 删除过
数据目录权限不是mysql
参数改错了
Warning: mysqld.service changed on disk. Run 'systemctl daemon-reload' to reload units.
2.1.10 初次设定密码
mysqladmin -uroot -p password
Enter password:
New password:
Confirm new password:
2.1.11 管理员用户密码忘记了?
1. 关闭数据库:
使用 systemctl stop mysqld
或 service mysqld stop
或 /etc/init.d/mysqld stop
或 mysql> shutdown
或 mysqladmin -u -p shutdown
2. 使用安全模式启动:
[root@db01 ~]# mysqld_safe --skip-grant-tables --skip-networking & #只允许本地无密码登录
[1] 4004
[root@db01 ~]# 2020-09-24T09:45:25.485161Z mysqld_safe Logging to '/data/mysql/data/db01.err'.
2020-09-24T09:45:25.521325Z mysqld_safe Starting mysqld daemon with databases from /data/mysql/data
--------------------
#或者
service mysqld start --skip-grant-tables --skip-networking
---------------------
3. 打开新窗口:
执行 mysql>
4. 修改密码:
mysql> flush privileges; #手动加载授权表
Query OK, 0 rows affected (0.00 sec)
mysql> alter user root@'localhost' identified by '123456' ; #修改密码
Query OK, 0 rows affected (0.00 sec)
5. 重启数据库到正常模式
[root@db01 ~]# systemctl restart mysqld.service
2.2 编译安装(了解)
2.2.1 下载并上传软件
mkdir -p /server/tools
cd /server/tools
mkdir /application
2.2.2 安装依赖包
yum install -y ncurses-devel libaio-devel
#(5)安装cmake
yum install cmake –y
2.2.3 环境清理
rpm -qa |grep mariadb
yum remove mariadb-libs-5.5.65-1.el7.x86_64 -y
2.2.4 创建用户
useradd -s /sbin/nologin -M mysql
2.2.5 编译安装
tar xf mysql-5.6.39.tar.gz
cd mysql-5.6.39
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.39 \
-DMYSQL_DATADIR=/application/mysql-5.6.39/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.39/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_ZLIB=bundled \
-DWITH_SSL=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_DEBUG=0
make && make install
2.2.6 配置并启动
(1)制作软连接:
ln -s /application/mysql-5.6.39/ /application/mysql
(2)拷贝配置文件到/etc:
cp support-files/my*.cnf /etc/my.cnf
(3)初始化数据库:
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data --user=mysql
(4)创建关键目录并设置权限:
mkdir -p /application/mysql/tmp
chown -R mysql.mysql /application/mysql/
(5)复制启动脚本到/etc/init.d/mysqld
cp support-files/mysql.server /etc/init.d/mysqld
授权
chmod 700 /etc/init.d/mysqld
####################################
centos6
开机自启动
chkconfig mysqld on
chkconfig --list mysqld
centos7
systemctl enable mysqld.service
systemctl is-enabled mysqld.service
###########################################
(6)启动数据库
/etc/init.d/mysqld start
netstat -lntup|grep 330
(7)配置环境变量
echo 'PATH=/application/mysql/bin/:$PATH' >>/etc/profile
tail -1 /etc/profile
source /etc/profile
echo $PATH
mysql> select user,host,password from mysql.user;
*2.3 面试题
2.3.1 MySQL 有哪些安装方式?
Centos: rpm(yum)、二进制、源码。
2.3.2 MySQL 5.6 、 5.7 安装过程有什么区别?
初始化
5.6 /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data --user=mysql
5.7 8.0 mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data