Home Database Mysql Tutorial MySQL使用随笔

MySQL使用随笔

Jun 07, 2016 pm 03:33 PM
mysql use Check Version

001 查看版本 mysql --version mysql select version(); mysql status; 002 新建MySQL用户、授权 insert into mysql.user(Host,User,Password) values(localhost,username,password(yourpassword)); GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost'

001 查看版本

mysql --version
mysql > select version();
mysql > status;

002 新建MySQL用户、授权

insert into mysql.user(Host,User,Password) values("localhost","username",password("yourpassword"));

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY "yourpassword" WITH GRANT OPTION; 

flush privileges;

003. 查询支持的存储引擎
show engines;

004 查看变量
show variables like 'innodb%';

005 导入数据库(sql文件)
mysql -uroot -p dbname
006 my.cnf说明
[client] #客户端配置
port                              = 3306
socket                         = /var/lib/mysql/mysql.sock #本地客户端使用这个socket链接mysqld
default_character_set   = utf8
secure_auth                    = 0                         #跳过密码格式不统一问题                 

[mysqld]
datadir=/usr/local/mysql/data        #指定数据路径
socket=/var/lib/mysql/mysql.sock #使用这个socket启动server
user=mysql
old_passwords = 1
secure_auth = 0
innodb_force_recovery=0          #必须为0,否则innodb就是read only模式
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

007 修改root密码
mysqld_safe --skip-grant-tables
mysql > update mysql.user set password=password("new_pass") where user="root";

008 让mysql服务器输出所有执行的SQL语句记录

mysqld --general-log=TRUE

日志输出为mysql服务器数据目录的localhost.log, 其他日志请执行mysqld --verbose --help查看

009 数据库导出导入

导出数据
mysqldump -uusername -ppassword databasename > databasename.sql
导出存储过程和函数
mysqldump -uusername -ppassword -ntd -R databasename > stored-procs.sql
只导出表结构
mysqldump -uusername -ppassword -d --add-drop-table databasename > table-init.sql
导入sql文件(可以为数据,表结构,存储过程...)
mysql -uusername -ppassword databasename

010 查看当前哪些线程在运行

mysql >show processlist;

MySQL使用随笔

在Info字段还可以查看执行的语句,如果有执行很慢的语句,可以直接查询到。

011 删除表记录

DELETE FROM tablename

TRUNCATE TABLE tablename

012 mysql存储过程的sql secuirty
mysql存储过程中sql secuirty决定了执行相关存储过程时的安全限制,sql secuirty可以为DEFINER或者INVOKER。
DEFINER表示执行此存储过程的用户必须是存储过程创建者且必须存在这个用户并有对应权限。
INVOKER表示执行此存储过程的用户必须是存在的用户且这个用户并有对应权限。

创建存储过程时,可以指定sql secuirty,以下例子指定sql secuirty为INVOKER。
CREATE DEFINER = 'admin'@'localhost' PROCEDURE account_count()
SQL SECURITY INVOKER
BEGIN
  SELECT 'Number of accounts:', COUNT(*) FROM mysql.user;
END;

也可以通过修改mysql.proc表中对应字段security_type来设定sql secuirty。例
update mysql.proc set security_type = ...

alter procedure pro_name sql security invoker;

013 数据表增加字段(时间字段)

mysql> alter table mytable add column ctime datetime DEFAULT NOW() NOT NULL after flag;  
Query OK, 0 rows affected (0.26 sec)
Records: 0  Duplicates: 0  Warnings: 0

014 左、右join

语法:

SELECT * FROM tableA
LEFT|RIGHT JOIN tableB
ON tableA.field1 = tableB.field2
左JOIN:以左(tableA)表为基础查出所有符合条件的tableA和tableB,tableB不存在的记录显示tableB相关字段为NULL;

右JOIN:以右(tableB)表为基础查出所有符合条件的tableA和tableB,tableA不存在的记录显示tableA相关字段为NULL;

015 修改列定义,定义索引

ALTER TABLE table_name modify column_name varchar(32);
ALTER TABLE table_name ADD INDEX `column_name` (`column_name`);

016 make mysql

http://dev.mysql.com/doc/internals/en/cmake-build-options-official-mysql.html

  • Unix (Makefiles)

    mkdir bld
    cd bld
    cmake .. -DBUILD_CONFIG=mysql_release
    make
    
    Copy after login

注:安装libaio后如果还是提示依赖,重新执行上面步骤(删除bld)

017 mysql升级后无法赋用户权限,修正

2014-05-27 14:48:34 17291 [ERROR] Missing system table mysql.proxies_priv; please run mysql_upgrade to create it
2014-05-27 14:48:34 17291 [Warning] Info table is not ready to be used. Table 'mysql.slave_master_info' cannot be opened.
2014-05-27 14:48:34 17291 [Warning] Info table is not ready to be used. Table 'mysql.slave_relay_log_info' cannot be opened.
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'cond_instances' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_current' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_history' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_history_long' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_summary_by_host_by_event_name' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_summary_by_instance' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_summary_by_thread_by_event_name' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_summary_by_user_by_event_name' has the wrong structure
2014-05-27 14:48:34 17291 [ERROR] Native table 'performance_schema'.'events_waits_summary_by_account_by_event_name' has the wrong structure

解决办法:
mysql_upgrade -u root -p
Copy after login


018  sql_mode doesn't have a default value null 

现象是无法导入存储过程,提示如题所示的错误,解决办法,修改配置文件中sql_mode,注释掉重新启动MySQL

#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

如果发现执行存储过程时有默认值的情况仍旧无法插入数据,也可能是这个原因,这时要直接修改mysql.proc表中对应存储过程的sql_mode为NO_ENGINE_SUBSTITUTION即可

详细信息查看这里

也可以直接连接到mysql执行source /path/to/sql_file.sql


019 MySQL安装后没有mysql数据库

运行安装目录中的./scripts/mysql_install_db

020 mysql连接localhost

安装mysql后,只能使用localhost作为host连接,无法使用其他远程主机连接,纠结许久(防火墙好像没有限制啊!),发现mysqld是以13306端口启动的,但应用server的数据库配置中写的是3306,仍然连接成功。为什么?

原因是mysql连接localhost使用的是socket文件,而不是tcp连接,因此指定端口是无效的。如下所示的13306端口会被忽略

shell> <strong><code>mysql --port=13306 --host=localhost</code></strong>
Copy after login
如果需要使用TCP连接,则需要指定--host为不同于localhost,或指定以TCP方式连接。
shell> <strong><code>mysql --port=13306 --host=127.0.0.1</code></strong>
shell> <strong><code>mysql --port=13306 --protocol=TCP</code></strong>
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

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's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

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

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

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.

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

See all articles