Linux系统中MySQL 5.7.26的安装及初始化操作
1. MySQL下载
官网下载地址: https://downloads.mysql.com/archives/community/
2. 将下载的软件上传至服务器,进行解压及安装前的环境部署
[root@templates ~]# cd /server/tools/ #<==== 软件包上传至/server/tools目录下 [root@templates tools]# ll total 629756 -rw-r--r-- 1 root root 644869837 Mar 2 17:24 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz [root@templates tools]# tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz #<==== 将软件包进行解压 [root@templates tools]# ll total 629756 drwxr-xr-x 9 root root 129 Mar 2 17:25 mysql-5.7.26-linux-glibc2.12-x86_64 -rw-r--r-- 1 root root 644869837 Mar 2 17:24 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz [root@templates tools]# mkdir /application #<==== 创建安装目录 [root@templates tools]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql #<==== 将解压后的mysql文件移动至安装目录下 [root@templates tools]# yum remove mariadb-libs -y #<==== 检查系统中是否存在mariadb相关软件,若存在进行卸载 Loaded plugins: fastestmirror No Match for argument: mariadb-libs No Packages marked for removal [root@templates tools]# rpm -qa| grep mariadb #<==== 检查系统中是否存在mariadb相关软件 [root@templates tools]# useradd -s /sbin/nologin -M mysql #<==== 创建mysql虚拟用户 [root@templates tools]# echo "export PATH=/application/mysql/bin:$PATH" >> /etc/profile #<==== 将mysql的安装目录加入环境变量 [root@templates tools]# source /etc/profile #<==== 使环境变量生效 [root@templates tools]# mysql -V #<==== 检查环境变量是否生效 mysql Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using EditLine wrapper
3. 创建数据存储路径,并进行授权。企业中建议使用独立的一块硬盘用来存储数据或者挂存储服务器。
[root@templates tools]# mkdir -p /data/mysql/data #<==== 创建数据存储路径 [root@templates tools]# chown -R mysql.mysql /data #<==== 授权 [root@templates tools]# chown -R mysql.mysql /application/ #<==== 授权
4. 初始化数据库
方式一:利用 initialize 参数初始化同时,给数据库root@localhost用户设置一个临时密码,该密码过期时间为180天,12位字符(包括大小写字母、数字和字符组成)。
[root@templates ~]# mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data 2021-03-02T09:51:00.108059Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2021-03-02T09:51:00.479634Z 0 [Warning] InnoDB: New log files created, LSN=45790 2021-03-02T09:51:00.560426Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2021-03-02T09:51:00.631505Z 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: cb7034eb-7b3c-11eb-b8e8-005056b484f7. 2021-03-02T09:51:00.634190Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2021-03-02T09:51:00.635779Z 1 [Note] A temporary password is generated for root@localhost: Juy:kr/fp9LB
方式二:利用 initialize-insecure 参数,无临时密码。
[root@templates ~]# mysqld --initialize-insecure --basedir=/application/mysql --datadir=/data/mysql/data 2021-03-02T09:57:59.911031Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2021-03-02T09:58:00.396741Z 0 [Warning] InnoDB: New log files created, LSN=45790 2021-03-02T09:58:00.574981Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2021-03-02T09:58:00.688947Z 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: c5cfe2b3-7b3d-11eb-81fb-005056b484f7. 2021-03-02T09:58:00.691947Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2021-03-02T09:58:00.693066Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
【注意】如遇到以下报错,解放方法如下
报错: mysqld:error while loading shared libraries:libaio.so.1:cannot open shared object file: No such file or directory
处理方法:
yum install libaio-devel -y
5. 编辑数据库配置文件
[root@templates ~]# vim /etc/my.cnf [mysqld] user=mysql basedir=/application/mysql datadir=/data/mysql/data socket=/tmp/mysql.sock server_id=6 port=3306 [mysql] socket=/tmp/mysql.sock
6. 启动数据库
方式一:sys -v 方式启动
[root@templates ~]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld [root@templates data]# service mysqld start Starting MySQL. SUCCESS! [root@templates data]# netstat -lntup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1356/sshd tcp6 0 0 :::3306 :::* LISTEN 1083/mysqld tcp6 0 0 :::22 :::* LISTEN 1356/sshd [root@templates data]# netstat -lntup|grep 3306 tcp6 0 0 :::3306 :::* LISTEN 1083/mysqld
[root@templates data]# service mysqld stop
方式二:systemd方式启动
[root@templates data]# vim /etc/systemd/system/mysqld.service [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=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf LimitNOFILE=5000
[root@templates data]# systemctl start mysqld [root@templates data]# netstat -lntup|grep 3306 tcp6 0 0 :::3306 :::* LISTEN 1224/mysqld [root@templates data]# systemctl stop mysqld [root@templates data]# netstat -lntup|grep 3306
【注意】如果用sys-v方式启动数据库,就必须使用该方式进行停止。反之则一样。不能用两种方式中的一种进行启动,而另一种进行关闭数据库!
7. 管理员密码设定
[root@templates data]# mysqladmin -uroot -p password xxx #<==== 设置数据库管理员密码 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.
8. 数据库登录验证
[root@templates data]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.26 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> select user,host from mysql.user; +---------------+-----------+ | user | host | +---------------+-----------+ | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | +---------------+-----------+ 3 rows in set (0.00 sec)
部署完成!