目录
背景
在项目开发过程中,有些甲方提供的linux服务器,对安全性要求控制得较严格,会可能不提供root用户给我们用,此时按传统的直接yum或源码安装后mysql然后再启动的方式就不可行,此时需要在非root用户下进行mysql的安装。接下来通过二进制安装方式按步骤进行操作安装。
一、下载二进制mysql安装文件
先下安装包,到mysql官网https://dev.mysql.com/downloads/mysql/选好安装包版本,点击右边链接Looking for previous GA versions,Linux-Generic, x86-64位版本
二、开始进行安装
1.解压文件
以安装在/home/liu账号下为例
##解压
tar -xvf mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /home/liu
cd /home/liu
mv mysql-5.7.34-linux-glibc2.12-x86_64 mysql-5.7.34
##建好配置文件与mysql数据存储目录
mkdir /home/liu/mysql-5.7.34/data
mkdir /home/liu/mysql-5.7.34/conf
2.配置文件准备
cat > /home/liu/mysql-5.7.34/conf/my.cnf << EOF
[client]
default-character-set=utf8mb4
[mysql]
port = 3306
socket = /home/liu/mysql-5.7.34/data/mysql.sock
default-character-set=utf8mb4
[mysqld]
port = 3306
default_storage_engine=InnoDB
basedir = /home/liu/mysql-5.7.34
datadir = /home/liu/mysql-5.7.34/data
socket = /home/liu/mysql-5.7.34/data/mysql.sock
character-set-client-handshake = FALSE
character-set-server=utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
max_connections = 2000
max_allowed_packet = 128M
innodb_file_per_table = 1
tmp_table_size = 134217728
max_heap_table_size = 134217728
lower_case_table_names=1
log-bin = mysql-bin
max_binlog_size = 1024M
expire_logs_days = 1
log_slave_updates = 1
server-id = 1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
EOF
3.修改环境变量
##配置环境变量
cat >> /home/liu/.bashrc << EOF
export PATH=/home/liu/mysql-5.7.34/bin:\$PATH
EOF
##生效
source .bashrc
##将sock文件链接到/tmp目录,默认mysql是读/tmp下的
ln -s /home/liu/mysql-5.7.34/data/mysql.sock /tmp/mysql.sock
4.初始化数据库
mysqld --defaults-file=/home/liu/mysql-5.7.34/conf/my.cnf --user=liu --initialize
##执行命令
mysqld --defaults-file=/home/liu/mysql-5.7.34/conf/my.cnf --user=liu --initialize
##执行日志如下:
[liu@localhost ~]$ mysqld --defaults-file=/home/liu/mysql-5.7.34/conf/my.cnf --user=liu --initialize
2021-04-30T05:54:25.918199Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 10000)
2021-04-30T05:54:25.918287Z 0 [Warning] Changed limits: max_connections: 214 (requested 2000)
2021-04-30T05:54:25.918291Z 0 [Warning] Changed limits: table_open_cache: 400 (requested 2000)
2021-04-30T05:54:25.918421Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-04-30T05:54:26.083506Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-04-30T05:54:26.110619Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-04-30T05:54:26.180108Z 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: 85420f95-a978-11eb-b8f9-080027473f54.
2021-04-30T05:54:26.182418Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-04-30T05:54:26.682357Z 0 [Warning] CA certificate ca.pem is self signed.
2021-04-30T05:54:26.782180Z 1 [Note] A temporary password is generated for root@localhost: 7a8a-VU>owtq
5.启动数据库
/home/liu/mysql-5.7.34/bin/mysqld_safe --defaults-file=/home/liu/mysql-5.7.34/conf/my.cnf --user=liu &
/home/liu/mysql-5.7.34/bin/mysqld_safe --defaults-file=/home/liu/mysql-5.7.34/conf/my.cnf --user=liu &
##启动日志如下:
[liu@localhost ~]$ 2021-04-30T05:58:21.373237Z mysqld_safe Logging to '/home/liu/mysql-5.7.34/data/localhost.localdomain.err'.
2021-04-30T05:58:21.399471Z mysqld_safe Starting mysqld daemon with databases from /home/liu/mysql-5.7.34/data
##查看ps -ef|grep mysqld,可看到mysql正常启动
[liu@localhost ~]$ ps -ef|grep mysqld
liu 13020 12906 0 13:58 pts/0 00:00:00 /bin/sh /home/liu/mysql-5.7.34/bin/mysqld_safe --defaults-file=/home/liu/mysql-5.7.34/conf/my.cnf --user=liu
liu 13381 13020 0 13:58 pts/0 00:00:00 /home/liu/mysql-5.7.34/bin/mysqld --defaults-file=/home/liu/mysql-5.7.34/conf/my.cnf --basedir=/home/liu/mysql-5.7.34 --datadir=/home/liu/mysql-5.7.34/data --plugin-dir=/home/liu/mysql-5.7.34/lib/plugin --log-error=localhost.localdomain.err --pid-file=localhost.localdomain.pid --socket=/home/liu/mysql-5.7.34/data/mysql.sock --port=3306
liu 13413 12906 0 14:00 pts/0 00:00:00 grep --color=auto mysqld
6.登陆测试
mysql -uroot -hlocalhost -p
##
mysql -uroot -hlocalhost -p
执行命令后输入上面输出的root密码7a8a-VU>owtq可看到登陆正常
##日志如下
[liu@localhost ~]$ mysql -uroot -hlocalhost -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.34-log
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>
至此,mysql通过二进制安装成功。
其他
1.修改root密码
通过alert user来执行
###修改root密码,root登录后执行:
alter user user() identified by "123456";
flush privileges;
##日志如下:
mysql> alter user user() identified by "123456";
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
2.如果忘记了root密码
如果忘记了root密码,以–skip-grant-tables重启mysql并进行修改
/home/liu/mysql-5.7.34/bin/mysqld_safe --defaults-file=/home/liu/mysql-5.7.34/conf/my.cnf --skip-grant-tables &
##此时直接不要密码能进入
mysql -uroot -hlocalhost
##然后更新自己想要的密码
update mysql.user set authentication_string=password("123456") where user='root' and host='localhost';
flush privileges;
##最后再停止数据按正常启动数据库让密码校验生效
3.停止数据库
可通过直接杀进程执行,但不建议这样做,容易在生产环境中出现不可知错误导致一些问题。
可以采用mysqladmin -shutdown的方式
/home/liu/mysql-5.7.34/bin/mysqladmin -u root -p shutdown
##日志如下:
[liu@localhost ~]$ /home/liu/mysql-5.7.34/bin/mysqladmin -u root -p shutdown
Enter password:
2021-04-30T06:13:06.147682Z mysqld_safe mysqld from pid file /home/liu/mysql-5.7.34/data/localhost.localdomain.pid ended
[1]+ Done /home/liu/mysql-5.7.34/bin/mysqld_safe --defaults-file=/home/liu/mysql-5.7.34/conf/my.cnf --user=liu
[liu@localhost ~]$
4.远程授权其他普通用户
%表示所有ip,也可以指定具体的ip,精细控制能访问的ip
grant all privileges on *.* to 'webuser'@'%' identified by '123456' with grant option;
flush privileges;