通常企业环境中的MySQL安装使用两种方法
1、MySQL二进制安装包 mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz
2、MySQL源码包安装 mysql-5.6.33.tar.gz
区别:
二进制包安装方便不需要编译,直接解压就可以使用。但是二进制包要比源码包大。下面介绍一下MySQL二进制安装包的安装方法。(写的不好的地方大家多多指教)
1、添加mysql用户
useradd mysql -s /sbin/nologin -M
参数说明:-s /sbin/nologin 表示禁止该用户登录,
-M 不用创建家目录。
2、创建一个目录 mkdir /application
解释:二进制MySQL安装包默认安装在/usr/local下,这个目录可以不创建,这个只是个人习惯。运维工作人员的习惯就是走老路,避免新的坑。
3、解压安装包
tar -xvf mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz
4、将解压文件重命名
mv mysql-5.6.33-linux-glibc2.5-x86_64 /application/mysql-5.6.33
5、建立一个软连接
ln -s /application/mysql-5.6.33 /application/mysql
6、修改MySQL的所属主、所属组
chown -R mysql.mysql /application/mysql/
7、初始化数据库
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data/ --user=mysql
8、拷贝MySQL启动脚本
cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
9、给启动脚本加执行权限
chmod +x /etc/init.d/mysqld
10、二进制安装包默认安装路径是/usr/local,所以将脚本中的/usr/local替成/application
11、启动MySQL
/etc/init.d/mysqld start
Starting MySQL…………………………………. [确定]
如果报以下错误:
Starting MySQL.180223 17:46:06 mysqld_safe Directory ‘/var/lib/mysql’ for UNIX socket file don’t exists.
The server quit without updating PID file (/var/lib/mysql/i[FAILED]3fkghffrq11dqZ.pid).
![](/qrcode.jpg)
将/etc/my.cnf文件重新命名
12、检查MySQL是否真正启动
netstat -nplt | grep 3306
tcp 0 0 :::3306 :::* LISTEN 15064/mysqld
13、如果发现MySQL端口没有启动,请看/application/mysql/data/.err日志,根据报错信息在调试
vim /application/mysql/data/.err
14、设置MySQL开机自启动
chkconfig --add mysqld
chkconfig mysqld on
15、设置全局变量
vim /etc/profile
在文件最后加入
export PATH=/application/mysql/bin:$PATH
16、登录MySQL,这个时候不需要密码也可以登录,并且是root用户
[root@Jhyeliu data]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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>
17、如果出现下面这个情况,重新初始化数据。
ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)
1)、删除初始化的data目录 rm -rf /application/mysql/data
2)、重新初始化
18、MySQL安全加固
为root用户设置密码
mysqladmin -u root password 'password'
19、清理无用的库
drop database test ;
20、清理无用的用户
select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | jhyeliu |
| root | jhyeliu |
| | localhost |
| root | localhost |
+------+-----------+
drop user "root"@"::1";
Query OK, 0 rows affected (0.05 sec)
drop user ""@"localhost";
Query OK, 0 rows affected (0.00 sec)
drop user ""@"jhyeliu";
Query OK, 0 rows affected (0.00 sec)
drop user "root"@"jhyeliu";
Query OK, 0 rows affected (0.00 sec)
select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
flush privileges;
至此MySQL数据库就算基本完成了。