MySQL資料庫 第一條產品線:5.0.xx及升級到5.1.xx的產品系列,這條產品線繼續完善與改進其用戶體驗和性能,同時增加新功能。 第二條產品線:為了更好地整合MySQL AB公司社區和第三方公司開發的新存儲引擎,以及吸收新的實現和演算法等,從而更好地支持SMP架構,提高性能而做了大量的代碼重構。... ...
MySQL資料庫
第一條產品線:5.0.xx及升級到5.1.xx的產品系列,這條產品線繼續完善與改進其用戶體驗和性能,同時增加新功能。
第二條產品線:為了更好地整合MySQL AB公司社區和第三方公司開發的新存儲引擎,以及吸收新的實現和演算法等,從而更好地支持SMP架構,提高性能而做了大量的代碼重構。版本編號從5.4.xx開始,目前發展到5.7.xx。
第三條產品線:為了更好地推廣MySQL Cluster版本,以及提高MySQL Cluster的性能和穩定性,以及功能改進和增加,以及改動MySQL基礎功能,使其對Cluster存儲引擎提供更有效的支持與優化。版本號為6.0.xx開始,目前發展到7.1.xx。
安裝MySQL
Linux軟體的安裝方式
1、yum/rpm簡單、快,無法定製,
2、編譯安裝,./configure;make;make install。複雜、數度慢,可定製。
針對mysql,第一條產品線的編譯方式5.0—5.1。
mysql5.5以上,編譯安裝,./cmake;gmake;gmake install。
3、二進位包,解壓即用。簡單、快,不好定製。
mysql-5.5.32-linux2.6-x86_64.tar.gz
- [root@lnmp tools]# ls -l mysql-5.5.32-linux2.6-x86_64.tar.gz
- -rw-r--r-- 1 root root 186722932 Feb 25 10:17 mysql-5.5.32-linux2.6-x86_64.tar.gz
創建mysql用戶
- [root@lnmp tools]# useradd -s /sbin/nologin -M mysql
- [root@lnmp tools]# id mysql
- uid=503(mysql) gid=503(mysql) groups=503(mysql)
解壓
- [root@lnmp tools]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz
- [root@lnmp tools]# ls -ld mysql-5.5.32-linux2.6-x86_64
- drwxr-xr-x 13 root root 4096 Feb 25 10:22 mysql-5.5.32-linux2.6-x86_64
移動到安裝目錄、創建軟連接
- [root@lnmp tools]# mv mysql-5.5.32-linux2.6-x86_64 /application/mysql-5.5.32
- [root@lnmp tools]# cd /application/
- [root@lnmp application]# ln -s mysql-5.5.32/ mysql
- [root@lnmp application]# ls -l mysql
- lrwxrwxrwx 1 root root 13 Feb 25 10:25 mysql -> mysql-5.5.32/
操作到此步驟相當於編譯安裝make install之後。
初始化資料庫
- [root@lnmp application]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql
授權mysql用戶管理mysql
- [root@lnmp application]# chown -R mysql:mysql /application/mysql
- [root@lnmp application]# ls -ld /application/mysql
- lrwxrwxrwx 1 mysql mysql 13 Feb 25 10:25 /application/mysql -> mysql-5.5.32/
出現兩個OK表示成功
- Installing MySQL system tables...
- OK
- Filling help tables...
- OK
生成MySQL配置文件
- [root@lnmp mysql]# cp support-files/my-small.cnf /etc/my.cnf
配置啟動MySQL
- [root@lnmp mysql]# sed -i "s#/usr/local/mysql#/application/mysql#g" /application/mysql/bin/mysqld_safe
後臺啟動MySQL
- [root@lnmp mysql]# /application/mysql/bin/mysqld_safe &
- [root@lnmp mysql]# lsof -i:3306
- COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
- mysqld 1455 mysql 10u IPv4 16888 0t0 TCP *:mysql (LISTEN)
配置環境變數
- [root@lnmp mysql]# vim /etc/profile
- PATH="/application/mysql/bin:$PATH"
- [root@lnmp mysql]# source /etc/profile
- [root@lnmp mysql]# which mysql
- /application/mysql/bin/mysql
登錄測試
- [root@lnmp mysql]# mysql
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 2
- Server version: 5.5.32 MySQL Community Server (GPL)
- Copyright (c) 2000, 2013, 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
- [root@lnmp mysql]# cp support-files/mysql.server /etc/init.d/mysqld
- [root@lnmp mysql]# sed -i "s#/usr/local/mysql#/application/mysql#g" /etc/init.d/mysqld
- [root@lnmp mysql]# chmod +x /etc/init.d/mysqld
- [root@lnmp mysql]# killall mysqld
- [root@lnmp mysql]# lsof -i:3306
- [root@lnmp mysql]# /etc/init.d/mysqld start
- Starting MySQL.. SUCCESS!
- [root@lnmp mysql]# lsof -i:3306
- COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
- mysqld 6993 mysql 10u IPv4 21591 0t0 TCP *:mysql (LISTEN)
設置密碼
- [root@lnmp mysql]# mysqladmin -uroot password "system"
- [root@lnmp mysql]# mysql
- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
- [root@lnmp mysql]# mysql -uroot -psystem
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 3
- Server version: 5.5.32 MySQL Community Server (GPL)
- Copyright (c) 2000, 2013, 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>
修改密碼
- [root@lnmp mysql]# mysqladmin -uroot -psystem password "123456"
- [root@lnmp mysql]# mysql -uroot -psystem
- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
- [root@lnmp mysql]# mysql -uroot -p123456
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 7
- Server version: 5.5.32 MySQL Community Server (GPL)
- Copyright (c) 2000, 2013, 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
- [root@lnmp mysql]# mysql -uroot -p
- Enter password:
- mysql> show databases; #查看所有庫
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- | test |
- +--------------------+
- 4 rows in set (0.00 sec)
- mysql> drop database test; #刪除test表
- Query OK, 0 rows affected (0.04 sec)
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | mysql |
- | performance_schema |
- +--------------------+
- 3 rows in set (0.00 sec)
- mysql> select user,host from mysql.user;
- +------+-----------+
- | user | host |
- +------+-----------+
- | root | 127.0.0.1 |
- | root | ::1 |
- | | lnmp |
- | root | lnmp |
- | | localhost |
- | root | localhost |
- +------+-----------+
- 6 rows in set (0.03 sec)
- mysql> drop user ''@'lnmp'; #刪除用戶
- Query OK, 0 rows affected (0.05 sec)
- mysql> drop user ''@'localhost';
- Query OK, 0 rows affected (0.00 sec)
- mysql> drop user 'root'@'lnmp';
- Query OK, 0 rows affected (0.00 sec)
- mysql> drop user 'root'@'::1';
- Query OK, 0 rows affected (0.00 sec)
- mysql> select user,host from mysql.user;
- +------+-----------+
- | user | host |
- +------+-----------+
- | root | 127.0.0.1 |
- | root | localhost |
- +------+-----------+
- 2 rows in set (0.00 sec)
當有特殊字元或大寫時,使用delete刪除。
- mysql> delete from mysql.user where user="root" and host="A";
- Query OK, 0 rows affected (0.05 sec)
最後執行刷新,讓許可權生效。
- mysql> flush privileges;
- Query OK, 0 rows affected (0.00 sec)
查所有的庫:show databases;
切庫:use mysql;
查表:show tables;
查看用戶列表:select user,host from mysql.user;
查看當前用戶:select user();
查看當前所在庫:select database();
刪除資料庫:drop database 庫名;
刪除用戶:drop user '用戶名'@'主機';