CentOS6.x 下 LNMP环境搭建(一、安装 MySQL)
1. 创建用户
# groupadd mysql
# useradd -s /sbin/nologin -g mysql -M mysql
---- 检查 ----
# tail -1 /etc/passwd
mysql:x:501:501::/home/mysql:/sbin/nologin
# id mysql
uid=501(mysql) gid=501(mysql) groups=501(mysql)
---- 检查 ----
注:也可以用一条命令代替【useradd mysql -s /sbin/nologin -M】
2. 解压、配置
# cd /root/src
# tar -zxvf mysql-5.5.50-linux2.6-x86_64.tar.gz
# mv mysql-5.5.50-linux2.6-x86_64 /lnmp/server/mysql-5.5.50 && cd /lnmp/server
# ls -l
# ln -s /lnmp/server/mysql-5.5.50/ mysql
# ls -l
# cd mysql
# ls -l support-files/*.cnf <------- 查看所有配置样例
# /bin/cp support-files/my-large.cnf /etc/my.cnf <------- /bin/cp 直接覆盖重名文件,不提示
3. 初始化数据(是否需要单独挂一个目录?根据需要)
# mkdir -p /data/mysql
# chown -R mysql:mysql /data/mysql
# ./scripts/mysql_install_db --basedir=/lnmp/server/mysql --datadir=/data/mysql --user=mysql
...
Installing MySQL system tables...
160529 18:23:19 [Note] /lnmp/server/mysql/bin/mysqld (mysqld 5.5.50) starting as process 33182 ...
OK
Filling help tables...
160529 18:23:20 [Note] /lnmp/server/mysql/bin/mysqld (mysqld 5.5.50) starting as process 33189 ...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
...
注:如上出现两个ok,标识初始化成功;否则失败,需解决初始化错误
4. 修改设置并启动服务
# cp ./support-files/mysql.server /etc/init.d/mysqld
# chmod 755 /etc/init.d/mysqld
# sed -i 's#/usr/local/mysql#/lnmp/server/mysql#g' /lnmp/server/mysql/bin/mysqld_safe /etc/init.d/mysqld
<------- 替换二进制包的默认MySQL安装路径
# vim /etc/init.d/mysqld <------- 修改 basedir 和 datadir,如下
basedir=/lnmp/server/mysql
datadir=/data/mysql
# /etc/init.d/mysqld start <------- 启动服务
Starting MySQL... SUCCESS!
检查 3306 是否启动:
# netstat -tlunp|grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 33457/mysqld
5. 设置开机自启动
# chkconfig --add mysqld
# chkconfig mysqld on
# chkconfig --list|grep mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
6. 配置MySQL命令全局使用
# echo 'export PATH=/lnmp/server/mysql/bin:$PATH' >>/etc/profile
# tail -1 /etc/profile <------- 检查写入文件
export PATH=/lnmp/server/mysql/bin:$PATH
# source /etc/profile <------- 使修改马上生效
# echo $PATH <------- 检查最终设置结果
/lnmp/server/mysql/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/whoru/bin
7. 登录测试,更改默认管理员密码
# mysql <------- 输入 mysql 可以直接登录,安装好后默认密码为空
Welcome to the MySQL monitor. Commands end with ; or \g.
.....
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>quit; <------- 退出 mysql 命令行
# mysqladmin -u root password 'whoru123' <------- 修改 root 默认的空密码
# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
# mysql -uroot -p <------- 必须使用用户名和密码登录
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
.....
mysql>
8. 清理没有用的用户及库
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | web1 |
| root | web1 |
+------+-----------+
6 rows in set (0.00 sec)
mysql> drop user "root"@"::1";
Query OK, 0 rows affected (0.00 sec)
mysql> drop user ""@"localhost";
Query OK, 0 rows affected (0.00 sec)
mysql> drop user ""@"web1";
Query OK, 0 rows affected (0.00 sec)
mysql> drop user "root"@"web1";
Query OK, 0 rows affected (0.01 sec)
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)
mysql> flush privileges; <------- 清除缓存
Query OK, 0 rows affected (0.00 sec)
来源:oschina
链接:https://my.oschina.net/u/2276973/blog/752468