一、簡介
MySQL 是最流行的關系型數據庫管理系統,由瑞典MySQL AB公司開發,目前屬於Oracle公司。
MySQL所使用的SQL語言是用於訪問數據庫的最常用標准化語言。
MySQL由於其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,一般中小型網站的開發都選擇MySQL作為網站數據庫。
二、准備
1、所需軟件
mysql-5.6.34.tar.gz
下載地址:http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.34.tar.gz
2、所需環境
1)卸載自帶 mysql
# yum -y remove mysql*
2)將原配置文件刪除或改名
# mv /etc/my.cnf /etc/my.cnf.old
三、安裝
1、配置yum源
1)將系統 rhel-server-7.3-x86_64-dvd.iso 文件拷貝到 /soft 目錄下
# cp rhel-server-7.3-x86_64-dvd.iso /soft/
2)建立掛載所需的目錄
# mkdir -p /media/cdrom
3)掛載
# mount -o loop /soft/rhel-server-7.3-x86_64-dvd.iso /media/cdrom
4)進入 /etc/yum.repo.d 目錄,對yum 源進行設置
# cd /etc/yum.repo.d
# vi server.repo
5)查看是否安裝成功,顯示有4751 個軟件包,源配置成功
# yum repolist
2、安裝依賴
# yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make cmake
3、創建用戶和組
# groupadd -r mysql
# useradd -g mysql -r -d /mydata/data mysql
4、解壓安裝包
# tar zxvf mysql-5.6.34.tar.gz
5、編譯
# cd mysql-5.6.34
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mydata/data -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
其他參考
# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/webserver/mysql/ -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_INNODB_MEMCACHED=1 -DWITH_DEBUG=OFF -DWITH_ZLIB=bundled -DENABLED_LOCAL_INFILE=1 -DENABLED_PROFILING=ON -DMYSQL_MAINTAINER_MODE=OFF -DMYSQL_DATADIR=/usr/local/webserver/mysql/data -DMYSQL_TCP_PORT=3306
6、安裝
# make
# make install
7、查看是否安裝成功
查看mysql版本
# /usr/local/mysql/bin/mysql --version
四、配置
1、更改 /usr/local/mysql 目錄所屬
# cd /usr/local
# chown -R mysql:mysql mysql
2、安裝perl
# yum -y install perl*
3、初始化數據庫
# mkdir -p /mydata/data
# cd /usr/local/mysql
# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/data
Installing MySQL system tables...2016-12-12 13:24:13 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-12-12 13:24:13 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2016-12-12 13:24:13 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.34) starting as process 4076 ...
2016-12-12 13:24:13 4076 [Note] InnoDB: Using atomics to ref count buffer pool pages
2016-12-12 13:24:13 4076 [Note] InnoDB: The InnoDB memory heap is disabled
2016-12-12 13:24:13 4076 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-12-12 13:24:13 4076 [Note] InnoDB: Memory barrier is not used
2016-12-12 13:24:13 4076 [Note] InnoDB: Compressed tables use zlib 1.2.7
2016-12-12 13:24:13 4076 [Note] InnoDB: Using CPU crc32 instructions
2016-12-12 13:24:13 4076 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-12-12 13:24:13 4076 [Note] InnoDB: Completed initialization of buffer pool
2016-12-12 13:24:13 4076 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2016-12-12 13:24:13 4076 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2016-12-12 13:24:13 4076 [Note] InnoDB: Database physically writes the file full: wait...
2016-12-12 13:24:13 4076 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2016-12-12 13:24:14 4076 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2016-12-12 13:24:16 4076 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2016-12-12 13:24:16 4076 [Warning] InnoDB: New log files created, LSN=45781
2016-12-12 13:24:16 4076 [Note] InnoDB: Doublewrite buffer not found: creating new
2016-12-12 13:24:16 4076 [Note] InnoDB: Doublewrite buffer created
2016-12-12 13:24:16 4076 [Note] InnoDB: 128 rollback segment(s) are active.
2016-12-12 13:24:16 4076 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-12-12 13:24:16 4076 [Note] InnoDB: Foreign key constraint system tables created
2016-12-12 13:24:16 4076 [Note] InnoDB: Creating tablespace and datafile system tables.
2016-12-12 13:24:16 4076 [Note] InnoDB: Tablespace and datafile system tables created.
2016-12-12 13:24:16 4076 [Note] InnoDB: Waiting for purge to start
2016-12-12 13:24:16 4076 [Note] InnoDB: 5.6.34 started; log sequence number 0
2016-12-12 13:24:16 4076 [Note] RSA private key file not found: /mydata/data//private_key.pem. Some authentication plugins will not work.
2016-12-12 13:24:16 4076 [Note] RSA public key file not found: /mydata/data//public_key.pem. Some authentication plugins will not work.
2016-12-12 13:24:19 4076 [Note] Binlog end
2016-12-12 13:24:19 4076 [Note] InnoDB: FTS optimize thread exiting.
2016-12-12 13:24:19 4076 [Note] InnoDB: Starting shutdown...
2016-12-12 13:24:19 4076 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK
Filling help tables...2016-12-12 13:24:20 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-12-12 13:24:20 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2016-12-12 13:24:20 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.34) starting as process 4098 ...
2016-12-12 13:24:20 4098 [Note] InnoDB: Using atomics to ref count buffer pool pages
2016-12-12 13:24:20 4098 [Note] InnoDB: The InnoDB memory heap is disabled
2016-12-12 13:24:20 4098 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-12-12 13:24:20 4098 [Note] InnoDB: Memory barrier is not used
2016-12-12 13:24:20 4098 [Note] InnoDB: Compressed tables use zlib 1.2.7
2016-12-12 13:24:20 4098 [Note] InnoDB: Using CPU crc32 instructions
2016-12-12 13:24:20 4098 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-12-12 13:24:20 4098 [Note] InnoDB: Completed initialization of buffer pool
2016-12-12 13:24:20 4098 [Note] InnoDB: Highest supported file format is Barracuda.
2016-12-12 13:24:20 4098 [Note] InnoDB: 128 rollback segment(s) are active.
2016-12-12 13:24:20 4098 [Note] InnoDB: Waiting for purge to start
2016-12-12 13:24:20 4098 [Note] InnoDB: 5.6.34 started; log sequence number 1625977
2016-12-12 13:24:20 4098 [Note] RSA private key file not found: /mydata/data//private_key.pem. Some authentication plugins will not work.
2016-12-12 13:24:20 4098 [Note] RSA public key file not found: /mydata/data//public_key.pem. Some authentication plugins will not work.
2016-12-12 13:24:20 4098 [Note] Binlog end
2016-12-12 13:24:20 4098 [Note] InnoDB: FTS optimize thread exiting.
2016-12-12 13:24:20 4098 [Note] InnoDB: Starting shutdown...
2016-12-12 13:24:21 4098 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h test password 'new-password'
Alternatively you can run:
/usr/local/mysql/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd . ; /usr/local/mysql/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Please report any problems at http://bugs.mysql.com/
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
New default config file was created as /usr/local/mysql/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings
WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server
-------------------------------------------------------------------------------------------------------------------------
可能出現的錯誤:
bash: ./scripts/mysql_install_db: /usr/bin/perl: bad interpreter: No such file or directory
安裝 perl 后問題解決
-------------------------------------------------------------------------------------------------------------------------
4、配置文件
# cp support-files/my-default.cnf /etc/my.cnf
5、添加到服務
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld on
6、添加到環境變量
# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
添加到 /etc/profile 后,為什么沒有生效呢?
別忘了執行下一步:
# source /etc/profile
7、啟動
# service mysqld start
8、測試
# mysql -uroot -p
初始沒有密碼,直接回車
登錄成功
測試
9、設置密碼
方式一:
# mysqladmin -uroot password 要設置的密碼(加不加引號都可以)
或者
# mysqladmin -u用戶名 -p舊密碼 password 新密碼(加不加引號都可以)
方式二:
mysql> SET PASSWORD FOR '用戶名'@'主機' = PASSWORD('密碼');
方式三:
mysql> update mysql.user set authentication_string=PASSWORD('要設置的密碼') where user='root';
mysql> flush privileges; # 刷新數據庫權限
10、創建用戶
1)創建用戶
mysql> create user '用戶名'@'%' identified by '密碼';
2)創建用戶並授權
mysql> grant all privileges on *.* to 用戶名@'%' identified by '密碼' with grant option;
mysql> flush privileges;# 刷新數據庫權限
五、常用命令
- 啟動:service mysqld start
- 停止:service mysqld stop
- 重啟:service mysqld restart
- 重載配置:service mysqld reload