安装路径:/application/mysql-5.7.18
1.前期准备
mysql依赖
1
2
|
libaio yum install -y libaio |
创建用户mysql,以该用户的身份执行mysql
1
|
useradd -s /bin/ false -M mysql |
下载mysql二进制包并解压
1
2
3
|
cd /tools wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz tar -zxf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /application/ |
切换到/application目录,将mysql文件夹名改短,给mysql目录做一个软链接
1
2
3
|
cd /application/ mv mysql-5.7.18-linux-glibc2.5-x86_64/ mysql-5.7.18 ln -s mysql-5.7.18/ mysql |
在mysql目录下创建mysql-files,该文件夹权限为750,递归设置mysql目录的所属组和所属用户
1
2
3
|
mkdir mysql/mysql-files chmod 750 mysql/mysql-files chown -R mysql:mysql mysql-5.7.18/ |
2.mysql目录内操作
1
|
cd mysql |
初始化数据库
会在mysql目录内生成一个data目录,存放数据库的目录
1
|
bin/mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data |
返回结果最后一行的末尾有随机密码,我的记下来:wa0I:1w?V--a
1
|
2017-04-28T02:49:00.853710Z 1 [Note] A temporary password is generated for root@localhost: wa0I:1w?V --a |
想设置默认密码为空则将--initialize选项替换为--initialize-insecure选项
1
|
bin/mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/application/mysql/data |
安装ssl
1
|
bin/mysql_ssl_rsa_setup --datadir /application/mysql/data/ |
指定data目录的路径
更改所属用户和组
1
2
|
chown -R root . chown -R mysql data mysql-files |
除了mysql目录下的data目录和mysql-files目录所属用户不变,其他所有文件的所属用户改为root
修改配置文件
1
2
3
4
|
sed -i 's/^datadir=\/var\/lib\/mysql/datadir=\/application\/mysql\/data/g' /etc/my.cnf sed -i 's/^socket=\/var\/lib\/mysql\/mysql.sock/socket=\/tmp\/mysql.sock/g' /etc/my.cnf sed -i 's/^log-error=\/var\/log\/mariadb\/mariadb.log/log-error=\/application\/mysql\/data\/err.log/g' /etc/my.cnf sed -i 's/^pid-file=\/var\/run\/mariadb\/mariadb.pid/pid-file=\/application\/mysql\/data\/mysql.pid/g' /etc/my.cnf |
等价于:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
vi /etc/my.cnf datadir=/application/mysql/data socket=/tmp/mysql.sock log-error=/application/mysql/data/err.log pid-file=/application/mysql/data/mysql.pid /etc/my.cnf Content: [mysqld] datadir=/application/mysql/data socket=/tmp/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=/application/mysql/data/err.log pid-file=/application/mysql/data/mysql.pid # # include all files from the config directory # !includedir /etc/my.cnf.d |
拷贝启动程序
1
|
cp support-files/mysql.server /etc/init.d/mysql |
将mysql的启动程序拷贝到/etc/init.d/目录下,以便启动程序
编辑启动文件,配置启动目录
方法一:
思路是给配置文件提供的变量进行赋值。较麻烦。
1
2
3
|
sed -i 's/^basedir=/basedir=\/application\/mysql/g' /etc/init.d/mysql sed -i 's/^datadir=/datadir=\/application\/mysql\/data/g' /etc/init.d/mysql sed -i 's/^mysqld_pid_file_path=/mysqld_pid_file_path=\/application\/mysql\/data\/mysql.pid/g' /etc/init.d/mysql |
等价于将45、46两行
1
2
3
|
basedir= datadir= mysqld_pid_file_path= |
替换成
1
2
3
|
basedir=/application/mysql datadir=/application/mysql/data mysqld_pid_file_path=/application/mysql/data/mysql.pid |
方法二(推荐):
思路是将脚本的默认地址(/usr/local/mysql)直接替换成自定义路径(/application/mysql),便不用给变量赋值
1
|
sed -i 's#/usr/local/mysql#/application/mysql#g' /etc/init.d/mysql |
到这里mysql安装完成可以正常启动
3.后期结尾
命令创建软链接
将mysql命令创建软链接到环境变量的目录,使用户可以在变量找到相应的命令
1
|
ln -s /application/mysql/bin/* /usr/ local /sbin |
登录mysql
1
|
[root@www mysql]# mysql -u root -p |
Enter password: #输入之前保存的随机密码drRR0
...
mysql> #成功登陆到mysql控制台
修改密码sql语句
1
|
mysql> alter user 'root' @ 'localhost' identified by 'NewPassWord' ; |
#将密码修改为NewPassWord
Query OK, 0 rows affected (0.01 sec) #修改成功
键入quit或Ctrl + d退出
1
2
3
4
5
6
|
mysql> quit Bye [root@www mysql]# or mysql> ^DBye [root@www mysql]# |
4.常用命令
进入mysql
1
|
mysql -u root -p |
启动mysql
1
|
service mysql start |
停止mysql
1
|
service mysql stop |
重启mysql
1
|
service mysql restart |
以上所述是小编给大家介绍的Mysql5.7.18版本(二进制包安装)自定义安装路径教程详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.2cto.com/database/201707/654706.html