Ubuntu 12.04 安装MySQL
本文地址:http://www.cnblogs.com/yhLinux/p/4012689.html
本文适合新手入门。
本文是对 Ubuntu 12.04 环境下安装 MySQL 的记录,通过这两天的折腾,不断试错,才发现要完整地掌握 MySQL 安装其实并不复杂,关键是遇到问题时记住到官方文档寻找解决方案,在网络上搜索的网友的方法不一定适合你,而官方文档是比较全面的,建议大家有问题多看看。
安装MySQL的方法有多种方式,包括源码安装,包管理器安装,二进制安装等。之前,我使用最快的方式——包管理器安装,但安装完成后,对MySQL的很多文件的作用完全是迷茫的,不知任何用处;而采用源码方式安装,又需要设置更多的编译选项,折中的办法是选择二进制安装,即Generic Binaries,在Linux下就主要分为三步完成(创建mysql用户,解压安装包,安装后设置),看上去是不是和Windows下的绿色软件安装很像吧?而且,通过此安装配置过程,读者可以了解到 MySQL 安装布局,方便你轻松掌握 MySQL 的各类文件作用,对了解 MySQL 以快速入门有很大帮助。好了,废话不多说,开始安装 MySQL 吧。
Ubuntu12.04 以通用可执行文件(Generic Binaries)安装MySQL
目录:
选择安装版本:
[http://dev.mysql.com/doc/refman/5.6/en/choosing-version.html]
Normally, if you are beginning to use MySQL for the first time or trying to port it to some system for which there is no binary distribution, use the most recent General Availability series listed in the preceding descriptions.由此,本文选择安装MySQL 5.6: Latest General Availability (Production) release
MySQL 安装选择上说:“We put a great deal of effort into ensuring that our binaries are built with the best possible options for optimal performance.”,由此可知,Generic Binaries安装有诸多优化,所以我们按此链接来做,Installing from a generic binary package in.tar.gz
format.
See Section 2.2, “Installing MySQL on Unix/Linux Using Generic Binaries” for more information.
下载http://dev.mysql.com/downloads/mysql/#current-tab,我的系统是64位的,对应Linux - Generic (glibc 2.5) (x86, 64-bit), Compressed TAR Archive,下载mysql-5.6.21-linux-glibc2.5-x86_64.tar.gz,md5值79f9a54bdc3731fd4bdf22db77f27ca7
If you have previously installed MySQL using your operating system native package management system, such as yum
or apt-get
, you may experience problems installing using a native binary. Make sure your previous MySQL previous installation has been removed entirely (using your package management system), and that any additional files, such as old versions of your data files, have also been removed. You should also check the existence of configuration files such as /etc/my.cnf
or the /etc/mysql
directory have been deleted.
sudo apt-get autoremove --purge mysql-server-5.5
sudo apt-get remove mysql-server
sudo apt-get autoremove mysql-server
sudo apt-get remove mysql-common
Table 2.3 MySQL Installation Layout for Generic Unix/Linux Binary Package
Directory | Contents of Directory |
---|---|
bin |
Client programs and the mysqld server |
data |
Log files, databases |
docs |
Manual in Info format |
man |
Unix manual pages |
include |
Include (header) files |
lib |
Libraries |
scripts |
mysql_install_db |
share |
Miscellaneous support files, including error messages, sample configuration files, SQL for database installation |
sql-bench |
Benchmarks |
记得删除之前安装所残余的mysql文件,然后:
做完以上准备工作,现在我们来预览一下整个安装过程。
安装流程概览:
To install and use a MySQL binary distribution, the basic command sequence looks like this:
//Create a mysql User and Group
shell> groupadd mysql
shell> useradd -r -g mysql mysql //Obtain and Unpack the Distribution
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql //Perform Postinstallation Setup
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql . shell> apt-get install libaio1 libaio-dev
shell> ./scripts/mysql_install_db --user=mysql shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server
以上安装命令是全部的动作,一共分为三部分(创建mysql用户,解压安装包,安装后设置),每一步骤的解释都有对应的官方文档,以下一步一步来做:
2.2 Installing MySQL on Unix/Linux Using Generic Binaries
//Create a mysql User and Group
shell> groupadd mysql
shell> useradd -r -g mysql mysql //Obtain and Unpack the Distribution
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
Section 2.10, “Postinstallation Setup and Testing”.
------------>2.10.1 Postinstallation Procedures for Unix-like Systems
//Perform Postinstallation Setup
.
shell> cd mysql .(以root权限运行)
shell> chown -R mysql .
shell> chgrp -R mysql . .(以root权限运行)
shell> apt-get install libaio1 libaio-dev
shell> ./scripts/mysql_install_db --user=mysql
3.The mysql_install_db program creates the server's data directory with mysql
as the owner. Under the data directory, it creates directories for the mysql
database that holds the grant tables and the test
database that you can use to test MySQL.The script also creates privilege table entries for root
and anonymous-user accounts. The accounts have no passwords initially.
It is important to make sure that the database directories and files are owned by the mysql
login account so that the server has read and write access to them when you run it later. To ensure this if you run mysql_install_db as root
, include the --user
option as shown. Otherwise, you should execute the script while logged in as mysql
, in which case you can omit the --user
option from the command.
4.Most of the MySQL installation can be owned by root
if you like. The exception is that the data directory must be owned by mysql
. To accomplish this, run the following commands as root
in the installation directory:
.(以root权限运行)
shell> chown -R root .
shell> chown -R mysql data
5.暂无
6.If you want MySQL to start automatically when you boot your machine, you can copy support-files/mysql.server
to the location where your system has its startup files. More information can be found in the mysql.server
script itself, and inSection 2.10.1.2, “Starting and Stopping MySQL Automatically”. 如下:
Generally, you start the mysqld server in one of these ways:
,,Invoke mysqld_safe, which tries to determine the proper options for mysqld and then runs it with those options. This script is used on Unix and Unix-like systems. SeeSection 4.3.2, “mysqld_safe — MySQL Server Startup Script”.
,,Invoke mysql.server. This script is used primarily at system startup and shutdown on systems that use System V-style run directories (that is,/etc/init.d
and run-level specific directories), where it usually is installed under the name mysql
. The mysql.server script starts the server by invoking mysqld_safe. See Section 4.3.3, “mysql.server — MySQL Server Startup Script”.
[插播4.1 Overview of MySQL Programs]
The MySQL server, mysqld, is the main program that does most of the work in a MySQL installation. The server is accompanied by several related scripts that assist you in starting and stopping the server:
-
The SQL daemon (that is, the MySQL server). To use client programs, mysqld must be running, because clients gain access to databases by connecting to the server. See Section 4.3.1, “mysqld — The MySQL Server”.
-
A server startup script. mysqld_safe attempts to start mysqld. See Section 4.3.2, “mysqld_safe — MySQL Server Startup Script”.
-
A server startup script. This script is used on systems that use System V-style run directories containing scripts that start system services for particular run levels. It invokes mysqld_safe to start the MySQL server. See Section 4.3.3, “mysql.server — MySQL Server Startup Script”.
-
A server startup script that can start or stop multiple servers installed on the system. See Section 4.3.4, “mysqld_multi — Manage Multiple MySQL Servers”.
MySQL client programs that connect to the MySQL server:
-
The command-line tool for interactively entering SQL statements or executing them from a file in batch mode. See Section 4.5.1, “mysql — The MySQL Command-Line Tool”.
-
A client that performs administrative operations, such as creating or dropping databases, reloading the grant tables, flushing tables to disk, and reopening log files. mysqladmin can also be used to retrieve version, process, and status information from the server. See Section 4.5.2, “mysqladmin — Client for Administering a MySQL Server”.
[插播完毕]
To start or stop the server manually using the mysql.server script, invoke it with
start
or stop
arguments:
;以root权限运行
shell> mysql.server start
shell> mysql.server stop
Before mysql.server starts the server, it changes location to the MySQL installation directory, and then invokes mysqld_safe.
To start and stop MySQL automatically on your server, you need to add start and stop commands to the appropriate places in your /etc/rc*
files. 接着看下文,
If you install MySQL from a source distribution or using a binary distribution format that does not install mysql.server automatically, you can install it manually. The script can be found in the support-files
directory under the MySQL installation directory or in a MySQL source tree.
To install mysql.server manually, copy it to the/etc/init.d
directory with the name mysql, and then make it executable. Do this by changing location into the appropriate directory where mysql.server is located and executing these commands:
;以root权限运行
shell> cp ./support-files/mysql.server /etc/init.d/mysql
shell> chmod +x /etc/init.d/mysql
After installing the script, the commands needed to activate it to run at system startup depend on your operating system. On Linux, you can use chkconfig(Ubuntu对此不再提供支持,有一个替代的是sysv-rc-conf,见下文):
shell> chkconfig --add mysql ;不做此项,请接着看下面
For other systems, consult your operating system documentation to see how to install startup scripts.
sudo apt-get install sysv-rc-conf
cd /etc/init.d
sudo sysv-rc-conf atd on
Ubuntu下已经以sysv-rc-conf替代chkconfig了,执行sysv-rc-conf有图形和命令行两种格式,命令行格式如下:
# sysv-rc-conf --level ssh off
# sysv-rc-conf atd on
The first example will turn ssh off on levels 3 and 5. The second example turns atd on for run levels 2, 3, 4, and 5.
# sysv-rc-conf mysql on
# sysv-rc-conf --list mysql ;;可查看mysql的执行等级
在进行第七步之前,我们需要在 my.cnf 中设置一下 mysql.server,先来生成一个my.cnf,这里我们使用一个在线生成配置文件的网站,它为你方便地提供了优化了的 my.cnf:[https://tools.percona.com/]yhL#66 关于如何使用配置文件(如my.cnf),可以查看4.2.6 Using Option Files;而对配置文件中详细选项的了解,For a full description of MySQL Server command options, system variables, and status variables, see Section 5.1, “The MySQL Server”.
[插播,了解一下如何使用Option Files,即配置文件.cnf]4.2.6 Using Option Files
On Unix, Linux and Mac OS X, MySQL programs read startup options from the following files, in the specified order (top items are used first).
File Name | Purpose |
---|---|
/etc/my.cnf |
Global options |
/etc/mysql/my.cnf |
Global options |
|
Global options |
$MYSQL_HOME/my.cnf |
Server-specific options |
defaults-extra-file |
The file specified with --defaults-extra-file= , if any |
~/.my.cnf |
User-specific options |
~/.mylogin.cnf |
Login path options |
~
represents the current user's home directory (the value of $HOME
).
SYSCONFDIR
represents the directory specified with the SYSCONFDIR
option to CMake when MySQL was built. By default, this is the etc
directory located under the compiled-in installation directory.
MYSQL_HOME
is an environment variable containing the path to the directory in which the server-specific my.cnf
file resides. If MYSQL_HOME
is not set and you start the server using the mysqld_safe program, mysqld_safe attempts to set MYSQL_HOME
as follows:
Let
BASEDIR
andDATADIR
represent the path names of the MySQL base directory and data directory, respectively.If there is a
my.cnf
file inDATADIR
but not inBASEDIR
, mysqld_safe setsMYSQL_HOME
toDATADIR
.Otherwise, if
MYSQL_HOME
is not set and there is nomy.cnf
file inDATADIR
, mysqld_safe setsMYSQL_HOME
toBASEDIR
.
In MySQL 5.6, use of DATADIR
as the location for my.cnf
is deprecated.摒弃
Typically, DATADIR
is /usr/local/mysql/data
for a binary installation or /usr/local/var
for a source installation. Note that this is the data directory location that was specified at configuration time, not the one specified with the --datadir
option when mysqld starts. Use of --datadir
at runtime has no effect on where the server looks for option files, because it looks for them before processing any options.
MySQL looks for option files in the order just described and reads any that exist. If an option file that you want to use does not exist, create it with a plain text editor.
If multiple instances of a given option are found, the last instance takes precedence. There is one exception: For mysqld, the first instance of the --user
option is used as a security precaution, to prevent a user specified in an option file from being overridden on the command line.
On Unix platforms, MySQL ignores configuration files that are world-writable. This is intentional as a security measure.
Any long option that may be given on the command line when running a MySQL program can be given in an option file as well. To get the list of available options for a program, run it with the --help
option.
The syntax for specifying options in an option file is similar to command-line syntax (see Section 4.2.4, “Using Options on the Command Line”). However, in an option file, you omit the leading two dashes from the option name and you specify only one option per line. For example, --quick
and --host=localhost
on the command line should be specified as quick
and host=localhost
on separate lines in an option file. To specify an option of the form --loose-
in an option file, write it as opt_name
loose-
.opt_name
If an option group name is the same as a program name, options in the group apply specifically to that program. For example, the [mysqld]
and [mysql]
groups apply to the mysqld server and the mysql client program, respectively.
The [client]
option group is read by all client programs (but not by mysqld). This enables you to specify options that apply to all clients. For example, [client]
is the perfect group to use to specify the password that you use to connect to the server. (But make sure that the option file is readable and writable only by yourself, so that other people cannot find out your password.) Be sure not to put an option in the [client]
group unless it is recognized by all client programs that you use. Programs that do not understand the option quit after displaying an error message if you try to run them.
[插播完毕]
[插播,了解配置文件中那些单个选项的意义]see Section 5.1, “The MySQL Server”.自己看吧。
回到[https://tools.percona.com/]继续生成my.cnf,其中,data directory设置为/usr/local/mysql/data,我的my.cnf文件:
设置前,data文件夹里的文件(以备日后查看):
drwxr-xr-x mysql mysql Oct : ./
drwxr-xr-x root mysql Oct : ../
-rw-rw---- mysql mysql Oct : auto.cnf
-rw-rw---- mysql mysql Oct : ibdata1
-rw-rw---- mysql mysql Oct : ib_logfile0
-rw-rw---- mysql mysql Oct : ib_logfile1
drwx------ mysql mysql Oct : mysql/
-rw-r----- mysql mysql Oct : ovonel-usa.err
-rw-rw---- mysql mysql Oct : ovonel-usa.pid
drwx------ mysql mysql Oct : performance_schema/
drwxr-xr-x mysql mysql Oct : test/
最后网站生成的配置文件如下:
# Generated by Percona Configuration Wizard (http://tools.percona.com/) version REL5-20120208
# Configuration name server-dev-binary- generated for #**********@.com at -- :: [mysql] # CLIENT #
port =
socket = /usr/local/mysql/data/mysql.sock [mysqld] # GENERAL #
user = mysql
default-storage-engine = InnoDB
socket = /usr/local/mysql/data/mysql.sock
pid-file = /usr/local/mysql/data/mysql.pid # MyISAM #
key-buffer-size = 32M
myisam-recover = FORCE,BACKUP # SAFETY #
max-allowed-packet = 16M
max-connect-errors = # DATA STORAGE #
datadir = /usr/local/mysql/data/ # BINARY LOGGING #
log-bin = /usr/local/mysql/data/mysql-bin
expire-logs-days =
sync-binlog = # CACHES AND LIMITS #
tmp-table-size = 32M
max-heap-table-size = 32M
query-cache-type =
query-cache-size =
max-connections =
thread-cache-size =
open-files-limit =
table-definition-cache =
table-open-cache = # INNODB #
innodb-flush-method = O_DIRECT
innodb-log-files-in-group =
innodb-log-file-size = 64M
innodb-flush-log-at-trx-commit =
innodb-file-per-table =
innodb-buffer-pool-size = 128M # LOGGING #
log-error = /usr/local/mysql/data/mysql-error.log
log-queries-not-using-indexes =
slow-query-log =
slow-query-log-file = /usr/local/mysql/data/mysql-slow.log
在6中,If you installed MySQL using a source distribution, you may want to optionally copy one of the provided configuration files from the support-files
directory into your /etc
directory. There are different sample configuration files for different use cases, server types, and CPU and RAM configurations. If you want to use one of these standard files, you should copy it to /etc/my.cnf
, or /etc/mysql/my.cnf
and edit and check the configuration before starting your MySQL server for the first time.
这里我将生成的my.cnf复制到/etc/mysql目录下:
$ sudo mkdir /etc/msql
$ pwd
/etc/mysql
$ sudo vi my.cnf ;;然后将上面的配置文件内容复制粘贴
Section 2.10.1.2, “Starting and Stopping MySQL Automatically”. You can add options for mysql.server in a global /etc/my.cnf
file. A typical /etc/my.cnf
file might look like this: [这里我修改自己对应的/etc/mysql/my.cnf,添加到末尾]
[mysqld] # GENERAL #
user = mysql
default-storage-engine = InnoDB
socket = /usr/local/mysql/data/mysql.sock
pid-file = /usr/local/mysql/data/mysql.pid ;..........................
# LOGGING #
log-error = /usr/local/mysql/data/mysql-error.log
log-queries-not-using-indexes =
slow-query-log =
slow-query-log-file = /usr/local/mysql/data/mysql-slow.log [mysql.server] # BASE DIRECTORY #
basedir=/usr/local/mysql
The mysql.server script supports the following options: basedir
, datadir
, and pid-file
. If specified, they must be placed in an option file, not on the command line. mysql.server supports only start
and stop
as command-line arguments.
The following table shows which option groups the server and each startup script read from option files.
Table 2.15 MySQL Startup scripts and supported server option groups
Script | Option Groups |
---|---|
mysqld | [mysqld] , [server] , [mysqld- |
mysqld_safe | [mysqld] , [server] , [mysqld_safe] |
mysql.server | [mysqld] , [mysql.server] , [server] |
至此,Section 2.10.1.2, “Starting and Stopping MySQL Automatically”. 一节基本上就练习完了。返回2.10.1 Postinstallation Procedures for Unix-like Systems接着做第7步。
7. Start the MySQL Server:
shell> bin/mysqld_safe --user=mysql & ;;需要以root权限运行
To ensure this if you run mysqld_safe as root
, include the --user
option as shown.
8. Use mysqladmin to verify that the server is running. The following commands provide simple tests to check whether the server is up and responding to connections:
shell> bin/mysqladmin version
shell> bin/mysqladmin variables
运行结果:
$ ./bin/mysqladmin version
./bin/mysqladmin Ver 8.42 Distrib 5.6., for linux-glibc2. on x86_64
Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Server version 5.6.
Protocol version
Connection Localhost via UNIX socket
UNIX socket /tmp/mysql.sock
Uptime: hours min sec Threads: Questions: Slow queries: Opens: Flush tables: Open tables: Queries per second avg: 0.000
9. Verify that you can shut down the server:
$ ./bin/mysqladmin -u root shutdown
验证一下是否关闭了
$ ./bin/mysqladmin version
./bin/mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
$ ./bin/mysqladmin variables
./bin/mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
10. Verify that you can start the server again. Do this by using mysqld_safe or by invoking mysqld directly. For example:
$ ./bin/mysqld_safe --user=mysql & ;;需要以root权限运行
$ sudo netstat -tap | grep mysql
tcp6 [::]:mysql [::]:* LISTEN /mysqld
进行验证,
shell> bin/mysqladmin version
shell> bin/mysqladmin variables
执行之后出错:
./bin/mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' ()'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
解释的原因是不能通过'/tmp/mysql.sock'连接到localhost,执行ls /tmp -l果然没有mysql.sock文件存在。具体解释请看:B.5.2.2 Can't connect to [local] MySQL server。链接中给出的解释:
Someone has removed the Unix socket file that mysqld uses (/tmp/mysql.sock
by default). For example, you might have a cron job that removes old files from the /tmp
directory. You can always run mysqladmin version to check whether the Unix socket file that mysqladmin is trying to use really exists. The fix in this case is to change the cron job to not remove mysql.sock
or to place the socket file somewhere else. See Section B.5.4.5, “How to Protect or Change the MySQL Unix Socket File”. 结合这段分析,我的问题纠正就从这B.5.4.5来做:
The default location for the Unix socket file that the server uses for communication with local clients is /tmp/mysql.sock
.
On some versions of Unix, anyone can delete files in the /tmp
directory or other similar directories used for temporary files. If the socket file is located in such a directory on your system, this might cause problems.
Another approach is to change the place where the server creates the Unix socket file. If you do this, you should also let client programs know the new location of the file. You can specify the file location in several ways:
(1)Specify the path in a global or local option file. For example, put the following lines in /etc/my.cnf
,
我的是
/etc/mysql/my.cnf
:这样,我的my.cnf末尾就成下面这样了:
# Added by yh@-- #
[mysql.server] # BASE DIRECTORY #
basedir=/usr/local/mysql # GENERAL #
socket=/usr/local/mysql/data/mysql.sock # Added by yh@-- #
[client] socket=/usr/local/mysql/data/mysql.sock
之后测试成功:
$ sudo ./bin/mysqladmin version
接着做11步,
11. Run some simple tests to verify that you can retrieve information from the server. The output should be similar to what is shown here:
$ ./bin/mysqlshow
+--------------------+
| Databases |
+--------------------+
| information_schema |
| test |
+--------------------+
以下不做,直接跳过到13步:
13. At this point, you should have the server running. However, none of the initial MySQL accounts have a password, and the server permits permissive access to test databases. To tighten security, follow the instructions in Section 2.10.2, “Securing the Initial MySQL Accounts”.待做完安全设置,再返回2.10.1完成本节剩下的“时区”和“环境变量”设置(点击查看),先跨过下面两段。
The MySQL 5.6 installation procedure creates time zone tables in the mysql
database but does not populate them. To do so, use the instructions in Section 10.6, “MySQL Server Time Zone Support”.
To make it more convenient to invoke programs installed in the bin
directory under the installation directory, you can add that directory to your PATH
environment variable setting. That enables you to run a program by typing only its name, not its entire path name. See Section 4.2.10, “Setting Environment Variables”.
------------>2.10.2 Securing the Initial MySQL Accounts
The mysql.user
grant table defines the initial MySQL user accounts and their access privileges:
root用户:Some accounts have the user name root
. These are superuser accounts that have all privileges and can do anything. The initial root
account passwords are empty, so anyone can connect to the MySQL server as root
without a password and be granted all privileges.
On Unix, each root
account permits connections from the local host. Connections can be made by specifying the host name localhost
, the IP address 127.0.0.1
, the IPv6 address ::1
, or the actual host name or IP address. An attempt to connect to the host 127.0.0.1
normally resolves to the localhost
account. However, this fails if the server is run with the --skip-name-resolve
option, so the 127.0.0.1
account is useful in that case. The ::1
account is used for IPv6 connections.
匿名用户:Some accounts are for anonymous users. These have an empty user name. The anonymous accounts have no password, so anyone can use them to connect to the MySQL server.
On Unix, each anonymous account permits connections from the local host. Connections can be made by specifying a host name of localhost
for one of the accounts, or the actual host name or IP address for the other.
To display which accounts exist in the mysql.user
table and check whether their passwords are empty, use the following statement:
;;启动mysqld先
$ sudo netstat -tap | grep mysql ;;查看是否有mysqld已经存在
$ sudo ./bin/mysqld_safe --user=mysql &
$ sudo ./bin/mysqladmin version ;;验证一下启动正常不
$ sudo ./bin/mysql ;;root权限运行才可
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.6.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SELECT User, Host, Password FROM mysql.user;
+------+--------------------+----------+
| User | Host | Password |
+------+--------------------+----------+
| root | localhost | |
| root | myhost.example.com | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | myhost.example.com | |
+------+--------------------+----------+
your MySQL installation is unprotected until you do something about it:
You should assign a password to each MySQL
root
account.If you want to prevent clients from connecting as anonymous users without a password, you should either assign a password to each anonymous account or else remove the accounts.
root
password after setting it, see Section B.5.4.1, “How to Reset the Root Password”.Assigning root Account Passwords
The root
account passwords can be set several ways. The following discussion demonstrates three methods:
Use the
SET PASSWORD
statementUse the
UPDATE
statementUse the mysqladmin command-line client program
For Unix, do this:
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpwd');
mysql> UPDATE mysql.user SET Password = PASSWORD('newpwd')
-> WHERE User = 'root';
mysql> FLUSH PRIVILEGES;
The FLUSH
statement causes the server to reread the grant tables. Without it, the password change remains unnoticed by the server until you restart it.
详细方法请移步:2.10.2 Securing the Initial MySQL Accounts。
Assigning Anonymous Account Passwords
The mysql commands in the following instructions include a -p
option based on the assumption that you have set the root
account passwords using the preceding instructions and must specify that password when connecting to the server.否则会报错:[与此类似,以后执行mysql, mysqladmin, mysqld_safe 等程序都需带参数-p]
$ sudo ./bin/mysql
[sudo] password for xxxx:
ERROR (): Access denied for user 'root'@'localhost' (using password: NO)
To assign passwords to the anonymous accounts, connect to the server as root
, then use either SET PASSWORD
or UPDATE
. Be sure to encrypt the password using the PASSWORD()
function.
To use SET PASSWORD
on Unix, do this:
/usr/local/mysql$ sudo ./bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.6.-log MySQL Community Server (GPL) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> UPDATE mysql.user SET Password = PASSWORD('newpwd')
-> WHERE User = '';
Query OK, 2 rows affected (0.06 sec)
Rows matched: 2 Changed: 2 Warnings: 0 mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.06 sec)
详细方法请移步:2.10.2 Securing the Initial MySQL Accounts。
Securing Test Databases
By default, the mysql.db
table contains rows that permit access by any user to the test
database and other databases with names that start with test_
. (These rows have an empty User
column value, which for access-checking purposes matches any user name.) This means that such databases can be used even by accounts that otherwise possess no privileges. If you want to remove any-user access to test databases, do so as follows:
sudo ./bin/mysql -u root -p
Enter password:(enter root password here)
mysql> DELETE FROM mysql.db WHERE Db LIKE 'test%';
Query OK, 2 rows affected (0.04 sec) mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.06 sec)
With the preceding change, only users who have global database privileges or privileges granted explicitly for the test
database can use it.2.10.2 Securing the Initial MySQL Accounts文末说的删除test数据库,我这里不做。
.mysql_history
and is created in your home directory. Passwords can be written as plain text in SQL statements such as CREATE USER
, GRANT
, and SET PASSWORD
, so if you use these statements, they are logged in the history file. To keep this file safe, use a restrictive access mode, the same way as described earlier for the .my.cnf
file.
sudo chmod ~/.mysql_history
The MySQL 5.6 installation procedure creates time zone tables in the mysql
database but does not populate them. To do so, use the instructions in Section 10.6, “MySQL Server Time Zone Support”.[这留到以后要用到的时候再做]
[环境变量设置]
To make it more convenient to invoke programs installed in the bin
directory under the installation directory, you can add that directory to your PATH
environment variable setting. That enables you to run a program by typing only its name, not its entire path name. See Section 4.2.10, “Setting Environment Variables”. [[Section 2.12, “Environment Variables”说,In many cases, it is preferable to use an option file instead of environment variables to modify the behavior of MySQL. See Section 4.2.6, “Using Option Files”. ]]
The commands to set environment variables can be executed at your command prompt to take effect immediately, but the settings persist only until you log out. To have the settings take effect each time you log in, use the interface provided by your system or place the appropriate command or commands in a startup file that your command interpreter reads each time it starts.
On Unix, typical shell startup files are .bashrc
or .bash_profile
for bash, or .tcshrc
for tcsh.
$ echo $SHELL
/bin/bash ;;显示我的shell是bash
Suppose that your MySQL programs are installed in /usr/local/mysql/bin
and that you want to make it easy to invoke these programs. To do this, set the value of the PATH
environment variable to include that directory. For example, if your shell is bash, add the following line to your .bashrc
file:
$ vi ~/.bashrc
在文末添加下行:
PATH=${PATH}:/usr/local/mysql/bin
bash uses different startup files for login and nonlogin shells, so you might want to add the setting to .bashrc
for login shells and to .bash_profile
for nonlogin shells to make sure that PATH
is set regardless.
If the appropriate startup file does not exist in your home directory, create it with a text editor.
更改完成之后,可以在命令行中直接调用mysql程序了,但是一旦加上sudo运行,则提示出错,明明PATH目录下存在该文件的:
$ sudo mysql
sudo: mysql:找不到命令
造成该错误的原因是Linux的限制了sudo执行的范围,这出于安全方面的考虑,怎么做改变呢,参见[http://www.cnblogs.com/yhLinux/articles/4019311.html],
$ which visudo
/usr/sbin/visudo
ovonel@ovonel-usa:~$ sudo visudo
修改之前,visudo是下面这样的:
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" # Host alias specification # User alias specification 修改secure_path,最后是这样的:
# Added by xx@--
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/mysql/bin"
附:
1. 设置数据库编码(sudo vi /etc/mysql/my.cnf):
[client]
default-character-set = utf8 [mysqld]
character-set-server = utf8
collation-server = utf8_general_ci
详细内容见此链接:MySQL设置字符集CHARACTER SET
2. 贴出我的配置文件(PS: vim跨文件操作之全文复制命令"+yG,从当前行复制到文末):
# Generated by Percona Configuration Wizard (http://tools.percona.com/) version REL5-20120208
# Configuration name server-dev-binary- generated for #**********@.com at -- :: [mysql] # CLIENT #
port =
socket = /usr/local/mysql/data/mysql.sock [mysqld] # GENERAL #
user = mysql
default-storage-engine = InnoDB
socket = /usr/local/mysql/data/mysql.sock
pid-file = /usr/local/mysql/data/mysql.pid # MyISAM #
key-buffer-size = 32M
myisam-recover = FORCE,BACKUP # SAFETY #
max-allowed-packet = 16M
max-connect-errors = # DATA STORAGE #
datadir = /usr/local/mysql/data/ # BINARY LOGGING #
log-bin = /usr/local/mysql/data/mysql-bin
expire-logs-days =
sync-binlog = # CACHES AND LIMITS #
tmp-table-size = 32M
max-heap-table-size = 32M
query-cache-type =
query-cache-size =
max-connections =
thread-cache-size =
open-files-limit =
table-definition-cache =
table-open-cache = # INNODB #
innodb-flush-method = O_DIRECT
innodb-log-files-in-group =
innodb-log-file-size = 64M
innodb-flush-log-at-trx-commit =
innodb-file-per-table =
innodb-buffer-pool-size = 128M # LOGGING #
log-error = /usr/local/mysql/data/mysql-error.log
log-queries-not-using-indexes =
slow-query-log =
slow-query-log-file = /usr/local/mysql/data/mysql-slow.log # Added by yh @-- #
# SET CODING #
character-set-server = utf8
collation-server = utf8_general_ci # Added by yh@-- #
[mysql.server] # BASE DIRECTORY #
basedir=/usr/local/mysql # GENERAL #
socket=/usr/local/mysql/data/mysql.sock # Added by yh@-- #
[client] socket=/usr/local/mysql/data/mysql.sock # Added by yh@-- #
default-character-set = utf8
(完)
相关链接:
Ubuntu 12.04 安装MySQL的更多相关文章
- Ubuntu 18.04 安装MySQL
最近在写东西的时候,需要用到MySQL,在网上查了一下,都说Ubuntu18.04不能安装MySQL5.7.22, 总觉的不可能,所以自己就研究了一下,然后分享给大家 工具/原料 VMware W ...
- ubuntu 12.04 安装Docker 实战
2016-3-8 从网络服务商那里申请到一台Ubuntu测试服务器,用来测试安装Docker环境. 注:本人初学Docker,对Linux命令也仅是稍稍了解,如有错误,烦请告知. 查看系统相关信息 可 ...
- Ubuntu 16.04 安装Mysql 5.7 踩坑小记
title:Ubuntu 16.04 安装Mysql 5.7 踩坑小记 date: 2018.02.03 安装mysql sudo apt-get install mysql-server mysql ...
- 在Ubuntu 18.04 安装 MySQL 8.0
在Ubuntu 18.04 安装 MySQL 8.0 ① 登入 mysql 官网,在官网中下载 deb 包,点击该链接,即可下载. https://dev.mysql.com/downloads/re ...
- Ubuntu 12.04.1 mysql从5.5升级到5.6
Ubuntu 12.04.1 mysql从5.5升级到5.6 1 2 3 4 5 apt-cache search mysql-server sudo apt-add-repository ppa: ...
- Ubuntu 12.04 安装 Apache2+PHP5+MySQL
LAMP是Linux web服务器组合套装的缩写,分别是Apache+MySQL+PHP.此教程教大家如何在Ubuntu 12.04 LTS server 上安装Apache2服务器,包括PHP5(m ...
- ubuntu 12.04 安装 nginx+php+mysql web服务器
Nginx 是一个轻量级,以占用系统资源少,运行效率而成为web服务器的后起之秀,国内现在很多大型网站都以使用nginx,包括腾讯.新浪等大型信息网站,还有淘宝网站使用的是nginx二次开发的web服 ...
- [转] ubuntu 12.04 安装 nginx+php+mysql web服务器
Nginx 是一个轻量级,以占用系统资源少,运行效率而成为web服务器的后起之秀,国内现在很多大型网站都以使用nginx,包括腾讯.新浪等大型信息网站,还有淘宝网站使用的是nginx二次开发的web服 ...
- Ubuntu 12.04安装Adobe Reader
原本从Adobe 官方网站下载了 Adobe Reader, 是个rpm包,先用agt-get 装了rpm(sudo apt-get install rpm), 一安装(rpm -ivh AdobeR ...
随机推荐
- 2.struts2访问web资源(在struts2中获取session,request等等)
什么是web资源:web资源就是指request,response,session,servlet的api 为什么需要访问web资源:因为图片上传,需要获取图片的目录,就需要通过action来访问we ...
- 邮件中继成畅邮标配 U-Mail为何领先
中国力推“一带一路”战略,为首成立“亚投行”,越来越多的中国企业酝酿走出去.在企业间的商务往来中,电子邮件将扮演着重要角色,但是因为要跨洋 过海,各国互联网环境和法律法规有所区别,面临着种种变数,对方 ...
- Android菜鸟成长记10 -- ListVew
ListView在我们学习Android的过程中是非常重要得一个部分. listview主要有两个职责 1)将数据填充到布局. 2)处理用户的选择点击等操作. 一个ListView的创建需要3个元素 ...
- 转载:centos7下使用yum安装mysql
转自:http://www.cnblogs.com/hwd-cnblogs/p/5213337.html CentOS7的yum源中默认好像是没有mysql的.为了解决这个问题,我们要先下载mysql ...
- Java实现文件压缩与解压
Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹进行压缩和解压,对于一些初学者来说是个很不错的实例.(转载自http://www.puiedu. ...
- array_filter,匿名函数
static function get_categoryII() { return array( array('id' => 1, 'c1id' => 1, 'name' => '1 ...
- 九宝老师微信小程序开发的过程
- 第三方登录 QQ 错误码100044(提示 该应用非官方正版应用)
当你碰到这个问题的时候,不要着急,你的功能已经走通了,代码没有问题. 100044 原因: 1.首先确定你的包名和签名,跟申请第三方平台的是一个.(真心吐槽一下,接盘侠不好当,尤其是没有交接的) 2. ...
- videoconverter转换
以前录制的avi用vfw可以解码的,但是现在变成win7系统了,无法解码了.只好用视频转换软件把avi转成无压缩的. 选择losses uncompressed avi,点进去选UYVY就行了.
- POJ 2528 Mayor's posters (线段树)
题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...