MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择 MySQL 作为网站数据库。
mysql:流程图
mysql内部结构:
两种安装方式:源码编译,二进制安装。
mysql source安装5.5
依赖:
[root@sherry tools]# yum install -y cmake [root@sherry tools]# yum install readline-devel zlib-devel openssl-devel -y
添加mysql用户组:
[root@sherry tools]# groupadd -r -g 306 mysql [root@sherry tools]# useradd -g mysql -u 306 -r -s /sbin/nologin mysql
创建数据与日记存储目录:
[root@sherry tools]# mkdir /mydata/mysql/data -pv mkdir: created directory `/mydata/mysql' mkdir: created directory `/mydata/mysql/data' [root@sherry tools]# mkdir /mydata/mysql/log -pv mkdir: created directory `/mydata/mysql/log' [root@sherry mysql-5.5.48]# chown -R mysql.mysql /mydata/mysql/
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql5.5.48 \ -DMYSQL_DATADIR=/mydata/mysql/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
[root@sherry mysql-5.5.48]# make [root@sherry mysql-5.5.48]# make install
初始化:
[root@sherry local]# cd /usr/local [root@sherry local]# chown -R mysql.mysql mysql5.5.48/ [root@sherry local]# cd mysql5.5.48/ [root@sherry mysql5.5.48]# scripts/mysql_install_db --user=mysql --datadir=/mydata/mysql/data/
添加控制脚本:
[root@sherry mysql5.5.48]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld [root@sherry mysql5.5.48]# chmod +x /etc/rc.d/init.d/mysqld
配置文件:
[root@sherry mysql5.5.48]# cp support-files/my-large.cnf /etc/my.cnf [root@sherry mysql5.5.48]# vim /etc/my.cnf datadir=/mydata/mysql/data innodb_file_per_table=ON log-bin=/mydata/mysql/log/mysql-bin
path添加:
[root@sherry mysql5.5.48]# vim /etc/profile.d/mysql.sh export PATH=/usr/local/mysql5.5.48/bin:$PATH [root@sherry mysql5.5.48]# . /etc/profile.d/mysql.sh
启动脚本:
[root@sherry mysql5.5.48]# /etc/init.d/mysqld start Starting MySQL.. SUCCESS! [root@sherry mysql5.5.48]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.48-log Source distribution Copyright (c) 2000, 2016, 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>
二进制安装:5.6.30
创建用户:
groupadd -r mysql useradd -g mysql -r -s /sbin/nologin -M mysql
创建存储目录:
[root@martin local]# mkdir /mydata/mysql/data -pv [root@martin local]# chown -R mysql:mysql /mydata/mysql/ [root@martin mysql]# mkdir /mydata/mysql/log [root@martin mysql]# chown -R mysql.mysql /mydata/mysql/log
解压缩:
[root@ martin tools]# tar xf mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ [root@ martin local]# mv mysql-5.6.30-linux-glibc2.5-x86_64/ mysql-5.6.30
初始化库:
[root@martin local]# cd /usr/local/ [root@martin local]# chown -R mysql:mysql mysql-5.6.30/ [root@martin local]# cd mysql-5.6.30/ [root@martin mysql-5.6.30]# scripts/mysql_install_db --user=mysql --datadir=/mydata/mysql/data [root@martin mysql-5.6.30]# cd .. [root@martin local]# chown -R root.root mysql-5.6.30/
复制配置文件:
[root@lucia support-files]# cd /usr/local/mysql-5.6.30/support-files [root@lucia support-files]# cp my-default.cnf /etc/my.cnf
配置文件:
basedir = /usr/local/mysql datadir = /mydata/mysql/data port = 3306 server_id = 1 socket = /tmp/mysql.sock log-bin=/mydata/mysql/log/master-bin binlog_format=mixed innodb_file_per_table=ON
启动脚本:
[root@martin local]# ln -sv mysql-5.6.30/ mysql [root@martin mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld [root@martin mysql]# chmod +x /etc/rc.d/init.d/mysqld [root@martin mysql]# chkconfig --add mysqld [root@martin mysql]# chkconfig mysqld on
导入环境变量:
[root@martin local]# vim /etc/profile.d/mysqld.sh export PATH=/usr/local/mysql/bin:$PATH [root@martin local]# . /etc/profile.d/mysqld.sh
mysql数据初始化
mysql> use mysql; mysql> select user,host,password from user; +------+-----------+----------+ | user | host | password | +------+-----------+----------+ | root | localhost | | | root | martin | | | root | 127.0.0.1 | | | root | ::1 | | | | localhost | | | | martin | | +------+-----------+----------+
删除无用账号,设置密码
mysql> DROP USER ''@localhost; mysql> DROP USER 'root'@'::1'; mysql> DROP USER ''@'martin'; mysql> set password for 'root'@'localhost'=password('123456'); mysql> set password for 'root'@'127.0.0.1'=password('123456'); mysql> set password for 'root'@'martin'=password('123456'); mysql> FLUSH PRIVILEGES;
mysql> select user,host,password from user; +------+-----------+-------------------------------------------+ | user | host | password | +------+-----------+-------------------------------------------+ | root | localhost | *437F1809645E0A92DAB553503D2FE21DB91270FD | | root | 127.0.0.1 | *437F1809645E0A92DAB553503D2FE21DB91270FD | | root | marvin | *437F1809645E0A92DAB553503D2FE21DB91270FD | +------+-----------+-------------------------------------------+
用户连接:
[root@sherry mysql5.5.48]# mysql -uroot -hlocalhost --protocol=socket --socket=/tmp/mysql.sock --port=3306 -p
mysql命令解释:
mysql -u, --user 用户名 -h, --host 主机 -p, --password 密码 --protocol={tcp|socket|memory|pipe} 协议 --port 端口 --socket 例如:/tmp/mysql.sock mysql监听的端口: 3306/tcp
查看服务器变量:
mysql> SHOW [{GLOBAL|SESSION}] VARIABLES [LIKE ''];
修改变量:
mysql> SET {GLOBAL|SESSION} VARIABLE_NAME='VALUE';
注意:无论是全局还是会话级别的动态变量修改,在重启mysqld后都会失效;想永久有效,可定义在配置文件中的相应段中[mysqld];
mysql的快捷键:
Ctrl + w: 删除光标之前的单词 Ctrl + u: 删除光标之前至命令行首的所有内容 Ctrl + y: 粘贴使用Ctrl+w或Ctrl+u删除的内容 Ctrl + a: 移动光标至行首 Ctrl + e: 移动光标至行尾
权限设置
GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ... ON [TABLE|FUNCTION|PROCEDURE] priv_level TO username@hostname [IDENTIFIED BY 'password'], [username@hostname [],...] [REQUIRE SSL] [WITH with_option ...]
累加式: mysql> GRANT CREATE ON testdb.* TO 'marco3'@'192.168.1.%'; mysql> GRANT insert ON testdb.* TO 'marco3'@'192.168.1.%';
收回权限:
REVOKE priv_type [(column_list)] [, priv_type [(column_list)]] ... ON [object_type] priv_level FROM user [, user] ... REVOKE ALL PRIVILEGES, GRANT OPTION FROM user [, user] ...
mysql> REVOKE insert ON testdb.* FROM 'marco3'@'192.168.1.%';
小结:一般会用到 (grant|revoke) 操作 on [库名.表名] from 'user'@'host';