S_lion's Studio

普通用户安装mysql

字数统计: 1k阅读时长: 5 min
2021/10/31 Share

Mysql是最为常用的关系型数据库,关于安装网上大多都是通过root用户直接来安装的,真实场景中,多数情况是不会给root用户的,要不是直接向甲方申请mysql,要不是给普通用户自己安装。

下面演示如何通过普通用户完成mysql服务的搭建。

环境描述

主机名 ip地址 系统版本 系统资源
slions_pc2 192.168.100.11 CentOS Linux release 7.6.1810 cpu:2 mem:4GB

创建用户

后续通过slions用户来完成mysql环境搭建。

1
2
3
[root@slions_pc2 ~]groupadd -g 7295 slions && useradd -u 7295 -g 7295 -s /bin/bash slions && echo "slions:slions" | chpasswd
[root@slions_pc2 ~]# id slions
uid=7295(slions) gid=7295(slions) 组=7295(slions)

下载安装包

访问mysql官网,当前最新的版本为8.0.27,想要下载历史版本需点击标注处。

此次我们安装Mysql 5.7.36

选择tar.gz离线包

不登录,直接下载到本地

安装Mysql

使用slions用户登录服务器,将下载好的mysql离线包拷贝到服务器。

1
2
[slions@slions_pc2 ~]$ ls
mysql-5.7.36-el7-x86_64.tar.gz

解压

1
2
[slions@slions_pc2 ~]$ tar -zxvf mysql-5.7.36-el7-x86_64.tar.gz
[slions@slions_pc2 ~]$ mv mysql-5.7.36-el7-x86_64 mysql

创建数据、日志与socket目录以及配置文件目录

1
2
3
4
5
6
7
[slions@slions_pc2 ~]$ mkdir -p data/mysql/{data,logs,tmp} && mkdir conf
[slions@slions_pc2 ~]$ ls -l
总用量 715496
drwxrwxr-x. 2 slions slions 6 10月 31 13:50 conf
drwxrwxr-x. 3 slions slions 6 10月 31 11:08 data
drwxrwxr-x. 9 slions slions 129 10月 31 11:05 mysql
-rw-rw-r--. 1 slions slions 732667171 10月 31 11:02 mysql-5.7.36-el7-x86_64.tar.gz

编写mysql配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
[slions@slions_pc2 ~]$ cat >> conf/my.cnf << EOF
[client]
port = 33060
socket = /home/slions/data/mysql/tmp/mysql.sock
[mysqld]
user = slions
basedir = /home/slions/mysql
datadir = /home/slions/data/mysql/data
port = 33060
socket = /home/slions/data/mysql/tmp/mysql.sock
pid-file = /home/slions/data/mysql/tmp/mysqld.pid
tmpdir = /home/slions/data/mysql/tmp
skip_name_resolve = 1
symbolic-links=0
max_connections = 2000
group_concat_max_len = 1024000
sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
lower_case_table_names = 1
log_timestamps=SYSTEM
character-set-server = utf8
interactive_timeout = 1800
wait_timeout = 1800
max_allowed_packet = 32M
binlog_cache_size = 4M
sort_buffer_size = 2M
read_buffer_size = 4M
join_buffer_size = 4M
tmp_table_size = 96M
max_heap_table_size = 96M
max_length_for_sort_data = 8096
#logs
server-id = 1003306
log-error = /home/slions/data/mysql/logs/error.log
slow_query_log = 1
slow_query_log_file = /home/slions/data/mysql/logs/slow.log
long_query_time = 3
log-bin = /home/slions/data/mysql/logs/binlog
binlog_format = row
expire_logs_days = 15
log_bin_trust_function_creators = 1
relay-log = /home/slions/data/mysql/logs/relay-bin
relay-log-recovery = 1
relay_log_purge = 1
#innodb
innodb_file_per_table = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group = 2
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_flush_neighbors = 0
innodb_flush_method = O_DIRECT
innodb_autoinc_lock_mode = 2
innodb_read_io_threads = 8
innodb_write_io_threads = 8
innodb_buffer_pool_size = 2G
EOF

初始化数据库

1
[slions@slions_pc2 ~]$ ./mysql/bin/mysqld --defaults-file=/home/slions/conf/my.cnf --initialize --user=slions --basedir=/home/slions/mysql --datadir=/home/slions/data/mysql/data

获取root密码

1
[slions@slions_pc2 ~]$ cat data/mysql/logs/error.log |grep password

启动mysql

1
[slions@slions_pc2 ~]$ ./mysql/bin/mysqld_safe --defaults-file=/home/slions/conf/my.cnf --user=slions &

设置环境变量

1
[slions@slions_pc2 ~]$ echo "PATH=$PATH:/home/slions/mysql/bin" >> ~/.bash_profile

登录mysql

1
2
3
4
5
6
7
[slions@slions_pc2 ~]$ mysql -uroot -P33060 -p -S /home/slions/data/mysql/tmp/mysql.sock
# 修改默认的root密码及远程登录权限
mysql> alter user 'root'@'localhost' identified by 'admin123';
mysql> flush privileges;
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'admin123' WITH GRANT OPTION;
mysql> flush privileges;
mysql> exit

此时已经可以通过其他客户端访问该服务了。

设置命令别名

因为我们改了默认的mysql.sock路径,导致每次客户端连接时都要指明mysql.sock,不然会报错

1
2
[slions@slions_pc2 ~]$ mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

可以配置个命令别名,之后就可以正常使用了

1
2
3
[slions@slions_pc2 ~]$ alias mysql='mysql -S /home/slions/data/mysql/tmp/mysql.sock'
[slions@slions_pc2 ~]$ mysql
ERROR 1045 (28000): Access denied for user 'slions'@'localhost' (using password: NO)

配置开机自启

配置开机自启需要root权限。

systemd service方式

编写mysql服务启动文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@slions_pc2 ~]# cat >> /usr/lib/systemd/system/slions-mysql.service << EOF
[Unit]
Description=Slions Mysql Server
After=network.service
[Install]
WantedBy=multi-user.target
[Service]
User=slions
Group=slions
Type=forking
TimeoutSec=0
ExecStart=/home/slions/mysql/bin/mysqld --defaults-file=/home/slions/conf/my.cnf --basedir=/home/slions/mysql --datadir=/home/slions/data/mysql/data --daemonize
ExecStop=/bin/kill $MAINPID
LimitNOFILE=65535
Restart=on-failure
RestartSec=10
RestartPreventExitStatus=1
PrivateTmp=false
EOF

启动服务并设置开机自启

1
2
3
4
5
6
[root@slions_pc2 ~]# pkill -9 mysql
[root@slions_pc2 ~]# systemctl enable slions-mysql.service
[root@slions_pc2 ~]# systemctl start slions-mysql.service
[root@slions_pc2 ~]# ps -elf|grep mysql
5 S slions 11905 1 0 80 0 - 835134 poll_s 12:31 ? 00:00:08 /home/slions/mysql/bin/mysqld --defaults-file=/home/slions/conf/my.cnf --basedir=/home/slions/mysql --datadir=/home/slions/data/mysql/data --socket=/home/slions/data/mysql/tmp/mysql.sock --pid-file=/home/slions/data/mysql/tmp/mysqld.pid --daemonize
4 S root 12398 9776 0 80 0 - 28182 pipe_w 14:56 pts/0 00:00:00 grep --color=auto mysql
CATALOG
  1. 1. 环境描述
  2. 2. 创建用户
  3. 3. 下载安装包
  4. 4. 安装Mysql
  5. 5. 登录mysql
  6. 6. 设置命令别名
  7. 7. 配置开机自启
    1. 7.1. systemd service方式