本文所有操作的前提均在服务器联网状态下,如果服务器是离线的各位可以找别的资料啦。其次,作为一个懒人,做事情当然能简单就不复杂。
本文涉及到的安装配置环节都是能精简的精简,能忽略的忽略,所以想熟悉底层的大佬,喜欢骚操作的叛逆少年以及强迫症患者慎入。废话少说,正片开始。
零,前情提要
本人腾讯云购买的服务器,系统:CentOS 7.5 64位。就算各位在阿里或其他云上购买都万变不离其宗,系统按稳定最新版安装。
一,安装JDK
通过yum命令安装jdk1.8(没yum的重置服务器系统吧,太旧了干啥都很费劲)
yum install -y java-1.8.0-openjdk
检查安装状态
java -version
检查结果如下
[root@umbrella]# java -version
openjdk version "1.8.0_262"
OpenJDK Runtime Environment (build 1.8.0_262-b10)
OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)
二,安装MySql
1.下载mysql安装包
随便找个目录,如下载到/mnt下(服务器网络不行的,在自己电脑上进这个下载地址下载了传到服务器也可)。
#下载mysql安装包
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
2.解压后移动并更名
#解压
tar xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
#移动到local文件夹下
mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql
3.创建data目录,不配置的话数据就存跑了(会存到/var/lib/mysql下)
#创建data文件夹
mkdir /usr/local/mysql/data
4.创建mysql的用户
#创建mysql属组
groupadd mysql
#创建mysql用户
useradd -r -g mysql mysql
5.给用户授权
#给用户授权
chown -R mysql:mysql /usr/local/mysql
chmod -R 755 /usr/local/mysql
准备安装啦。先把可能的问题展示一下,因为大多数新服都需要先装这个才能进行下去。
/usr/local/mysql/bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
#出现libaio字眼报错,安装libaio(必要,不装肯定报错)
yum install libaio-devel.x86_64
#其他可能错误,安装numactl(非必要,报错再装,我没装反正没报错)
yum -y install numactl
6.安装以及打印
#安装mysql
/usr/local/mysql/bin/mysqld --initialize --user=root --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
#输出结果如下
2020-09-16T05:49:48.112249Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-09-16T05:49:48.483353Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-09-16T05:49:48.547858Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-09-16T05:49:48.618009Z 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: 6e75f2bf-f7e0-11ea-a388-5254006f3b76.
2020-09-16T05:49:48.625672Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-09-16T05:49:48.626144Z 1 [Note] A temporary password is generated for root@localhost: S0#f:uy=s#&P
可以注意到最后一句很明显提到一个password,这个就是mysql自建的root账户自动生成的密码。要么记下来,要么后续改掉也行。
7.编辑mysql创建的配置文件/etc/my.cnf
首先文件里面可能已经有一些默认配置,就像下面一样,也许也不太一样,反正全部干掉(删掉)。
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
替换成
[mysqld]
datadir=/usr/local/mysql/data
port=3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
symbolic-links=0
max_connections=600
innodb_file_per_table=1
lower_case_table_names=1
8.启动mysql服务
#启动mysql服务
/usr/local/mysql/support-files/mysql.server start
9.设置开机自启
#将服务文件拷贝到init.d下并更名
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
#授权
chmod +x /etc/init.d/mysqld
#将服务添加到自启动
chkconfig --add mysqld
到这里mysql服务的安装就已经完成并投入使用了。剩下就是一些修改密码或开通外网访问等操作,依据实际情况看需不需要做。
10.登陆mysql
#连接本地mysql并准备键入密码
/usr/local/mysql/bin/mysql -uroot -p
#回车后应该是一个等待输入密码的状态,如下
Enter password:
#把刚在安装mysql最后生成的密码粘贴或手打到这里直接回车。因为密码做了处理。你输入或者粘贴但是看不到任何字符,此为正常现象,输入正确后就会进入mysql命令行,如下
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.24
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>
11.修改密码
#设置密码为123!@#qweQWE,密码要尽可能复杂且好记,反正也不会频繁输入,可以参考我的设置方式,数字字符字母大小写
set password for root@localhost = password('123!@#qweQWE');
#修改成功打印如下
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
12.开放远程连接
#进入mysql数据库
use mysql;
#更新root访问权限,改为全允许而不是仅本地(local)
update user set user.Host='%' where user.User='root';
#刷新权限
flush privileges;
#执行效果如下
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set user.Host='%' where user.User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql>
然后就可以拿自己的Navicat或者代码去连一下试试啦。如果仍然连不上需要上自己的云服务器所在控制台检查3306端口是否开通访问,开通方式各有各的不同,这里不再赘述。
未完待续
参考文献
Linux下安装mysql-5.7.24|JFAlex| 文章遵循 CC 4.0 BY-SA 版权协议