下载mysqld二进制包
[[email protected] ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz --2020-03-01 20:44:21-- https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz 正在解析主机 downloads.mysql.com (downloads.mysql.com)... 137.254.60.14 正在连接 downloads.mysql.com (downloads.mysql.com)|137.254.60.14|:443... 已连接。 已发出 HTTP 请求,正在等待回应... 302 Found 位置:https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz [跟随至新的 URL] --2020-03-01 20:44:28-- https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz 正在解析主机 cdn.mysql.com (cdn.mysql.com)... 104.75.165.42 正在连接 cdn.mysql.com (cdn.mysql.com)|104.75.165.42|:443... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:654430368 (624M) [application/x-tar-gz] 正在保存至: “mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz” 100%[=====================================================================================================================================================>] 654,430,368 522KB/s 用时 19m 39s 2020-03-01 21:04:07 (542 KB/s) - 已保存 “mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz” [654430368/654430368])
卸载mariadb或mysql系统自动安装的相关的包
[[email protected] ~]# rpm -qa | grep maradb| xargs -e --nodeps
解压移动指定目录,并重名mysql
[[email protected] ~]# mv mysql-5.7.18-linux-glibc2.5-x86_64 /usr/local/mysql
创建用户与组
[[email protected] ~]# groupadd mysql [[email protected] ~]# useradd -r -g mysql mysql
编写默认配置文件
cat > /etc/my.cnf <<EOF [mysqld] datadir=/usr/local/mysql/data socket=/usr/local/mysql/mysql.sock symbolic-links=0 [mysqld_safe] log-error=/usr/local/mysql/logs/error.log pid-file=/usr/local/mysql/mysql.pid [client] socket=/usr/local/mysql/mysql.sock EOF
创建数据目录,日志目录及文件,并授权修改用户属组
[[email protected] ~]# mkdir /usr/local/mysql/{data,logs} [[email protected] ~]# touch /usr/local/mysql/logs/error.log [[email protected] ~]# chown mysql:mysql -R /usr/local/mysql
初始化数据库
[[email protected] ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 2020-03-04T11:36:19.672944Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).2020-03-04T11:36:24.605282Z 0 [Warning] InnoDB: New log files created, LSN=45790 2020-03-04T11:36:25.355978Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2020-03-04T11:36:25.413157Z 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: 615a0cd8-5e0c-11ea- 95be-000c29168707.2020-03-04T11:36:25.423186Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened. 2020-03-04T11:36:25.433433Z 1 [Warning] [email protected] is created with an empty password ! Please consider switching off the --initialize-insecure option.
安全加固操作
[[email protected] ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ Generating a 2048 bit RSA private key .........+++ ....+++ writing new private key to ‘ca-key.pem‘ ----- Generating a 2048 bit RSA private key ........+++ ..............................+++ writing new private key to ‘server-key.pem‘ ----- Generating a 2048 bit RSA private key .........................................................................................................................+++ ............................................................................................................................................................................................... ...........................+++writing new private key to ‘client-key.pem‘ -----
拷贝启动脚本
[[email protected] ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
修改启动脚本路径
[[email protected] ~]# sed -i ‘46c basedir=/usr/local/mysql‘ /etc/init.d/mysqld [[email protected] ~]# sed -i ‘47c datadir=/usr/local/mysql/data‘ /etc/init.d/mysqld [[email protected] ~]# sed -i ‘63c mysqld_pid_file_path=/usr/local/mysql/data/mysqld.pid‘ /etc/init.d/mysqld
启动
[[email protected] ~]# /etc/init.d/mysqld start Starting MySQL.. SUCCESS!
修改密码
[[email protected] ~]# mysql -e "use mysql;alter user ‘root‘@‘localhost‘ identified by ‘123456‘;"
登录
[[email protected] ~]# mysql -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.7.18 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> exit Bye
查看版本
[[email protected] ~]# mysql -uroot -p123456 -e "select version()" mysql: [Warning] Using a password on the command line interface can be insecure. +-----------+ | version() | +-----------+ | 5.7.18 | +-----------+
[[email protected] ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.18 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> exit Bye
修改密码
[[email protected] ~]# mysql -e "use mysql;alter user ‘root‘@‘localhost‘ identified by ‘123456‘;"
登录
[[email protected] ~]# mysql -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.7.18 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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>
设置启动脚本
#Before=shutdown.target # 关机前操作 #User=mysql # 此用户必须存在,即为启动mysql的用户 cat > /usr/lib/systemd/system/mysqld.service <<EOF [mysqld] [Unit] Description=MySQL SourcePath=/etc/init.d/mysqld Before=shutdown.target [Service] User=mysql Type=forking ExecStart=/etc/init.d/mysqld start ExecStop=/etc/init.d/mysqld stop [Install] WantedBy=multi-user.target EOF
重读文件
[email protected] ~]# systemctl daemon-reload [[email protected] ~]# systemctl restart mysqld
原文地址:https://www.cnblogs.com/rdchenxi/p/12416660.html
时间: 2024-10-26 04:21:36