CentOS6.x 下 LNMP環境搭建(準備篇)mysql
CentOS6.x 下 LNMP環境搭建(1、安裝 MySQL)linux
1. 建立用戶vim
# 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. 解壓、配置bash
# 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. 初始化數據(是否須要單獨掛一個目錄?根據須要)
tcp
# 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. 修改設置並啓動服務ui
# 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)