mysql目录
1. 数据库目录
/var/lib/mysql
2. 配置文件
/usr/share/mysql(mysql.server命令及配置文件)
3. 相关命令
/usr/bin(mysqladmin mysqldump等命令)
4. 启动脚本
/etc/rc.d/init.d/(启动脚本文件mysql的目录)
安装
# yum -y install mysql*
# service mysqld start
# netstat -tlpn | grep mysql
# mysql
MySQL默认没有密码,安装完毕增加密码的重要性是不言而喻的
# /usr/bin/mysqladmin -u root password redhat
# mysql
ERROR 1045: Access denied for user: 'root@localhost' (Using password: NO)
显示错误,说明密码已经修改
用修改后的密码登录
# mysql -u root -p
Enter password: (输入修改后的密码redhat)
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 4 to server version: 4.0.16-standard
Type 'help;' or '/h' for help. Type '/c' to clear the buffer.
mysql>
这是通过mysqladmin命令修改口令,也可通过修改库来更改口令。
启动与停止
1. 启动
MySQL安装完成后启动文件mysql在/etc/init.d目录下,在需要启动时运行下面命令即可
# /etc/init.d/mysqld start
2. 停止
# /usr/bin/mysqladmin -u root -p shutdown
3. 自动启动
1) 察看mysql是否在自动启动列表中
# /sbin/chkconfig --list
2) 把mysql添加到系统的启动服务组里面去
# /sbin/chkconfig -- add mysqld
3) 把mysql从启动服务组里面删除
# /sbin/chkconfig --del mysqld
mysql的常用操作
1. 显示数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql刚安装完有三个数据库:information_schema、mysql和test。mysql库非常重要,它里面有MySQL的系统信息,我们改密码和新增用户,实际上就是用这个库中的相关表进行操作。
2. 显示数据库中的表
mysql> use mysql; (打开库,对每个库进行操作就要打开此库,类似于foxpro )
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| func |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| proc |
| procs_priv |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
17 rows in set (0.01 sec)
3. 显示数据表的结构
describe 表名;
mysql> describe user;
4. 显示表中的记录
select * from 表名
mysql> select * from user;
5. 建库
create database 库名
mysql> create database lifang;
Query OK, 1 row affected (0.01 sec)
6. 建表
use 库名
create table 表名(字段设定列表);
例如:在刚创建的lifang库中建立表name,表中有id(序号,自动增长),xm(姓名),xb(姓别),csny(出身年月)四个字段
mysql> use lifang;
Database changed
mysql> create table name (id int(3) auto_increment not null primary key,xm char(8),xb char(2),csny date);
Query OK, 0 rows affected (0.01 sec)
可以用describe命令察看刚建立的表结构
mysql> describe name
-> ;
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| id | int(3) | NO | PRI | NULL | auto_increment |
| xm | char(8) | YES | | NULL | |
| xb | char(2) | YES | | NULL | |
| csny | date | YES | | NULL | |
+-------+---------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
7. 增加记录
mysql> insert into name values('','lifang','female','1984-05-10');
Query OK, 1 row affected, 2 warnings (0.00 sec)
mysql> insert into name values('','fuying','female','1986-07-14');
Query OK, 1 row affected, 2 warnings (0.00 sec)
mysql> insert into name values('','xiaodi','female','1982-12-28');
Query OK, 1 row affected, 2 warnings (0.00 sec)
可用select命令来验证结果
mysql> select * from name;
+----+--------+------+------------+
| id | xm | xb | csny |
+----+--------+------+------------+
| 1 | lifang | fe | 1984-05-10 |
| 2 | fuying | fe | 1986-07-14 |
| 3 | xiaodi | fe | 1982-12-28 |
+----+--------+------+------------+
3 rows in set (0.00 sec)
8. 修改记录
例如将xiaodi的出生年月改为1985-12-28
mysql> update name set csny='1985-12-28' where xm='xiaodi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
9. 删除记录
mysql> delete from name where xm='xiaodi';
Query OK, 1 row affected (0.00 sec)
10. 删库和删表
drop database 库名
drop table 表名
mysql> drop table name;
Query OK, 1 rows affected (0.00 sec)
mysql> drop database lifang;
Query OK, 1 rows affected (0.00 sec)
增加mysql用户
格式:grant select on 数据库.* to 用户名@登录主机 identified by “密码”
例1、 增加一个用户fuyiing密码为redhat,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MySQL,然后键入以下命令
mysql> grant select,insert,update,delete on *.* to fuying@station12 identified by "redhat";
Query OK, 1 rows affected (0.00 sec)
增加的用户是十分危险的,如果知道了fuying的密码,那么他就可以在网上的任何一台电脑上登录你的MySQL数据库并对你的数据为所欲为了,解决办法见例2
例2、 增加一个用户xiaodi密码为redhat,让此用户只可以在localhost上登录,并可以对数据库lifang进行查询、插入、修改、删除的操作(localhost指本地主机,即MySQL数据库所在的那台主机),这样用户即使用知道xiaodi的密码,他也无法从网上直接访问数据库,只能通过 MYSQL主机来操作lifang库
mysql> grant select,insert,update,delete on lifang.* to xiaodi@localhost identified by "redhat";
Query OK, 1 rows affected (0.00 sec)
用新增的用户如果登录不了MySQL,在登录时用如下命令:
# mysql -u fuying -p -h 192.168.0.12 (-h后跟的是要登录主机的ip地址)
备份与恢复
1. 备份
例如:将上例创建的lifang库备份到文件back_lifang中
[root@station12 mysql]# mysqldump -u root -p --opt lifang > back_lifang
Enter password:
[root@station12 mysql]#
2. 恢复
[root@station12 mysql]# mysql -u root -p lifang
Enter password:
[root@station12 mysql]#

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools