Ubuntu installs MySQL and modifies the database directory

Source: Internet
Author: User

.

.

.

.

.

Today, tossing the time of the afternoon, the recovery of countless virtual machine snapshots, finally on Ubuntu installed MySQL.

MySQL is downloaded from the official website: Mysql-server_5.7.16-1ubuntu12.04_i386.deb-bundle.tar

The system is 32-bit Ubuntu 12.04 LTS.

First put the downloaded Mysql-server_5.7.16-1ubuntu12.04_i386.deb-bundle.tar into the ~/tmp directory of the virtual machine, and then go to the directory to start the installation.

[Email protected]:~$mkdirTmp[email protected]-virtual-machine:~$ CD tmp/[email protected]-virtual-machine:~/tmp$sudoApt-getInstalllibaio1[sudo] Password foruser:reading Package Lists ... Donebuilding Dependency Tree Reading state information ... Donethe following NEW packages'll be installed:libaio1# ... Omit n rows here ... [Email protected]-virtual-machine:~/tmp$TarXF mysql-server_5.7.16-1ubuntu12.04_i386.deb-bundle.Tar[email protected]-virtual-machine:~/tmp$sudoDpkg-i Mysql-common_5.7.16-1ubuntu12.04_i386.deb# ... Omit n rows here ... [Email protected]-virtual-machine:~/tmp$sudoDpkg-i Libmysqlclient20_5.7.16-1ubuntu12.04_i386.deb# ... Omit n rows here ... [Email protected]-virtual-machine:~/tmp$sudoDpkg-i Libmysqlclient-dev_5.7.16-1ubuntu12.04_i386.deb# ... Omit n rows here ... [Email protected]-virtual-machine:~/tmp$sudoDpkg-i Libmysqld-dev_5.7.16-1ubuntu12.04_i386.deb# ... Omit n rows here ... [Email protected]-virtual-machine:~/tmp$sudoDpkg-i Mysql-community-client_5.7.16-1ubuntu12.04_i386.deb# ... Omit n rows here ... [Email protected]-virtual-machine:~/tmp$sudoDpkg-i Mysql-client_5.7.16-1ubuntu12.04_i386.deb# ... Omit n rows here ... [Email protected]-virtual-machine:~/tmp$sudoDpkg-i mysql-community-server_5.7.16-1ubuntu12.04_i386.deb# ... Omit n rows here ... update-alternatives:using/etc/mysql/mysql.cnf to PROVIDE/ETC/MYSQL/MY.CNF (MY.CNF)inchAuto mode ...* MySQL Community Server5.7. -is startedprocessing triggers forUreadahead Ureadahead is reprofiled on next rebootprocessing triggers for Mans-db ... [Email protected]-virtual-machine:~/tmp$

Explain the above actions:

We have to install libaio1 This library , or if first installed MySQL, a short time to report the lack of this library, then back to install this package will not be installed, do not know why, LZ flash back countless times the virtual machine snapshot to understand this routine. = =||

Then the MySQL installation package Mysql-server_5.7.16-1ubuntu12.04_i386.deb-bundle.tar extracted, will release a few files, must be installed in accordance with the order above!!! This is also the LZ analysis of the long-time error information only to ascertain the routine.

Of course, you can write a statement if you are not afraid to perform an error during execution:

sudo dpkg-i mysql-common_5. 7.16-1ubuntu12.04_i386.deb libmysqlclient20_5. 7.16-1ubuntu12.04_i386.deb libmysqlclient-dev_5. 7.16-1ubuntu12.04_i386.deb libmysqld-dev_5. 7.16-1ubuntu12.04_i386.deb mysql-community-client_5. 7.16-1ubuntu12.04_i386.deb mysql-client_5. 7.16-1ubuntu12.04_i386.deb mysql-community-server_5. 7.16-1ubuntu12.04_i386.deb

Note: When you install the last package, that is, mysql-community-server_5.7.16-1ubuntu12.04_i386.deb this package, you will be prompted to set the root password of the MySQL database.

After confirming the password, until we see it, it means it's installed, and the MySQL service has already started it for us.

5.7.  is started

At this point, if you want to verify, you can use the following command for a simple test:

[Email protected]:~/tmp$ mysql-u Root-Penter Password:welcome to the MySQL Monitor. Commands End With; or \g.your MySQL connectionIDIs2Server Version:5.7. -MySQL Community Server (GPL) Copyright (c) -, ., Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or Itsaffiliates. Other names trademarks of their respectiveowners. Type'Help ;'Or'\h'  forHelp. Type'\c'ToClearThe current input statement.mysql>show databases;+--------------------+| Database |+--------------------+| Information_schema | | MySQL | | Performance_schema | | SYS |+--------------------+4RowsinchSet (0.03sec) MySQL>Exitbye[email protected]-virtual-machine:~/tmp$

The-u parameter is followed by the user name of the login database;

-P indicates login with a password.

After landing, use show databases; You will be able to see the existing database.

It's not so boring----------------------------------------it's over----------------------------------------

Below LZ began a new death journey, modifying the database path.

By default, the database is saved in the/var/lib/mysql directory. As a general case, LZ will give/home a partition, but will not give/var a partition, so for the sake of security, LZ decided to move the database to the/home/mysqldata directory.

Note that the pure MV does not work, there must be some configuration in order for MySQL to know where his data is in the new home.

[Email protected]:~/tmp$sudoservice MySQL stop ...* MySQL Community Server5.7. -is Stopped[email protected]-virtual-machine:~/tmp$ cd/home/[email protected]-virtual-machine:/home$sudo CP-arp/var/lib/MySQL. [Email protected]-virtual-machine:/home$sudo MVMySQL mysqldata[email protected]-virtual-machine:/home$ lltotal -drwxr-xr-x4Root root4096Nov +  +: -./drwxr-xr-x atRoot root4096Nov +  -:Ten.. /drwxr-X---5MySQL MySQL4096Nov +  +: -mysqldata/drwxr-xr-x AUser User4096Nov +  +: -user/[email protected]-virtual-machine:/home$

First stop the MySQL service and then copy its data to the destination, that is, copy the/var/lib/mysql to/home/mysqldata.

Note the parameters of the CP command,-ARP is recursive replication and retains permissions .

Be sure to copy and then rename: MV MySQL mysqldata, or directly copy the words of permission may be a problem, like this:cp/var/lib/mysql./mysqldata. Of course, this step does not change the name is also possible, LZ just don't want to use the name of MySQL.

The next configuration:

Sudo

Edit the/etc/init.d/mysql script, search for keywords such as datadir, and modify it to look like this:

" /var/lib/mysql " ) mysqldata"/home/mysqldata")

The first line is commented out, LZ did not delete it, the second line is modified after the appearance.

This file may not be the same in different versions, because LZ in Baidu is not this way of writing, so you should also look at the changes in the circumstances appropriate changes.

Next, modify another script:

sudo vim-p/etc/mysql/my.cnf/etc/mysql/mysql.conf.d/2 files to edit

LZ this time opened two files, the purpose is to copy the contents of the/etc/mysql/mysql.conf.d/mysqld.cnf file into a/etc/mysql/my.cnf file.

The actual effect of course is the/etc/mysql/my.cnf file, here is a point to note: There may be more than one system MY.CNF, after the read will overwrite the first read configuration, if you want to know what order your system is read, you can look at the following way, so as not to find the reason for not to take effect after the change:

grep my.cnf/etc/my.cnf/etc/mysql/my.cnf ~/. my.cnf [email protected]

As can be seen, LZ here the priority is/ETC/MY.CNF </ETC/MYSQL/MY.CNF < ~/.my.cnf. Of course, the actual situation is the LZ system only/etc/mysql/my.cnf this one file, the other two are not created, so the LZ of course it is natural to modify this file.

Before modification:

# ... Omit N-line comment above...! includedir/etc/mysql/conf.d/!includedir/etc/mysql/mysql.conf.d/

After modification:

 # ... Omit n line comment above ... # !includedir/etc/mysql/conf.d/# !includedir/etc/mysql/ Mysql.conf.d/[mysqld]pid -file  =/var/ Run/mysqld/mysqld.pidsocket  =/var/run/mysqld/mysqld.sockdatadir  =/home/mysqldatalog -error =/var/log/mysql/ error.log# By default we are only accept connections from localhostbind -address = 
   
    127.0 . 
    0.1  
    # disabling symbolic -
    links is recommended to Prevent assorted security riskssymbolic -links=
    0  
   

Before the LZ in the online check of the post said also to change the socket file into/home/mysqldata/mysql/mysql.sock, after the experiment, do not change this path, otherwise you use MySQL every time The client's command has to be this way to connect to the database properly:

[Email protected]:/home$ mysql-u root-p-s/home/mysqldata/mysql/mysql.sock[email protected]

In fact, just need to copy the contents of the/etc/mysql/mysql.conf.d/mysqld.cnf file, and then modify the DataDir field is OK.

----------------------------------------you think it's over? That's naïve,----------------------------------------.

LZ pleased to perform the sudo service MySQL start, however ..... It doesn't start at all.

Is the authority wrong? is a configuration file not cleaned up?

LZ tried countless times chown-r mysql:mysql mysqldata and sudo service MySQL Stop/start, however there is no egg to use.

Later in the vast network found a life-saving straw (at the end of the link), the original security mechanism to make ghosts.

Then change:

Sudo

Sure enough, in this file, the MySQL database path before the modification was found:/var/lib/mysql.

Change to look like this:

dir Access   /var/lib/mysql/ R,  /var/lib/mysql*/* rwk,  /home/mysqldata/r,  /home/mysqldata/* * Rwk,

Divert, the original path to comment out, the new path to the configuration.

Restart the AppArmor service:

sudo* in/etc/apparmor.d/ in/etc/apparmor.d/disable: USR.SBIN.RSYSLOGD  [OK][email protected]-virtual-machine:/home$

OK, then finally the MySQL service can be started:

sudo5.7.  - is started[email protected]-virtual-machine:/home$

Wow, the start is successful, try it quickly.

[Email protected]:/home$ mysql-u Root-Penter Password:welcome to the MySQL Monitor. Commands End With; or \g.your MySQL connectionIDIs2Server Version:5.7. -MySQL Community Server (GPL) Copyright (c) -, ., Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or Itsaffiliates. Other names trademarks of their respectiveowners. Type'Help ;'Or'\h'  forHelp. Type'\c'ToClearThe current input statement.mysql>show databases;+--------------------+| Database |+--------------------+| Information_schema | | MySQL | | Performance_schema | | SYS |+--------------------+4RowsinchSet (0.03sec) MySQL>CREATE DATABASE live; Query OK,1Row affected (0.00sec) MySQL>show databases;+--------------------+| Database |+--------------------+| Information_schema | | Live | | MySQL | | Performance_schema | | SYS |+--------------------+5RowsinchSet (0.00sec) MySQL>Exitbye[email protected]-virtual-machine:/home$sudo lsmysqldata/auto.cnf CA.PEM Client-key.pem ibdata1 ib_logfile1 Live mysqld_safe.pid Private_key.pem server-Cert.pem Sysca-key.pem Client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 mysql performance_schema public_key.pem server-Key.pem[email protected]-virtual-machine:/home$

Above LZ created a new library called Live, and you can see that in the/home/mysqldata directory already has live this directory, indicating the new library migration success!

In the final step we can safely delete the old library. (Did you forget it?) )

sudo rm -rf/var/lib/mysql[email protected]

Resources:

MySQL hint [Warning] Cant Create test file xxx lower-test

Ubuntu installs MySQL and modifies the database directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.