MySQL是最流行的開放源碼SQL數據庫管理系統,它是由MySQL AB公司開發、發布並支持的。有以下特點:
MySQL表最大能達到多少?
InnoDB存儲引擎將InnoDB表保存在一個表空間內,該表空間可由數個文件創建。這樣,表的大小就能超過單獨文件的最大容量。表空間可包括原始磁盤分區,從而使得很大的表成為可能。表空間的最大容量為64TB。
下載MySQL地址:http://dev.mysql.com/downloads/mysql/
CentOS 安裝:
yum install mysql-server
Ubuntu 安裝:
1. sudo apt-get install mysql-server 2. sudo apt-get isntall mysql-client 3. sudo apt-get install libmysqlclient-dev # 檢測是否安裝成功(是否為LISTEN狀態) sudo netstat -tap | grep mysql
編譯安裝MySQL-5.5.32:
# 安裝依賴包 yum install ncurses-devel gcc gcc-c++ -y # 創建目錄 mkdir -p /home/oldsuo/tools # 安裝cmake軟件,gmake編譯安裝 cd /home/oldsuo/tools/ tar xf cmake-2.8.8.tar.gz cd cmake-2.8.8 ./configure #CMake has bootstrapped. Now run gmake. gmake gmake install cd ../ # 開始安裝mysql # 創建用戶和組 groupadd mysql useradd mysql -s /sbin/nologin -M -g mysql # 解壓編譯MySQL tar zxf mysql-5.5.32.tar.gz cd mysql-5.5.32 cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.5.32 \ -DMYSQL_DATADIR=/application/mysql-5.5.32/data \ -DMYSQL_UNIX_ADDR=/application/mysql-5.5.32/tmp/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \ -DENABLED_LOCAL_INFILE=ON \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_FEDERATED_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \ -DWITHOUT_PARTITION_STORAGE_ENGINE=1 \ -DWITH_FAST_MUTEXES=1 \ -DWITH_ZLIB=bundled \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_READLINE=1 \ -DWITH_EMBEDDED_SERVER=1 \ -DWITH_DEBUG=0 #-- Build files have been written to: /home/oldsuo/tools/mysql-5.5.32 提示: 編譯時可配置的選項很多,具體可參考結尾附錄或官方文檔: make #[100%] Built target my_safe_process make install ln -s /application/mysql-5.5.32/ /application/mysql 如果上述操作未出現錯誤,則MySQL5.5.32軟件cmake方式的安裝就算成功了。 #拷貝配置文件 cp mysql-5.5.32/support-files/my-small.cnf /etc/my.cnf #添加變量,並使之生效 echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile source /etc/profile echo $PATH #授權用戶及/tmp/臨時文件目錄 chown -R mysql.mysql /application/mysql/data/ chmod -R 1777 /tmp/ #初始化數據庫 cd /application/mysql/scripts/ ./mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql cd ../ #啟動數據庫 cp support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld /etc/init.d/mysqld start #檢查端口 netstat -lntup|grep 3306
1、刪除不必要的用戶和庫:
#查看用戶和主機列,從mysql.user裡查看 select user,host from mysql.user; #刪除用戶名為空的庫,並檢查 delete from mysql.user where user=''; select user,host from mysql.user; #刪除主機名為localhost.localdomain的庫,並檢查 delete from mysql.user where host='localhost.localdomain'; select user,host from mysql.user; #刪除主機名為::1的庫,並檢查。::1庫的作用為IPV6 delete from mysql.user where host='::1'; #刪除test庫 drop database test;
2、添加額外管理員:
# 添加額外管理員,system作為管理員,oldsuo為密碼 mysql> delete from mysql.user; Query OK, 2 rows affected (0.00 sec) mysql> grant all privileges on *.* to system@'localhost' identified by 'oldsuo' with grant option; Query OK, 0 rows affected (0.00 sec) # 刷新MySQL的系統權限相關表,使配置生效 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> select user,host from mysql.user; +--------+-----------+ | user | host | +--------+-----------+ | system | localhost | +--------+-----------+ 1 row in set (0.00 sec) mysql>
3、設置登錄密碼並開機自啟:
#設置密碼,並登陸 /usr/local/mysql/bin/mysqladmin -u root password 'oldsuo' mysql -usystem -p #開機啟動mysqld,並檢查 chkconfig mysqld on chkconfig --list mysqld
#安裝依賴包 yum –y install ncurses ncurses-devel gcc gcc-c++ #添加mysql用戶及組 groupadd mysql useradd -r -s /sbin/nologin -g mysql mysql #mysql5.1.62編譯參數: ./configure \ --prefix=/usr/local/mysql \ --with-unix-soket-path=/usr/local/tmp/mysql.sock \ --localstatedir=/usr/local/mysql/data \ --enable-assembler \ --enable-thread-safe-client \ --with-mysqld-user=mysql \ --with-big-tables \ --without-debug \ --with-pthread \ --enable-assembler \ --with-extra-charsets=complex \ --with-readline \ --with-ssl \ --with-embedded-server \ --enable-local-infile \ --with-plugins=partition,innobase \ --with-mysqld-ldflags=-all-static \ --with-client-ldflags=-all-static make && make install #初始化mysql mkdir -p /usr/local/mysql/data #建立mysql數據文件目錄 chown -R mysql.mysql /usr/local/mysql/ #授權mysql用戶訪問mysql安裝目錄 /usr/local/mysql/bin/mysql_install_db --user=mysql #初始化 #拷貝mysql啟動腳本 cp support-files/my-small.cnf /etc/my.cnf #cp support-files/mysql.server /etc/init.d/mysqld chmod 700 /etc/init.d/mysqld #配置mysql使用全局路徑 echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile #添加變量到profile source /etc/profile #使變量生效 echo $PATH #檢查 #啟動mysqld /etc/init.d/mysqld start #登陸報錯,做軟鏈接 #ln -s /usr/local/mysql/bin/mysql /usr/bin/ #啟動報錯日志: Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't #exist #解決方法: /usr/local/mysql/bin/mysql_install_db --user=mysql #初始化數據庫即可 #登陸報錯: mysql: unknown variable 'datadir=/usr/local/mysql/data' #解決方法: my.cnf 配置問題,vim /etc/my.cnf [client] #password = your_password port = 3306 socket = /tmp/mysql.sock #datadir = /data1/mysql/var/ #這個不能加在上面,去掉 [mysqld] port = 3306 socket = /tmp/mysql.sock datadir = /data1/mysql/var/ #加在這裡就可以了 #設置mysql用戶root 的密碼為oldsuo /usr/local/mysql/bin/mysqladmin -u root password 'oldsuo' mysql5.1.62安裝編譯
對於新手來說,字符集亂碼問題無疑是頭痛的問題,小編就帶你不在頭痛,從此幸福。
字符集,character set,就是一套表示字符的符號和這些的符號的底層編碼;而校驗規則,則是在字符集內用於比較字符的一套規則。簡單的說,字符集就是一套文字符號及其編碼、比較規則的集合,第一個計算機字符集ASC2,MySQL數據庫字符集包括字符集和校對規則兩個概念,字符集是定義數據庫裡面的內容字符串的存儲方式,而校對規則是定義比較字符串的方式。
建議:中英文環境選擇utf8
# 查看MySQL字符集設置情況 show variables like 'character_set%'; # 查看庫的字符集 show create database db; # 查看表的字符集 show create table db_tb\G # 查詢所有 show collation; # 設置表的字符集 set tables utf8;show create database nick_defailt\G #查看nick_defailt庫字符集 mysql -uroot -p -e "SHOW CHARACTER SET;" show variables like 'character_set%'; mysql> show variables like 'character_set%'; +-----------------------------------------+------------------------------------------------------------+ | Variable_name | Value | +----------------------------------------+-------------------------------------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/local/mysql/share/mysql/charsets/ | +----------------------------------------+--------------------------------------------------------------+ 8 rows in set (0.00 sec) mysql> show create database nick_defailt \G *************************** 1. row *************************** Database: data Create Database: CREATE DATABASE `data` /*!40100 DEFAULT CHARACTER SET utf8 */ 1 row in set (0.00 sec) View Code
1> 系統方面 cat /etc/sysconfig/i18n LANG="zh_CN.UTF-8" 2> 客戶端(程序),調整字符集為latin1。 mysql> set names latin1; #臨時生效 Query OK, 0 rows affected (0.00 sec) #更改my.cnf客戶端模塊的參數,實現set name latin1 的效果,並且永久生效。 [client] default-character-set=latin1 #無需重啟服務,退出登錄就生效,相當於set name latin1。 3> 服務端,更改my.cnf參數 [mysqld] default-character-set=latin1 #適合5.1及以前版本 character-set-server=latin1 #適合5.5 4> 庫、表、程序 #建表指定utf8字符集 mysql> create database nick_defailtsss DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; Query OK, 1 row affected (0.00 sec)
1> 導出表結構 #以utf8格式導出 mysqldump -uroot -p --default-character-set=utf8 -d nick_defailt>alltable.sql --default-character-set=gbk #表示已GBK字符集連接 –d 只表示表結構 2> 編輯alltable.sql 將utf8改成gbk。 3> 確保數據庫不在更新,導出所有數據 mysqldump -uroot -p --quick --no-create-info --extended-insert --default-character-set=utf8 nick_defailt>alldata.sql 4> 打開alldata.sql將set name utf8 修改成 set names gbk(或者修改系統的服務端和客戶端) 5> 建庫 create database oldsuo default charset gbk; 6> 創建表,執行alltable.sql mysql -uroot -p oldsuo <alltable.sql 7> 導入數據 mysql -uroot -p oldsuo <alltable.sql
MySQL最常用存儲引擎Myisam和Innodb。mysql 5.5.5以後默認存儲引擎為Innodb。
MySQL的每種引擎在MySQL裡是通過插件的方式使用的,MySQL可以支持多種存儲引擎。
建議:使用 Innodb引擎,因為支持回滾,後續博客會講。
1) MyISAM引擎系統庫表對應文件 [root@mysql 3306]# ll /data/3306/data/mysql/ -rw-rw----. 1 mysql mysql 10630 10月 31 16:05 user.frm #保存表的定義 -rw-rw----. 1 mysql mysql 1140 10月 31 18:40 user.MYD #數據文件 -rw-rw----. 1 mysql mysql 2048 10月 31 18:40 user.MYI #索引文件 [root@mysql 3306]# file data/mysql/user.frm data/mysql/user.frm: MySQL table definition file Version 9 [root@mysql 3306]# file data/mysql/user.MYD data/mysql/user.MYD: DBase 3 data file (167514107 records) [root@mysql 3306]# file data/mysql/user.MYI data/mysql/user.MYI: MySQL MISAM compressed data file Version 1 2) InnoDB引擎 [root@mysql 3306]# ll data/ -rw-rw----. 1 mysql mysql 134217728 10月 31 20:05 ibdata1
創建後引擎的修改 語法: ALTER TABLE student ENGINE = INNODB; ALTER TABLE student ENGINE = MyISAM;mysql> use teacher; Database changed mysql> show create table student; +---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | student | CREATE TABLE `student` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, `age` tinyint(2) NOT NULL DEFAULT '0', `dept` varchar(16) DEFAULT NULL, PRIMARY KEY (`id`), KEY `index_name` (`name`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 | +---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec) mysql> ALTER TABLE student ENGINE = MyISAM; Query OK, 3 rows affected (0.05 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> show create table student; +---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | student | CREATE TABLE `student` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, `age` tinyint(2) NOT NULL DEFAULT '0', `dept` varchar(16) DEFAULT NULL, PRIMARY KEY (`id`), KEY `index_name` (`name`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 | +---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) 修改實例
mysql> create table mess ( -> id int(4) not null, -> name char(20) not null, -> age tinyint(2) NOT NULL default '0', -> dept varchar(16) default NULL -> ) ENGINE=MyISAM CHARSET=utf8; Query OK, 0 rows affected (0.00 sec)
運行相關:
1、 單實例mysql啟動 [root@localhost ~]# /etc/init.d/mysqld start Starting MySQL [確定] #mysqld_safe –user=mysql & 2、 查看MySQL端口 [root@localhost ~]# ss -lntup|grep 3306 tcp LISTEN 0 50 *:3306 *:* users:(("mysqld",19651,10)) 3、 查看MySQL進程 [root@localhost ~]# ps -ef|grep mysql|grep -v grep root 19543 1 0 Oct10 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/localhost.localdomain.pid mysql 19651 19543 0 Oct10 ? 00:05:04 /usr/local/mysql/libexec/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql --log-error=/usr/local/mysql/data/localhost.localdomain.err --pid-file=/usr/local/mysql/data/localhost.localdomain.pid --socket=/tmp/mysql.sock --port=3306 4、 MySQL啟動原理 /etc/init.d/mysqld 是一個shell啟動腳本,啟動後最終會調用mysqld_safe腳本,最後調用mysqld服務啟動mysql。 "$manager" \ --mysqld-safe-compatible \ --user="$user" \ --pid-file="$pid_file" >/dev/null 2>&1 & 5、關閉數據庫 [root@localhost ~]# /etc/init.d/mysqld stop Shutting down MySQL.... [確定] 6、 查看mysql數據庫裡操作命令歷史 cat /root/.mysql_history 7、 強制linux不記錄敏感歷史命令 HISTCONTROL=ignorespace 8、 mysql設置密碼 /usr/local/mysql/bin/mysqladmin -u root password 'oldsuo' 9、 mysql修改密碼,與多實例指定sock修改密碼 mysqladmin -uroot -passwd password 'oldsuo' mysqladmin -uroot -passwd password 'oldsuo' -S /data/3306/mysql.sock
操作相關:
#登陸mysql數據庫 mysql -uroot –p #查看有哪些庫 show databases; #刪除test庫 drop database test; #使用test庫 use test; #查看有哪些表 show tables; #查看suoning表的所有內容 select * from suoning; #查看當前版本 select version(); #查看當前用戶 select user(); #查看用戶和主機列,從mysql.user裡查看 select user,host from mysql.user; #刪除前為空,後為localhost的庫 drop user ""@localhost; #刷新權限 flush privileges; #跳出數據庫執行命令 system ls;
忘記mysql登錄密碼也是一件頭疼的事,那麼小編會讓你繼續幸福。
1> 普通方式 #> service mysqld stop #>mysqld_safe --skip-grant-tables & 輸入 mysql -uroot -p 回車進入 >use mysql; > update user set password=PASSWORD("newpass")where user="root"; 更改密碼為 newpassord > flush privileges; 更新權限 > quit 退出 service mysqld restart mysql -uroot -p新密碼進入
2> 普通方式的簡寫 service mysqld stop mysqld_safe --skip-grant-tables --user=mysql & mysql update mysql.user set password=PASSWORD("newpass")where user="root" and host='localhost'; flush privileges; mysqladmin -uroot -pnewpass shutdown /etc/init.d/mysqld start mysql -uroot -pnewpass #登陸 3>多實例方式 killall mysqld mysqld_safe –defaults-file=/data/3306/my.cnf –skip-grant-table & mysql –u root –p –S /data/3306/mysql.sock #指定sock登陸 update mysql.user set password=PASSWORD("newpass")where user="root"; flush privileges; mysqladmin -uroot -pnewpass shutdown /etc/init.d/mysqld start mysql -uroot -pnewpass #登陸
注:本文有看不懂的在後續博客有詳解