文章目录
1 软件包准备
[root@localhost bin]# cat /etc/centos-release
CentOS Linux release 7.8.2003 (Core)
安装包下载MySQL
上传工具:winSCP
跨平台链接工具:Putty
2、安装MySql
1、使用winSCP上传
2、解压文件至/usr/local中
[root@localhost ~]# ls
anaconda-ks.cfg mysql-8.0.13-el7-x86_64.tar.gz
[root@localhost ~]# tar -zxvf mysql-8.0.13-el7-x86_64.tar.gz -C /usr/local/
mysql-8.0.13-el7-x86_64/bin/myisam_ftdump
......
[root@localhost local]# ls
bin etc games include lib lib64 libexec mysql-8.0.13-el7-x86_64 sbin share src
[root@localhost local]# mv mysql-8.0.13-el7-x86_64/ mysql
[root@localhost local]# ls
bin etc games include lib lib64 libexec mysql sbin share src
[root@localhost local]#
3、创建用户组及用户
[root@localhost local]# groupadd mysql
[root@localhost local]# useradd -g mysql mysql
4、创建数据目录
[root@localhost local]# cat /etc/profile | grep -i mysql # 此处为测试环境,将变量写入了配置文件中,
MYSQL_HOME=/usr/local/mysql
[root@localhost local]#
[root@localhost local]# echo $MYSQL_HOME
/usr/local/mysql
[root@localhost local]# mkdir -p $MYSQL_HOME/data
[root@localhost mysql]# ll
total 468
drwxr-xr-x 2 root root 4096 Oct 21 01:27 bin
drwxr-xr-x 2 root root 4096 Oct 21 01:43 data
drwxr-xr-x 2 root root 4096 Oct 21 01:27 docs
drwxr-xr-x 3 root root 4096 Oct 21 01:27 include
drwxr-xr-x 6 root root 4096 Oct 21 01:27 lib
-rw-r--r-- 1 7161 31415 335809 Oct 7 2018 LICENSE
-rw-r--r-- 1 7161 31415 101807 Oct 7 2018 LICENSE.router
drwxr-xr-x 4 root root 4096 Oct 21 01:27 man
-rw-r--r-- 1 7161 31415 687 Oct 7 2018 README
-rw-r--r-- 1 7161 31415 700 Oct 7 2018 README.router
drwxr-xr-x 28 root root 4096 Oct 21 01:27 share
drwxr-xr-x 2 root root 4096 Oct 21 01:27 support-files
[root@localhost mysql]# chown -R mysql:mysql /usr/local/mysql/
[root@localhost mysql]# ll
total 468
drwxr-xr-x 2 mysql mysql 4096 Oct 21 01:27 bin
drwxr-xr-x 2 mysql mysql 4096 Oct 21 01:43 data
drwxr-xr-x 2 mysql mysql 4096 Oct 21 01:27 docs
drwxr-xr-x 3 mysql mysql 4096 Oct 21 01:27 include
drwxr-xr-x 6 mysql mysql 4096 Oct 21 01:27 lib
-rw-r--r-- 1 mysql mysql 335809 Oct 7 2018 LICENSE
-rw-r--r-- 1 mysql mysql 101807 Oct 7 2018 LICENSE.router
drwxr-xr-x 4 mysql mysql 4096 Oct 21 01:27 man
-rw-r--r-- 1 mysql mysql 687 Oct 7 2018 README
-rw-r--r-- 1 mysql mysql 700 Oct 7 2018 README.router
drwxr-xr-x 28 mysql mysql 4096 Oct 21 01:27 share
drwxr-xr-x 2 mysql mysql 4096 Oct 21 01:27 support-files
[root@localhost lib]# mkdir -p /var/lib/mysql
[root@localhost lib]# chown mysql:mysql mysql/
5、初始化数据库
[root@localhost lib]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
2020-10-21T06:06:32.627048Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.13) initializing of server in progress as process 2304
2020-10-21T06:06:32.652269Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2020-10-21T06:06:35.241934Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: :zW/hmaN*5MB #密码
2020-10-21T06:06:36.906665Z 0 [System] [MY-013170] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.13) initializing of server has completed
[root@localhost lib]#[root@localhost lib]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost lib]# service mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
. SUCCESS!
[root@localhost rules.d]# service mysqld status
SUCCESS! MySQL running (2631)
6、配置MySQL自启动
[root@localhost init.d]# echo "service mysqld start" >> /etc/rc.local
[root@localhost init.d]# cat /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
service mysqld start
[root@localhost init.d]#
7、配置数据库
[root@localhost init.d]# cd $MYSQL_HOME
[root@localhost mysql]# ls
bin data docs include lib LICENSE LICENSE.router man README README.router share support-files
[root@localhost mysql]# cd bin/
[root@localhost bin]# ./mysql -u root -p
Enter password: # 此处密码为初始化时的回显密码
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.13
Copyright (c) 2000, 2018, 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> alter user 'root'@'localhost' identified with mysql_native_password by 'root'; # 修改root密码,修改为root
Query OK, 0 rows affected (0.10 sec)
mysql>update mysql.user set host='%' where user='root'; # 允许root远程链接
Query OK, 1 row affected (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges; # 刷新权限
Query OK, 0 rows affected (0.02 sec)
mysql> select user,plugin from mysql.user;
+------------------+-----------------------+
| user | plugin |
+------------------+-----------------------+
| root | mysql_native_password |
| mysql.infoschema | caching_sha2_password |
| mysql.session | caching_sha2_password |
| mysql.sys | caching_sha2_password |
+------------------+-----------------------+
4 rows in set (0.00 sec)
mysql>
8、链接数据库
FAQ
加入系统环境变量
[root@localhost bin]# vi /etc/profile
在最后加入:export PATH=$PATH:/usr/local/mysql/bin
执行链接命令
[root@localhost bin]# ln -s /usr/local/mysql/bin/mysql /usr/bin/
[root@localhost bin]# ls /usr/bin/mysql
/usr/bin/mysql
配置文件/etc/my.cnf
[root@localhost bin]# cat /etc/my.cnf
[client]
port=3306
socket=/var/lib/mysql/mysql.sock
default-character-set=utf8
[mysqld]
port=3306
user=mysql
socket=/var/lib/mysql/mysql.sock
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=10000
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 服务端使用的字符集默认为UTF8
character-set-server=utf8
# 默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
[mysql]
socket=/var/lib/mysql/mysql.sock
default-character-set=utf8
[root@localhost bin]#