Preface installing MySQL is an old topic. I have installed the MySQL server for no less than a hundred times. To ensure the consistency of the blog structure, I should write another article as the environment basis and give myself a memo. Directory MySQL installation in LinuxUbuntu access MySQL through the command line Client modify MySQL server configuration new database and set the access account
Preface installing MySQL is an old topic. I have installed the MySQL server for no less than a hundred times. To ensure the consistency of the blog structure, I should write another article as the environment basis and give myself a memo. Directory MySQL installation in Linux Ubuntu access MySQL through the command line Client modify MySQL server configuration new database and set the access account
Preface
Installing MySQL is an old topic. I have installed the MySQL server for no less than a hundred times. To ensure the consistency of the blog structure, I should write another article as the environment basis and give myself a memo.
Directory
- Install MySQL in Linux Ubuntu
- Access MySQL through the command line Client
- Modify the configuration of the MySQL server
- Create a database and set an access account
- Change Data storage location
1. Install MySQL in Linux Ubuntu
The Linux system used in this article is Ubuntu 12.04.2 LTS 64bit. you can install the MySQL database package through apt-get.
Install MySQL database in Linux Ubuntu
# Install the MySQL server ~ Sudo apt-get install mysql-server
A prompt box is displayed during the installation process. Enter the password of the root user. Here, set the password to mysql.
After the installation is complete, the MySQL server will automatically start. We check the MySQL server program.
# Check the system process of the MySQL server ~ Ps-aux | grep mysqlmysql 3205 2.0 0.5 549896 44092? Ssl/usr/sbin/mysqldconan 3360 0.0 0.0 11064 pts/0 S + grep -- color = auto mysql # Check the port occupied by the MySQL server ~ Netstat-nlt | grep 3306tcp 0 0 127.0.0.1: 3306 0.0.0.0: * LISTEN # Run the startup command to check the MySQL server status ~ Sudo/etc/init. d/mysql statusmysql start/running, process 3205 # Check the MySQL server status through System Service commands ~ Service mysql statusmysql start/running, process 3205
2. Access MySQL through the command line Client
After the MySQL server is installed, the MySQL command line client program is automatically installed together.
Enter the mysql command on the local machine to start MySQL. The client program accesses the mysql server.
~ mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 42Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu)Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
Use username and password to log on to the server
~ mysql -uroot -pEnter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 37Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu)Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
Some simple command operations of MySQL.
# View all databases mysql> show databases; + -------------------- + | Database | + -------------------- + | information_schema | test | + ------------------ + 2 rows in set (0.00 sec) # Switch to information_schema database mysql> use information_schemaReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with-ADatabase changed # view all tables in information_schema database mysql> show tables; + variables + | Tables_in_information_schema | + variables + | CHARACTER_SETS | COLLATIONS | EVENTS | COLUMNS | COLUMN_PRIVILEGES | ENGINES | EVENTS | FILES | GLOBAL_STATUS | GLOBAL_VARIABLES | KEY_COLUMN_USAGE | PARAMETERS | PARTITIONS | PLUGINS | PROCESSLIST | PROFILING | metrics | ROUTINES | SCHEMATA | metrics | SESSION_STATUS | SESSION_VARIABLES | STATISTICS | TABLES | TABLESPACES | TABLE_CONSTRAINTS | TABLE_PRIVILEGES | TRIGGERS | USER_PRIVILEGES | VIEWS | catalog | INNODB_TRX | catalog | INNODB_CMP | INNODB_LOCKS | | INNODB_CMPMEM_RESET | INNODB_CMP_RESET | INNODB_BUFFER_PAGE_LRU | + ------------------------------------- + 40 rows in set (0.01 sec) # view the character set encoding of the database mysql> show variables like '% char % '; + bytes + | Variable_name | Value | + bytes + | character_set_client | utf8 | character_set_connection | utf8 | character_set_database | utf8 | bytes | binary | utf8 | | character_set_server | latin1 | character_set_system | utf8 | character_sets_dir |/usr/share/mysql/charsets/| + ---------------------- + rows + 8 rows in set (0.00 sec)
3. modify the configuration of the MySQL server
Next, I need to configure MySQL to meet basic development requirements.
4.1 set character encoding to UTF-8
By default, MySQL character set is latin1, so in the storage of Chinese, there will be garbled, so we need to unified character set into UTF-8.
Use vi to open the MySQL server configuration file my. cnf
4.2 remote access to the MySQL server
By default, the MySQL server does not allow remote access, but only local access. Therefore, you need to enable the remote access function.
Use vi to open the MySQL server configuration file my. cnf
~ Sudo vi/etc/mysql/my. cnf # comment bind-address # bind-address = 127.0.0.1
After modification, restart the MySQL server.
~ sudo /etc/init.d/mysql restartRather than invoking init scripts through /etc/init.d, use the service(8)utility, e.g. service mysql restartSince the script you are attempting to invoke has been converted to anUpstart job, you may also use the stop(8) and then start(8) utilities,e.g. stop mysql ; start mysql. The restart(8) utility is also available.mysql start/running, process 3577
Log on to the server again
~ Mysql-uroot-pEnter password: Welcome to the MySQL monitor. commands end with; or \ g. your MySQL connection id is 37 Server version: 5.5.35-0ubuntu0. 12.04.2 (Ubuntu) Copyright (c) 2000,201 3, Oracle and/or its affiliates. all rights reserved. oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. other names may be trademarks of their respectiveowners. type 'help; 'or' \ H' for help. type '\ C' to clear the current input statement. # Check the string encoding mysql> show variables like '% char %' again '; + bytes + | Variable_name | Value | + bytes + | character_set_client | utf8 | character_set_connection | utf8 | character_set_database | utf8 | bytes | binary | utf8 | | character_set_server | utf8 | character_set_system | utf8 | character_sets_dir |/usr/share/mysql/charsets/| + ---------------------- + rows + 8 rows in set (0.00 sec)
Check the network listening port of MySQL.
# Check the port occupied by the MySQL server ~ Netstat-nlt | grep 3306 tcp 0 0 0.0.0.0: 3306 0.0.0.0: * LISTEN
We can see that the network listener from 127.0.0.1: 3306 to 0 0.0.0.0: 3306 indicates that MySQL has allowed remote access. Remote access using the root account is very insecure. Therefore, we will create a new database and then create a new user for remote access.
4. Create a database and set an access account
Log on to the MySQl server using the root account
~ Mysql-uroot-pEnter password: Welcome to the MySQL monitor. commands end with; or \ g. your MySQL connection id is 39 Server version: 5.5.35-0ubuntu0. 12.04.2 (Ubuntu) Copyright (c) 2000,201 3, Oracle and/or its affiliates. all rights reserved. oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. other names may be trademarks of their respectiveowners. type 'help; 'or' \ H' for help. type '\ C' to clear the current input statement. # creating DATABASE abcmysql> CREATE Database abc; # using DATABASE abcmysql> use abc; Database changed # In DATABASE abc, create a table a1mysql> CREATE table a1 (id int primary key, name varchar (32) not null); Query OK, 0 rows affected (0.05 sec) # create a book user with the password book, and allow the book to remotely access the abc database, authorize book to perform ALL database mysql> grant all on abc for abc. * to book @ '%' identified by 'book'; Query OK, 0 rows affected (0.00 sec) # Allow the book to access the abc database locally, authorize book to perform ALL database mysql> grant all on abc for abc. * to book @ localhost identified by 'book'; Query OK, 0 rows affected (0.00 sec)
We use the book user to log on locally.
# Use a book user to log in ~ Mysql-ubook-pEnter password: Welcome to the MySQL monitor. commands end with; or \ g. your MySQL connection id is 40 Server version: 5.5.35-0ubuntu0. 12.04.2 (Ubuntu) Copyright (c) 2000,201 3, Oracle and/or its affiliates. all rights reserved. oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. other names may be trademarks of their respectiveowners. type 'help; 'or' \ H' for help. type '\ C' to clear the current input statement. # mysql> use abc; reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with-ADatabase changed # view the table mysql> show tables of the abc database; + --------------- + | Tables_in_abc | + ----------------- + | a1 | + --------------- + 1 row in set (0.00 sec)
We use the book user to log on to another remote Linux
~ mysql -ubook -p -h 192.168.1.199Enter password:Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 41Server version: 5.5.35-0ubuntu0.12.04.2 (Ubuntu)Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> use abcReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> show tables;+---------------+| Tables_in_abc |+---------------+| a1 |+---------------+1 row in set (0.00 sec)
5. Change the data storage location
Sometimes we may need to change the location of MySQL Data Storage. One way is to directly modify the configuration file/etc/mysql/my. cnf and find the datadir Attribute Modification directory.
~ vi /etc/mysql/my.cnf[mysqld]datadir = /var/lib/mysql
If this method is used, we also need to modify the other places where the storage path is called. For example, the/usr/bin/mysql_install_db command is used, the properties of ldata in the file also need to be modified. For details about how to use the mysql_install_db command, refer to the Article. [MySQL optimization] reduced the size of the MySQL Data File ibdata1.
Another way to modify the storage location is through the Linux system's soft connection (ln-s. When we mount a new hard disk, stop the MySQL service, and then move the/var/lib/mysql directory to the new hard disk storage, just create a soft connection at the specified position in/var/lib/mysql.
# Stop the MySQL server ~ /Etc/init. d/mysql stop # attach the hard disk ~ Mount-t ext4/dev/vdb1/vdb1 # create a new storage directory ~ Mkdir/vdb1/data # Move the MySQL data DIRECTORY to the new directory ~ Mv/var/lib/mysql/vdb1/data/# soft connection ~ Ln-s/vdb1/data/mysql/var/lib/mysql
Modify the alias definition file of apparmor
~ vi /etc/apparmor.d/tunables/aliasalias /var/lib/mysql/ -> /vdb1/data/mysql/,
Note: If the apparmor configuration is not modified, MySQL cannot be started and the permission issue is always prompted.
# Restart the apparmor service ~ /Etc/init. d/apparmor restart # restart the MySQL server ~ /Etc/init. d/mysql start
This completes the modification of the MySQL data storage location.
Through the above operation, we have installed the MySQL database server in the Linux Ubuntu system.