標籤:mysql
環境:
[[email protected] ~]# cat /etc/redhat-release
CentOS release 6.8 (Final)
[[email protected] ~]# uname -r
2.6.32-642.el6.x86_64
開始安裝配置:
[[email protected] ~]# cd /usr/local/src/
[[email protected] src]# wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.55-linux2.6-x86_64.tar.gz
[[email protected] src]# ll -h
total 178M
-rw-r--r-- 1 root root 178M Apr 22 16:27 mysql-5.5.55-linux2.6-x86_64.tar.gz
[[email protected] src]# tar zxf mysql-5.5.55-linux2.6-x86_64.tar.gz -C /usr/local/ # 解壓
[[email protected] src]# cd ..
[[email protected] local]# mv mysql-5.5.55-linux2.6-x86_64/ mysql-5.5.55 # 改名
[[email protected] local]# ln -s mysql-5.5.55 mysql # 做軟串連
[[email protected] local]# useradd mysql -s /sbin/nologin -M # 建立系統使用者
[[email protected] local]# mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql # 初始化資料庫
[[email protected] local]# chown -R mysql:mysql mysql-5.5.55 # 對目錄授權
[[email protected] local]# vi /etc/my.cnf # 建立設定檔,測試用的
[client]port=3306socket= /usr/local/mysql/mysql.sock[mysqld]user = mysqldatadir = /usr/local/mysql/data/character-set-server = utf8skip-character-set-client-handshakeinit-connect = ‘SET NAMES utf8‘open_files_limit=1024back_log = 600max_connections = 800max_connect_errors = 3000table_cache = 614external-locking = FALSEmax_allowed_packet =8Msort_buffer_size = 1Mjoin_buffer_size = 1Mthread_cache_size = 100thread_concurrency = 2query_cache_size = 2Mquery_cache_limit = 1Mquery_cache_min_res_unit = 2kthread_stack = 192Ktmp_table_size = 2Mmax_heap_table_size = 2Mlog-bin = /usr/local/mysql/data/mysql-binbinlog_cache_size = 1Mmax_binlog_cache_size = 1Mmax_binlog_size = 2Mexpire_logs_days = 7key_buffer_size = 16Mread_buffer_size = 1Mread_rnd_buffer_size = 1Mbulk_insert_buffer_size = 1Mlower_case_table_names = 1skip-name-resolveslave-skip-errors = 1032,1062replicate-ignore-db=mysqlserver-id = 1innodb_additional_mem_pool_size = 4Minnodb_buffer_pool_size = 16Minnodb_file_io_threads = 4innodb_thread_concurrency = 8innodb_flush_log_at_trx_commit = 2innodb_log_buffer_size = 2Minnodb_log_file_size = 4Minnodb_log_files_in_group = 3innodb_max_dirty_pages_pct = 90innodb_lock_wait_timeout = 120innodb_file_per_table = 0[mysqldump]quickmax_allowed_packet = 2M[mysqld_safe]log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid
[[email protected] local]# cp mysql/bin/* sbin/ # 複製執行命令到 sbin 下
[[email protected] local]# cp mysql/support-files/mysql.server /etc/init.d/mysqld # 產生啟動指令碼
[[email protected] local]# /etc/init.d/mysqld start # 直接啟動服務
Starting MySQL...... SUCCESS!
[[email protected] ~]# netstat -lntup # 查看服務
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 7853/mysqld
[[email protected] ~]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 7853 mysql 12u IPv4 24118 0t0 TCP *:mysql (LISTEN)
[[email protected] local]# mysqladmin -u root password # 給 root 密碼,預設免密登入
Enter password:
[[email protected] local]# mysql -uroot -p # 互動式密碼登入
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.55-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.13 sec)
mysql> drop database test; # 刪掉測試庫
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | sql-m |
| root | sql-m |
+------+-----------+
6 rows in set (0.11 sec)
mysql> drop user [email protected]‘::1‘; # 把沒用的使用者刪除
Query OK, 0 rows affected (0.14 sec)
mysql> drop user ‘‘@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> drop user ‘‘@‘sql-m‘;
Query OK, 0 rows affected (0.00 sec)
mysql> drop user [email protected]‘sql-m‘;
Query OK, 0 rows affected (0.03 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)
mysql> quit
[[email protected] local]# mysqladmin -uroot -p123 password # 改密碼
New password:
Confirm new password:
測試忘記root密碼重新找回:
[[email protected] ~]# /etc/init.d/mysqld stop # 先停掉服務
Shutting down MySQL.. SUCCESS!
[[email protected] ~]# mysqld_safe --skip-grant-tables & # 忽略授權表方式啟動服務
[1] 3685
170422 17:37:34 mysqld_safe Logging to ‘/var/log/mysqld.log‘.
170422 17:37:34 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
[[email protected] ~]# 170422 17:37:39 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
[1]+ Done mysqld_safe --skip-grant-tables
奇怪居然失敗了!看一下日誌先。。。
[[email protected] ~]# tail /var/log/mysqld.log
170422 17:39:41 InnoDB: Waiting for the background threads to start
170422 17:39:42 InnoDB: 5.5.55 started; log sequence number 1595668
170422 17:39:42 [Note] Recovering after a crash using /usr/local/mysql/data/mysql-bin
170422 17:39:42 [Note] Starting crash recovery...
170422 17:39:42 [Note] Crash recovery finished.
170422 17:39:42 [Note] Server hostname (bind-address): ‘0.0.0.0‘; port: 3306
170422 17:39:42 [Note] - ‘0.0.0.0‘ resolves to ‘0.0.0.0‘;
170422 17:39:42 [Note] Server socket created on IP: ‘0.0.0.0‘.
170422 17:39:42 [ERROR] /usr/local/sbin/mysqld: Can‘t create/write to file ‘/var/run/mysqld/mysqld.pid‘ (Errcode: 2)
170422 17:39:42 [ERROR] Can‘t start server: can‘t create PID file: No such file or directory
可以看到,無法建立 pid 檔案,這肯定是沒許可權造成的。
/var/run/mysqld 這個路徑是設定檔裡配的:
[[email protected] ~]# tail -3 /etc/my.cnf
[mysqld_safe]log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid
這裡有點奇怪,用 /etc/init.d/mysqld 啟動服務時它不會讀取設定檔裡配置的 pid 路徑,而用 mysqld_safe --skip-grant-tables & 忽略授權表方式啟動服務卻會讀取設定檔裡的 pid 路徑。
不想動 /var/log/ 這個目錄的許可權,改設定檔
[[email protected] ~]# vim /etc/my.cnf
[mysqld_safe]log-error=/var/log/mysqld.logpid-file=/usr/local/mysql/mysqld.pid
[[email protected] ~]# /usr/local/mysql/bin/mysqld_safe --skip-grant-tables & # 再次執行
[1] 7109
170422 17:54:17 mysqld_safe Logging to ‘/var/log/mysqld.log‘.
170422 17:54:17 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
[[email protected] ~]# # 這次可以了
[[email protected] ~]# mysql # 免密登入
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.55-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>
mysql> update mysql.user set password=password(‘123‘) where user=‘root‘ and host=‘localhost‘; # 修改密碼
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.19 sec)
突然想到 mysqld_safe 這個命令這麼厲害,看一下它的許可權:
[[email protected] ~]# ll /usr/local/sbin/mysqld_safe
-rwxr-xr-x 1 root root 27100 Apr 22 16:39 /usr/local/sbin/mysqld_safe
嚇一跳有沒有,居然什麼人都可以執行,太危險了!
馬上改掉,兩個地方都改掉:
[[email protected] ~]# chmod 700 /usr/local/sbin/mysqld_safe
[[email protected] ~]# chmod 700 /usr/local/mysql/bin/mysqld_safe
[[email protected] ~]# ll /usr/local/sbin/mysqld_safe
-rwx------ 1 root root 27100 Apr 22 16:39 /usr/local/sbin/mysqld_safe
[[email protected] ~]# ll /usr/local/mysql/bin/mysqld_safe
-rwx------ 1 mysql mysql 27100 Mar 18 13:14 /usr/local/mysql/bin/mysqld_safe
以上二進位包簡單安裝完成。
本文出自 “從沒想過放棄!” 部落格,請務必保留此出處http://yuyicong.blog.51cto.com/11274530/1918544
MySQL-5.5之二進位包安裝