MySQL DBA 常用手册小结
MySQL DBA 常用手册小结,使用mysql的朋友可以参考下。
1.mysql的远程连接命令可以远程导入导出数据mysqldump --default-character-set=gb2312 -h255.255.000.00 -uroot -pxxxxxx mydatabase>d:\data.sql
将指定的数据库导出到一个外部SQL文件中去!
还原命令:
mysql --default-character-set=gb2312 -h255.255.000.00 -uroot -pxxxxxx mydatabase
2. MYSQL三种升级方法
安装办法:二进制包编译安装
第一种:适用于任何一种存储引擎。
1. 下载并安装好新版本的MySQL数据库,并将其端口改为3307(避免和旧版本的3306冲突),启动服务。
2. 在新版本下创建同名数据库。
# mysqldump -p3307 -uroot create mysqlsystems_com
3. 在旧版本下备份该数据库。
# mysqldump -p3306 -uroot mysqlsystems_com > mysqlsystems_com.bk
Note: 你也可以加上–opt选项,这样可以使用优化方式将你的数据库导出,减少未知的问题。
4. 将导出的数据库备份导入到新版本的MySQL数据库中。
# mysql -p3307 -uroot mysqlsystems_com
5. 再将旧版本数据库中的data目录下的mysql数据库全部覆盖到新版本中。
# cp -R /opt/mysql-5.1/data/mysql /opt/mysql-5.4/data(权限库保持不变)
Note: 大家也都知道这个默认数据库的重要性。
6. 在新版下执行mysql_upgrade命令,其实这个命令包含一下三个命令:
# mysqlcheck –check-upgrade –all-databases –auto-repair
# mysql_fix_privilege_tables
# mysqlcheck –all-databases –check-upgrade –fix-db-names –fix-table-names
Note: 在每一次的升级过程中,mysql_upgrade这个命令我们都应该去执行,它通过mysqlcheck命令帮我们去检查表是否兼容新版本的数据库同时 作出修复,还有个很重要的作用就是使用mysql_fix_privilege_tables命令去升级权限表。
7. 关闭旧版本,将新版的数据库的使用端口改为3306,重新启动新版本MySQL数据库。到此,一个简单环境下的数据库升级就结束了。
第二种,同样适用任何存储引擎。
1. 同样先安装好新版本的MySQL。
2. 在旧版本中,备份数据库。
# mkdir /opt/mysqlsystems_bk ; mysqldump -p3306 -uroot –tab=/opt/mysqlsystems_bk mysqlsystems_com
Note: –tab选项可以在备份目录mysqlsystems_bk下生成后缀为*.sql和*.txt的两类文件;其中,.sql保存了创建表的SQL语句而.txt保存着原始数据。
3. 接下来在新版本的数据库下更新数据。
# mysqladmin -p3307 -uroot create mysqlsystems_com
# cat /opt/mysqlsystems_bk/*.sql | mysql -p3307 -uroot mysqlsystems_com ( Create Tables )
# mysqlimport mysqlsystems_com /opt/mysqlsystems_bk/*.txt ( Load Data )
4. 之后的所有步骤与第一种方法的后三步5、6、7相同。
第三种,适用于MyISAM存储引擎,全部是文件间的拷贝。
1. 安装。
2. 从旧版本mysqlsystems_com数据库下将所有.frm、.MYD 和.MYI文件拷贝到新版本的相同目录下。
3.之后的步骤依然同于第一种的后三步。
以上就是三种升级MySQL的方法,看似没有出现什么问题,其实,在实际的生产环境中,为会有诸多问题发生,这就需要我们在升级之前充分了解新版本中增加了哪些新功能,进一步分析升级以后这些新特性是否将会对我们原来应用产生影响。
3. MYSQL远程连接不了的解决方案
有可能是这个用户权限不够。查看一下权限表。
4. MYSQL忘记密码解决办法
在windows下:
打开命令行窗口,停止mysql服务:Net stop mysql
到mysql的安装路径启动mysql,在bin目录下使用mysqld-nt.exe启动,在命令行窗口执行:mysqld-nt --skip-grant-tables
然后另外打开一个命入令行窗口,执行mysql,此时无需输入密码即可进入。
>use mysql
>update user set password=password("new_pass") where user="root";
>flush privileges;
>exit
使用任务管理器,找到mysqld-nt的进程,结束进程!
在重新启动mysql-nt服务,就可以用新密码登录了。
在linux下:
如果 MySQL 正在运行,首先杀之: killall -TERM mysqld。
启动 MySQL :bin/safe_mysqld --skip-grant-tables &
就可以不需要密码就进入 MySQL 了。
然后就是
>use mysql
>update user set password=password("new_pass") where user="root";
>flush privileges;
重新杀 MySQL ,用正常方法启动 MySQL 。
5. 更改MYSQL的默认字符集
法1、 用 SET 语法来指定,不加 "GLOBAL" 的话就只对本次会话有效
SET [GLOBAL] character_set_client = utf8;
SET [GLOBAL] character_set_connection = utf8;
SET [GLOBAL] character_set_database = utf8;
SET [GLOBAL] character_set_results = utf8;
SET [GLOBAL] character_set_server = utf8;
方法2、 也用SET语法,只对本次会话有效
SET NAMES 'utf8';
方法3、) 直接修改 my.cnf,增加一行内容,然后重启 MySQL,使之全局生效
default-character-set = utf8
6.MYSQL慢查询分析工具:mysqldumpslow
mysqldumpslow命令
/path/mysqldumpslow -s c -t 10 /database/mysql/slow-log
这会输出记录次数最多的10条SQL语句,其中:
* -s, 是表示按照何种方式排序,c、t、l、r分别是按照记录次数、时间、查询时间、返回的记录数来排序,ac、at、al、ar,表示相应的倒叙;
* -t, 是top n的意思,即为返回前面多少条的数据;
* -g, 后边可以写一个正则匹配模式,大小写不敏感的;
比如
/path/mysqldumpslow -s r -t 10 /database/mysql/slow-log
得到返回记录集最多的10个查询。
/path/mysqldumpslow -s t -t 10 -g “left join” /database/mysql/slow-log
得到按照时间排序的前10条里面含有左连接的查询语句。
查看mysql的某个选项
show variables like ‘%VAR_NAME%';
select @@VAR_NAME;
在Linux下管理MySQL数据库的时候总有一些很紧急的情况,发现数据库突然变得压力很大了,那么作为一个DBA,也许需要一些常用的手段或者说命令去分析问题出现在哪里,然后解决:
数据库突然产生压力时查看正在查询的SQL:(如果这里内容太多表示并发执行的SQL过多,或许数据库堵塞了,会越来越慢,正常情况下这里应该很少有东西的,也就是连接都在Sleep状态)
/usr/local/mysql/bin/mysql -uroot -ppassword databaseName -e “show full processlist” | grep -v Sleep
正在运行的SQL太多了,看不过来,那需要排序了,看持续执行时间最长的那些SQL:
/usr/local/mysql/bin/mysql -uroot -ppassword databaseName -e “show full processlist” | grep -v Sleep | sort -k6rn >sort.tmp
如果发现IOWait很高,请查看临时表的生成情况,特别是disk tmp table:
/usr/local/mysql/bin/mysql -uroot -ppassword databaseName -e “show global status like ‘%tmp%'”
通过这样一些办法可以查看数据库都在忙什么,那些忙的SQL又具体在哪一个步骤上卡住了,是在创建磁盘临时文件、Sending Data、statistics?依照不同的原因来解决问题
—————————————————————
关于Mysql Replication日常管理,重做,问题分析时常用的办法:
重做Slave,或者Master变化等等,需要将Slave与新的Master同步:
change master to master_host=IP,master_user='replication userName',master
_password='replication Passwrod',master_log_file='log-bin.000001′,master_log_pos=0;
导出数据成SQL文本,慎用,根据你的DB大小会锁表,导致堵塞其他访问:
nohup /usr/local/mysql/bin/mysqldump –database DATABASEName -uUserName -pPassWord –lock-all-tables -F >DATA20070519.sql &
-F后会刷新Master Log这样配合上面的Change Master可以让Slave进行同步
只导出数据库的结构(没有任何内容)
/usr/local/mysql/bin/mysqldump -d DATABASEName -uUserName -pPassWord >DATA20070519.structure
只导出数据库的数据(没有创建表结构的语句等等)
/usr/local/mysql/bin/mysqldump -t DATABASEName -uUserName -pPassWord >DATA20070519.data
同步的时候出现问题(或者其他问题)了,根据同步出现问题的位置(偏移量),查看Binlog的具体内容
/usr/local/mysql/bin/mysqlbinlog binlogFileName –start-position=偏移量
呵呵,我们碰到过Master执行的SQL到了Slave会报语法错误,够诡异吧!不过就是这样查到了原因:如果通过存储过程将bit的内容改为1就会出现这样的问题,后来将bit改为tinyint(1)就好了
授权给某一台Slave拥有复制的权限:
grant replication slave on *.* to 用户名@IP identified by ‘密码';
查看Slave状态:
Show slave status \G
查看Master状态:
Show master status;
重置Slave(慎用)
reset slave;
Slave出现问题了,先跳过这一条语句(请确认所要跳过的具体内容不会影响后面的同步,确认方法查看Binlog文件):
set global sql_slave_skip_counter=1; (记得先暂停Slave:stop slave; 然后重启Slave:start slave;)
———————————————–
纯粹Linux相关的:
tcpdump -A “dst port 3306″ 查看3306端口的通信具体内容

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











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 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.

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 and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA
