MySQl的安装请看我前面的LAMP环境配置
安装好就要进入MySQL了,首次进入数据库是不用密码的:
# /usr/local/mysql/bin/mysql -uroot//不加-uroot也可以
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.1.40-log MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
退出的话,直接quit或exit即可退出。
如果输入一个 “mysql” 命令是不行的,因为 “/usr/local/mysql/bin” 没有在 PATH 这个环境变量里。如何把它加入环境变量PATH中:
[root@cent11 ~]# PATH=$PATH:/usr/local/mysql/bin
这样就可以了,但重启Linux后还会失效,所以需要它开机加载:
# echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
# source /etc/profile//加载配置文件让它生效
# mysql -uroot //就可登录了
给MySQL设置登录密码:
# mysqladmin -uroot password '123456'
这时在登录# mysql -uroot就会报错:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
登录MySQL,-p后面不能有空格:
# mysql -uroot -p123456
更改MySQL的密码:
# mysqladmin -uroot -p123456 password '654321'
刚刚讲过通过使用 mysql -uroot -p 就可以连接数据库了,但这只是连接的本地的数据库 “localhost”, 可是有很多时候都是去连接网络中的某一个主机上的mysql。
# mysql -uroot -p123456 -hlocalhost -P3306
-h:指定主机名,也可以是IP
-P:指定port,也就是端口
-e:不登录,执行某条命令,如:
# mysql -uroot -p123456 -e"show processlist"
常用的MySQL的命令:
1.查看有哪些库
mysql> show databases;
mysql的命令,结尾处需要加一个分号。
2. 查询某个库的表
首先需要进入到某个库里去:
mysql> use mysql;Database changed
mysql> show tables;
+----------------------------------+
| Tables_in_mysql |
+----------------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+----------------------------------+
23 rows in set (0.07 sec)
可以在/data/mysql 目录下看到你有哪些库
一个表在你Linux的/data/mysql/mysql 对应3个文件
3. 查看某个表的全部字段
mysql> desc db;
+-------------------------------+-----------------+------+--------+-----------+---------+
| Field | Type | Null | Key | Default | Extra|
+-------------------------------+-----------------+------+--------+-----------+---------+
| Host | char(60) | NO | PRI | | |
| Db | char(64) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
+-------------------------------+-----------------+-------+------+------------+---------+
22 rows in set (0.00 sec)
也可以查看表的建表语句
mysql> show create table db;
这样查看不是很好,加个\G格式化下
mysql> show create table db\G;
*************************** 1. row ***************************
Table: db
Create Table: CREATE TABLE `db` (
`Host` char(60) COLLATE utf8_bin NOT NULL DEFAULT '',
`Db` char(64) COLLATE utf8_bin NOT NULL DEFAULT '',
`User` char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
`Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
PRIMARY KEY (`Host`,`Db`,`User`),
KEY `User` (`User`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Database privileges'
1 row in set (0.00 sec)
ERROR:
No query specified
4. 查看当前是哪个用户
mysql> select user();
+--------------------------+
| user() |
+--------------------------+
| root@localhost |
+--------------------------+
1 row in set (0.00 sec)
5. 查看当前所使用的库
mysql> select database();
+--------------------------+
| database() |
+--------------------------+
| mysql |
+--------------------------+
1 row in set (0.00 sec)
也可以用命令进入到别的库中
mysql> use test;
6.创建库
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)
7.创建一个表
先进入库中,在创建表
mysql> use db1;
Database changed
`id` 是int型长度是4,`name`是char型长度是40。
mysql> create table tb1 (`id` int(4), `name` char(40));
Query OK, 0 rows affected (0.01 sec)
创建表的时候默认
引擎 ENGINE=MyISAM
字符集是 CHARSET=latin1 ,应该是CHARSET=utf8,可以修改创建显示的默认值。
8.查看数据库版本
mysql> select version();
+------------------+
| version() |
+------------------+
| 5.1.40-log |
+------------------+
1 row in set (0.00 sec)
9.查看当前MySQL状态
mysql> show status;
10. 查看MySQL的参数
mysql> show variables;
11.修改MySQL的参数
mysql> show variables like 'max_connect%';
+-------------------------------+--------+
| Variable_name | Value |
+-------------------------------+--------+
| max_connect_errors | 10 |
| max_connections | 151 |
+-------------------------------+---------+
2 rows in set (0.00 sec)
这些值有些可以在# vim /etc/my.cnf配置文件中设置,有些参数是可以在线编辑的。
mysql> set global max_connect_errors = 100;
在mysql命令行, “%” 类似于shell下的 *, 表示万能匹配。使用 “set global” 可以临时修改某些参数,但是重启mysqld服务后会失效,所以要想永久生效,需要在配置文件 my.cnf 中定义。
12. 查看当前mysql服务器的队列
这个在日常的管理工作中使用最为频繁,因为使用它可以查看当前mysql在干什么,可以发现是否有锁表:
mysql> show processlist;+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 6 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
13. 创建一个普通用户并授权
mysql> grant all on db1.* to 'user1'@'127.0.0.1' identified by '1234567';
Query OK, 0 rows affected (0.01 sec)
all 表示所有的权限(读、写、查询、删除等等操作),db1.*前面的 db1 表示数据库也可以用 * 代替表示所有数据库,后面的 * 表示所有的表,identified by 后面跟密码,用单引号括起来。这里的user1指的是localhost上的user1,如果是给网络上的其他机器上的某个用户授权则这样:
mysql> grant all on db1.* to 'user2'@'192.168.137.55' identified by '1234567';
Query OK, 0 rows affected (0.00 sec)
用户和主机的IP之间有一个@,另外主机IP那里可以用%替代,表示所有主机,例如:
mysql> grant all on db1.* to 'user3'@'%' identified by '1234567';
Query OK, 0 rows affected (0.00 sec)
如果想修改密码,可以# mysql -uroot -p123456修改:
mysql> update mysql.user SET password=PASSWORD("123456") WHERE user='user1';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings:
这里的mysql.user 也可以先进入use mysql 下省略mysql,如 uptate use SET ......WHERE user='user1';
然后更新下缓存,又不然用原始的密码还可以登录。
mysql> flush privileges;
常用SQL命令
1. 查询语句
mysql> select count(*) from mysql.user;
+----------------+
| count(*) |
+----------------+
| 8 |
+----------------+
1 row in set (0.00 sec)
mysql.user表示mysql库的user表;count(*)表示表中共有多少行。
查看所有的行都有哪些内容:
mysql> select * from mysql.db;
这个用来表示查询mysql库的db表中的所有数据,也可以查询单个字段或者多个字段:
mysql> select host from mysql.db;
mysql> select host ,user,db from mysql.db;
同样,在查询语句中可以使用万能匹配 “%”
mysql> select * from mysql.db where host like '127.0.%'\G;
mysql> select host,user,db from db where host='127.0.0.1';
2. 插入一行
mysql> insert into tb1 values(7,'abcde');
mysql> select * from tb1;
mysql> insert into tb1 (id)values(11);
3. 更改表的某一行
mysql> update tb1 set name='aaa' where id=7;
4. 清空表数据
mysql> truncate table tb1;
5. 删除表
mysql> drop table tb1;
6. 删除数据库
mysql> drop database db1;
7.修复表
mysql> repair table user;
通过下面这条命令可以恢复库中*.MYI结尾的文件。
mysql> repair table user use_frm;
MySQL的备份与恢复
备份:
[root@cent11 ~]# mysqldump -uroot -p123456 -hlocalhost -P3306 discuz > 1.sql
恢复:
[root@cent11 ~]# mysql -uroot -p123456 -hlocalhost -P3306 discuz < 2.sql
备份和恢复的时候最好加上--default-character=gbk,指定字符集
[root@cent11 ~]# mysqldump -uroot -p123456 -hlocalhost -P3306 --default-character=gbk mysql > 1.sql
[root@cent11 ~]# mysql -uroot -p123456 -hlocalhost -P3306 --default-character=gbk mysql < 1.sql
MySQL密码忘了怎么办
# vim /etc/my.cnf
在 thread_concurrency = 8 下面加入一行:
skip-grant
然后重启MySQL:
# /etc/init.d/mysqld restart
在登录MySQL,它要你输入密码回车就好了:
# mysql -uroot -p
mysql> update mysql.user SET password=PASSWORD("123456") WHERE user='root';
然后进入配置文件中,把skip-grant删除掉,在重启。
配置一台MySQL服务器启动多个端口
在 /e tc/my.cnf 中
[mysqld0]
port = 3300
socket = /tmp/mysql0.sock
pid-file = /home/mysql0/localhost.localdomain0.pid
datadir = /home/mysql0
#log = /data/mysql0/mysql0.log
user = mysql
skip-locking
skip-name-resolve
#skip-bdb
#skip-innodb
key_buffer = 64M
max_allowed_packet = 1M
table_cache = 864
sort_buffer_size = 1M
read_buffer_size = 512K
read_rnd_buffer_size = 1M
myisam_sort_buffer_size = 32M
thread_cache_size = 16
query_cache_size = 32M
thread_concurrency = 8
#skip-networking
wait_timeout=8
max_connections=512
max_connect_errors = 10000000
max_user_connections=20
#slow_queries=/data/mysql0slowquer.sql
#log_slow_queries=/data/mysql0slowquer.sql
#long_query_time=3
#log-bin=mysql0-bin
###########################
[mysqld1]
port = 3301
socket = /tmp/mysql1.sock
pid-file = /home/mysql1/localhost.localdomain1.pid
datadir = /home/mysql1
#log = /data/mysql1/mysql1.log
user = mysql
skip-locking
skip-name-resolve
#加入上边这条命令可以避免由于解析主机名延迟造成的mysql执行慢
#skip-innodb
#skip-bdb
key_buffer = 64M
max_allowed_packet = 1M
table_cache = 864
sort_buffer_size = 1M
read_buffer_size = 512K
read_rnd_buffer_size = 1M
myisam_sort_buffer_size = 32M
thread_cache_size = 16
query_cache_size = 32M
thread_concurrency = 8
#skip-networking
wait_timeout=8
max_connections=512
max_connect_errors = 10000000
max_user_connections=20
#log_slow_queries=/data/mysql1slowquer.sql
#long_query_time=3
#log-bin=mysql1-bin 这个去掉,可以减少磁盘的i/o
#########################################