Linux MySQL
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 '用户名'@'主机';
posted on 2017-02-25 15:43 yinshoucheng 阅读(349) 评论(0) 编辑 收藏 举报