Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

September 5, 2015

Khắc phục lỗi giới hạn import CSDL vào phpMyAdmin bị giới hạn 2MB

Khắc phục lỗi của localhost:
Mở file:
php.ini (nếu dùng VertrigoServ thì vào phần Config files)

Tìm:
Sửa thành:
Khắc phục lỗi nạp CSDL vào phpMyAdmin bị giới hạn 2Mb (localhost):
Tìm:
Sửa
Khắc phục lỗi của .htaccess:
Dùng đoạn mã sau:
Sau đó restart lại apache

Công cụ, thủ thuật và kinh nghiệm tối ưu hóa MySQL trên VPS Linux

Nếu bạn sử dụng 1 VPS Linux mới cài nguyên bản LAMP lên, hẳn bạn sẽ gặp trường hợp VPS của bạn bị treo vì MySQL hoặc Apache gây quá tải RAM. Lỗi này thường được biết đến khi mà bạn chẳng tối ưu hóa các thông số kĩ thuật cho VPS. Từ quan điểm của 1 tay mơ, để VPS chạy tốt, bạn cần biết đến từng thành phần tối ưu hóa, ở đây trong bài viết này là MySQL.

CÔNG CỤ TỐI ƯU HÓA MYSQL

Ở đây chúng ta có 2 lựa chọn, chọn cái nào cũng được.

Lựa chọn 1: MySQL Performance Tuning Primer Script

Cài đặt bằng các dòng lệnh sau:
Lựa chọn 2 (phổ biến): MySQLTuner

Cài đặt bằng các dòng lệnh sau:
Dòng lệnh cuối cùng sau khi bạn chạy sẽ check thông tin MySQL của bạn và cho ra 1 số khuyến nghị (khuyến khích làm nhưng không phải là làm theo tuốt đâu nhé!). Chẳng hạn như:
TỐI ƯU HÓA THÔNG SỐ MYSQL

Sau khi bạn có 1 số thông tin cấu hình ở trên, bạn cần thực hiện các điều chỉnh. Thông thường các điều chỉnh này cần bạn sửa 1 file cấu hình của MySQL (thường nằm ở /etc/my.cnf) như sau:

Lưu ý: Bạn cần biết sử dụng editor (công cụ sửa file trên SSH). Ở đây mình thấy vi editor khá dễ sử dụng.
Cơ bản và nhanh để biết nhé:
+ Sửa file nào đó thì gõ lệnh vi <đường dẫn tới file>
+ Sửa nội dung file (sau khi vào trong file) thì ấn phím "Insert" trên bàn phím.
+ Lưu lại: ấn nút "Esc" trên bàn phím rồi gõ :wq! (gõ liền 1 cái rồi nhấn Enter thì sẽ lưu và quay trở lại command).

Bây giờ, quay trở lại, ta đang cần tối ưu hóa MySQL đúng không nào. Hãy bắt đầu bằng dòng lệnh sau:

Nội dung file sẽ hiện ra, chẳng hạn như với 1 VPS 500MB RAM thì đây là khuyến nghị của mình:
Còn với 1 VPS chỉ vẻn vẹn 200MB RAM, mình thấy cần cấu hình như sau:
Sau khi bạn thử thực hiện điều chỉnh thông số cho phù hợp, bạn lưu lại và thực hiện restart MySQL bằng các dòng lệnh sau:

Cuối cùng, bạn thử dòng lệnhtop -c để kiểm tra xem mức độ ổn định của VPS đã hơn chưa nhé.

LƯU Ý:
1, Hãy thực hiện công việc này lúc VPS ít khách nhất để tránh downtime vì MySQL có thể lỗi.
2, Nếu bạn restart MySQL mà không lên thì remove file my.cnf đi và thực hiện tunning lại, TỪNG DÒNG CẤU HÌNH MỘT.
Để xóa file my.cnf:
Để đổi tên file my.cnf:

August 15, 2015

phpMyAdmin - Error You should upgrade to MySQL 5.5.0 or later

I installed phpMyAdmin-4.2.9.1-english on a Centos 6.5 platform.
2.6.32-431.23.3.el6.x86_64 #1 SMP Thu Jul 31 17:20:51 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
When I login I get the following screen:
phpMyAdmin - Error

You should upgrade to MySQL 5.5.0 or later.
Solved:
I went to /usr/share/phpMyAdmin/libraries/common.inc.php and comment out this lines:
    #if (PMA_MYSQL_INT_VERSION < 50500) {
    #    PMA_fatalError(
    #        __('You should upgrade to %s %s or later.'),
    #        array('MySQL', '5.5.0')
    #    );
    #}
and now I can admin my mysql .

July 17, 2015

Chạy lệnh mysql không cần password

Thủ thuật này dành cho những người lười thôi. Còn nếu bạn lo ngại đến vấn đề bảo mật thì tốt nhất không nên sử dụng.
Tạo file nano ~/.my.cnf rồi điền thông tin MySQL username và password tương ứng:
[client]
user=mysqluser
password=mysqlpass
Bảo mật file chỉ có thể đọc với user root: chmod 0600 ~/.my.cnf
Như vậy từ giờ trở đi bạn có thể sử dụng các lệnh mysqlmysqlcheckmysqldump… không cần phải nhập username và password nữa.

July 14, 2015

Installing Custom MySQL Versions

If at all possible, you should use WHM to install MySQL, however, if you need a version of MySQL outside of what they are offering in current cPanel versions, it is possible to install your own versions.
These instructions can also be used to install MariaDB!  For more info on this, see cPanel’s blog.
It should be noted that some versions of cPanel do not officially support certain versions of MySQL, or installing MySQL in the fashion that is about to be described.  So do this at your own risk! Going to a minor version that is not supported by cPanel may also prevent cPanel updates from running at all.

Make Backups

If this is on a live system, make sure to make backups of the databases.
mkdir /root/dbbackups
touch /root/dbbackups/list
for db in `mysql -e 'show databases' |awk '{print $1}' | grep -v Database` ; do
    mysqldump --add-drop-table $db > /root/dbbackups/$db.sql && echo $db >> list
done

Download Packages

Download the binaries for the OS version and MySQL version you are using from the cPanel repo.  You can find MySQL packages on these sites:
You can also download the RPMs from MySQL’s website – though it’s not officially supported by cPanel, using the vendor RPM seems to work just fine.

Break up cPanel and MySQL

Now, you need to tell cPanel not to try to manage MySQL binaries anymore:
/scripts/update_local_rpm_versions –edit target_settings.MySQL50 unmanaged
/scripts/update_local_rpm_versions –edit target_settings.MySQL51 unmanaged
/scripts/update_local_rpm_versions –edit target_settings.MySQL55 unmanaged
/scripts/update_local_rpm_versions –edit target_settings.MySQL56 unmanaged

Uninstall Existing MySQL

To remove existing MySQL packages from the server:
/scripts/check_cpanel_rpms –fix –targets=MySQL50,MySQL51,MySQL55,MySQL56

Install New MySQL Packages

Go back to where you downloaded the packages to on your server, and install them:
rpm -Uvh mysql-*
Then check to see that the new version is indeed installed:
root@server [~]# mysql -V
mysql  Ver 14.14 Distrib 5.5.35, for Linux (x86_64) using readline 5.1
If you moved between minor versions (ie, 5.1 to 5.5), you should also run:
mysql_upgrade
You may also need to run EasyApache if PHP ceases to function.  However, you can get around this by copying the MySQL client libraries from /usr/lib[64] to another location before upgrading, then moving them back afterwards.  This is only a band-aid though, and should only be used to allow enough time to recompile PHP.

Restoring Database Privileges

I’ve seen several cases in the last few months where database users seemingly “disappear” or privileges appear to become invalid when restoring MySQL data or an entire server from raw backups. This may commonly be attributed to a MySQL version mismatch, for example, when a server is running MySQL 5.0 and then is suddenly running MySQL 5.1 or higher (or data is directly restored to a server running such), without having the database tables updated.
First things first, make sure to resolve the database version discrepancy, if one exists. Back up your data, then upgrade or downgrade MySQL to the version that your server should be running. Note that you should typically avoid downgrading MySQL to a previous version in order to prevent corrupting data. After you upgrade, run the command “mysql_upgrade” to fix the tables if the procedure in the referenced article doesn’t already do this for you.
To restore privileges:
Run the following command to dump the privilege table into a MySQL syntax that can be easily imported:
/scripts/grabmysqlprivs > /root/privileges.sql
*If you only need a certain user’s privileges, simply use grep to pull out that data:
/scripts/grabmysqlprivs | grep $user > /root/privileges.sql
On the server you need to restore to, simply import then SQL file:
mysql < /root/privileges.sql
This should be sufficient, however, if you see that the database users are not populated in cPanel, you can try remapping them:
/usr/local/cpanel/bin/setupdbmap

How to Change the Location of MySQL on cPanel

There may be some situations where you have to move the location of MySQL, for example, if you’re out of disk space or perhaps looking to host it on another device to increase performance. Whatever the reason, moving MySQL is simple and has no impact ct on cPanel’s functionality.

1) Create a backup

This should go without saying, but never mess with your data without making a backup of it. One simple way:
tar -cvf mysql.tar /var/lib/mysql

2) Modify my.cnf

In the [mysqld] section of /etc/my.cnf, add/modify this line:

datadir=/new/path/to/mysql

For example, if you are moving MySQL from /var/lib/mysql to /home2/mysql:

datadir=/home2/mysql

Don’t restart MySQL yet.

3) Sync the data

Now migrate the data to the new location using rsync. Typically you’ll want to stop MySQL, sync the data, then start it up again. If you have a lot of data and know the sync will take a while, do several syncs while the server is running, until they take less time. However, your last sync should always be done with MySQL stopped, especially if you have InnoDB tables. Here’s the command to sync with the example of MySQL being moved to /home2/mysql:
rsync -av /var/lib/mysql /home2
Now, relink the socket:
ln -sf /home2/mysql/mysql.sock /tmp

4) Restart MySQL

Since you already added the datadir entry to my.cnf, all you need to do is restart again and everything should be working.

or…
stop mysql
cd /var/lib
mv mysql /home
ln -s /home/mysql mysql
start mysql

Disk Space for MySQL Databases in cPanel Show as 0MB

When you upgrade from cPanel 11.24 to 11.25, your users may notice that in cPanel, their MySQL databases show 0MB of disk space used, when they know their databases are much larger. This is due to an option in cPanel that you specifically have to enable. To enable MySQL disk usage tracking:
1) Edit /var/cpanel/cpanel.config
Change:
disk_usage_include_sqldbs=0
to
disk_usage_include_sqldbs=1
Then run the following:
/scripts/update_db_cache
This may take a few minutes if you have a ton of users with databases, but after this, you should see the database disk usage show up accurately in cPanel.

July 2, 2015

MySQL Basics

Install MySQL Server

Debian/Ubuntu

[mitesh@Matrix ~]$ sudo apt-get update && sudo apt-get install -y mysql-server

Redhat/CentOS

[mitesh@Matrix ~]$ sudo yum install -y mysql-server

Important Files

Configuration File

  • /etc/mysql/my.cnf The Main MySQL Configuration File

Log Files

  • /var/log/mysql/ Default Log Directory For MySQL
  • /etc/logrotate.d/mysql-server Log Rotation Policy For MySQL Logs

MySQL Tricks

Reset MySQL Password

[mitesh@Matrix ~]$ sudo mysqladmin -u root password NEWPASSWORD
NOTE!: Add skip-grant-tables in /etc/mysql/my.cnf under the [mysqld] section and restart mysql server.

Create MySQL Database

[mitesh@Matrix ~]$ mysql -u USERNAME -pPASSWORD -e 'create database DB_NAME'

Remove MySQL Database

[mitesh@Matrix ~]$ mysql -u USERNAME -pPASSWORD -e 'drop database DB_NAME'

Create MySQL USER

[mitesh@Matrix ~]$ create user 'USERNAME'@'localhost' identified by 'PASSWORD';

Grant Privileges

[mitesh@Matrix ~]$ grant all privileges on `DB_NAME`.* to 'USERNAME'@'localhost';

Grant File Privileges

[mitesh@Matrix ~]$ grant file on . to 'USERNAME'@'localhost';
[mitesh@Matrix ~]$ grant file on *.* to 'USERNAME'@'localhost';

Update User Passwords

[mitesh@Matrix ~]$ UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
[mitesh@Matrix ~]$ set password for 'USERNAME'@'localhost' = password('PASSWORD');

Backup MySQL Database

[mitesh@Matrix ~]$ mysqldump -u USERNAME -h HOSTNAME -pPASSWORD $DB_NAME

Backup All MySQL Database

[mitesh@Matrix ~]$ mysqldump -u USERNAME -h HOSTNAME -pPASSWORD --all-databases

Restore MySQL Database

[mitesh@Matrix ~]$ mysql -u USERNAME -pPASSWORD DB_NAME < Database.sql

Export Specific Tables Only

[mitesh@Matrix ~]$ mysql DB_NAME -u USERNAME -p -e 'show tables like "wp_5%"' | grep -v Tables_in | xargs mysqldump DB_NAME -u root -p > SQLFILE.sql

June 29, 2015

Backup và Restore MySQL Database bằng lệnh

Nếu bạn đang sử dụng MySQL database, chắc chắn bạn sẽ không mong muốn dữ liệu của bạn bị mât. Do vậy việc backup MySQL database thường xuyên là cần thiết. Bạn có thể đơn giản hóa bằng cách sử dụng lệnh trong terminal để backup và restore

Bài viết hướng dẫn bạn cách đơn giản nhất để sao lưu và phục hồi dữ liệu MySQL database.

1. Backup bằng Command Line (sử dụng mysqldump để backup database)

Để có thể sử dụng dòng lệnh sao lưu cơ sở dữ liệu bạn cần đăng nhập vào server thông qua kết nối ssh như putty
Sử dụng cấu trúc lệnh như sau
$ mysqldump --opt -u [uname] -p [dbname] > [backupfile.sql]
Trong đó:
[uname] : user của database
[dbname] : Tên của database
[backupfile.sql] : Tên file backup muốn lưu
[--opt] : Các tùy chọn mysqldump
Ví dụ : Để backup database thuthuatvietnam
$ mysqldump -u root -p thuthuatvietnam > backup_ttvn.sql
Bạn cũng có thể tùy chọn các bảng để backup bằng cách liệt kê các bảng, các bảng cách nhau bằng khoảng trắng (dấu space)
$ mysqldump -u root -p thuthuatvietnam bang_a bang_b > backup_ttvn.sql
Back up MySQL Database và đồng thời nén lại
Nếu database có dung lượng lớn, bạn cần nén lại, khi đó bạn có thể sử dụng cấu trúc lệnh nén cùng gzip
$ mysqldump -u [uname] -p [dbname] | gzip -9 > [backupfile.sql.gz]

2. Restore MySQL Database

Ở trên là cách tạo bản backup, tiếp tục chúng ta sẽ tìm hiểu cách restore. Cấu trúc lệnh như sau:
$ mysql -u [uname] -p [dbname] < [backupfile.sql]
Trong đó:
[uname] : user của database
[dbname] : Tên của database
[backupfile.sql] : Tên file backup đã lưu (file backup muốn phục hồi)
Ví dụ: Phục hồi database thuthuatvietnam
$ mysql -u root -p thuthuatvietnam < backup_ttvn.sql
Phục hồi database được nén
gunzip < [backupfile.sql.gz] | mysql -u [uname] -p [dbname]
Nếu bạn muốn phục hồi một database đã tạo ra trước đó bạn sử dụng lệnh mysqlimport. Cấu trúc lệnh như sau:
mysqlimport -u [uname] -p [dbname] [backupfile.sql]
- See more at: http://thuthuatvietnam.com/backup-va-restore-mysql-database.html#sthash.MQpcXYgb.dpuf

June 26, 2015

Hướng dẫn nhận biết và xử lý khi cơ sở dữ liệu bị lỗi corrupted/crashed cần repair

1. Mục đích và nguyên nhân:
Bài viết này sẽ hướng dẫn quý khách hàng cách nhận biết và xử lý lỗi database bị corrupt hay crash
Thường thì do một số nguyên nhân nào đó,  ví dụ như HDD server đầyHDD bị BAD hay thiếu bộ nhớ RAM cho các tiến trình mysql hoạt động,..là những nguyên nhân chủ yếu dẫn đến lỗi trên. Hoặc do một số lỗi phát sinh làm cho server bị treophải reboot và sau khi reboot thì có thể một số table của các database bị crash
2. Một số dấu hiệu nhận biết:Một số dấu hiệu để chắc chắn rằng database đã bị crash thường có dạng như:
Database error: [Table 'table_name' is marked as crashed and should be repaired]
Hoặc
[ERROR] /usr/sbin/mysqld: Table './wordpress/table_name' is marked as crashed and should be repaired
Và đôi khi những lỗi dưới đây nếu xuất hiện thì cũng có khả năng đã xảy ra lỗi do database đã bị crash:
“Error establishing a database connection”
Hoặc
"ERROR! mysql server PID file could not be found"3. Cách xử lý:
Để xử lý lỗi trên quý khách cần kiểm tra lại các table nào của database bị crash bằng các cách sau:
a. Đối với dịch vụ share hosting:
- Truy cập vào trung tâm tài khoản khách hàng tại link: https://secure.vinahost.vn/ac/index.php -> Control Panel -> Tên gói dịch vụ share hosting của khách hàng.
- Sau khi vào Cpanel khách hàng chọn Quản lý cơ sở dữ liệu -> Danh sách cơ sở dữ liệu MYSQL 
Tại mục Modify Databases chọn database cần kiểm tra sau đó click "Check DB". Nếu table bị crash quý khách chọn database bị crash và click vào "Repair DB" để repair lại database này.
Quý khách cũng có thể repair từng table bằng cách sau:
- Sau khi vào Cpanel khách hàng chọn Quản lý cơ sở dữ liệu ->Chọn phpMyAdmin -> chọn database (bên phải) -> chọn table -> Chọn tiếp Operations. Khi đó quý khách sẽ thấy Check table và Repair table.
b. Đối với dịch vụ VPS/Server:
Quý khách có thể thao tác tương tự như gói dịch vụ share hosting nếu trên server của khách hàng có Cpanel License. Nếu không quý khách hàng có thể chạy lệnh sau để kiểm tra database:
 - Check toàn bộ các table của database:
       # mysqlcheck -c database_name  -u root –p
 - Check từng table trong database
       # mysqlcheck -c database_name  table_name  -u root –p
 - Check tất cả các database có trong hệ thống
       # mysqlcheck -c  -u root -p --all-databases 
Nếu table bị crash quý khách chạy lệnh sau để repair database:
   # mysqlcheck -r  database_name  table_name  -u root –p
Thay vì chạy lệnh cho từng database quý khách có thể sử dụng lệnh sau để kết hợp việc check và repair bằng cách thêm vào “--auto-repair”
   # mysqlcheck -u root -p --auto-repair -c  database_name

Sau khi thực hiện các lệnh trên quý khách vui lòng restart 
lại dịch vụ mysql bằng lệnh:
   # service mysqld restart