您的位置:首页 > 运维架构 > Linux

Linux下MySQL安装与配置

2017-04-13 00:00 288 查看
1、下载安装包
http://dev.mysql.com/downloads/mysql/#downloads
推荐下载通用安装方法的TAR包
http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.12-linux-glibc2.5-x86_64.tar

2、检查库文件是否存在,如有删除。

[root@localhost Desktop]$ rpm -qa | grep mysql
mysql-libs-5.1.52-1.el6_0.1.x86_64
[root@localhost ~]# rpm -e mysql-libs-5.1.52.x86_64 --nodeps
[root@localhost ~]#

3、检查mysql组和用户是否存在,如无创建

[root@localhost ~]# cat /etc/group | grep mysql
mysql:x:490:
[root@localhost ~]# cat /etc/passwd | grep mysql
mysql:x:496:490::/home/mysql:/bin/bash

默认存在的情况,如无,执行添加命令:

[root@localhost ~]#groupadd mysql
[root@localhost ~]#useradd -r -g mysql mysql

useradd -r参数表示mysql用户是系统用户,不可用于登录系统。

4、解压TAR包,更改所属的组和用户

[root@localhost ~]# cd /usr/local/
[root@localhost local]# tar xvf mysql-5.7.12-linux-glibc2.5-x86_64.tar
[root@localhost local]# ls -l
total 1306432
-rwxr--r--. 1 root root  668866560 Jun  1 15:07 mysql-5.7.12-linux-glibc2.5-x86_64.tar
-rw-r--r--. 1 7161 wheel 638960236 Mar 28 12:54 mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz
-rw-r--r--. 1 7161 wheel  29903372 Mar 28 12:48 mysql-test-5.7.12-linux-glibc2.5-x86_64.tar.gz
[root@localhost local]# tar xvfz mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz
[root@localhost local]# mv mysql-5.7.12-linux-glibc2.5-x86_64 mysql
[root@localhost local]# ls -l
total 1306436
drwxr-xr-x. 2 root root       4096 Dec  4  2009 bin
drwxr-xr-x. 2 root root       4096 Dec  4  2009 etc
drwxr-xr-x. 2 root root       4096 Dec  4  2009 games
drwxr-xr-x. 2 root root       4096 Dec  4  2009 include
drwxr-xr-x. 2 root root       4096 Dec  4  2009 lib
drwxr-xr-x. 3 root root       4096 Dec  2 14:36 lib64
drwxr-xr-x. 2 root root       4096 Dec  4  2009 libexec
drwxr-xr-x. 9 7161 wheel      4096 Mar 28 12:51 mysql
-rw-r--r--. 1 7161 wheel 638960236 Mar 28 12:54 mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz
drwxr-xr-x. 2 root root       4096 Dec  4  2009 sbin
drwxr-xr-x. 6 root root       4096 Dec  2 14:36 share
drwxr-xr-x. 2 root root       4096 Dec  4  2009 src
[root@localhost local]# chown -R mysql:root mysql/
[root@localhost local]# cd mysql/

5、安装和初始化数据库

[root@localhost mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2016-06-01 15:23:25 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2016-06-01 15:23:30 [WARNING] The bootstrap log isn't empty:
2016-06-01 15:23:30 [WARNING] 2016-06-01T22:23:25.491840Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2016-06-01T22:23:25.492256Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2016-06-01T22:23:25.492260Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
----------------------

这一步我安装的时候,第一次不行,所有的重新来了一次,然后出现的是如下提示:

[root@localhost mysql]# ./bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
Installing MySQL system tables...OK

Filling help tables...OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/local/mysql//bin/mysqladmin -u root password 'new-password'
/usr/local/mysql//bin/mysqladmin -u root -h 127.0.1.1 password 'new-password'

Alternatively you can run:

/usr/local/mysql//bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

cd /usr ; /usr/local/mysql//bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/ 
The latest information about MySQL is available on the web at
 http://www.mysql.com 
Support MySQL by buying support/licenses at http://shop.mysql.com 
New default config file was created as /usr/local/mysql//my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

[root@localhost mysql]#

6、创建mysqld起动和配置文件

[root@localhost mysql]#
[root@localhost mysql]# cp -a ./support-files/my-default.cnf /etc/my.cnf
[root@localhost mysql]# cp -a ./support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# cd bin/
[root@localhost bin]# ./mysqld_safe --user=mysql &
[1] 2932
[root@localhost bin]# 2016-06-01T22:27:09.708557Z mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
2016-06-01T22:27:09.854913Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

[root@localhost bin]# /etc/init.d/mysqld restart
Shutting down MySQL..2016-06-01T22:27:50.498694Z mysqld_safe mysqld from pid file /usr/local/mysql/data/localhost.localdomain.pid ended
SUCCESS!
Starting MySQL. SUCCESS!
[1]+  Done                    ./mysqld_safe --user=mysql
[root@localhost bin]#
//设置开机启动
[root@localhost bin]# chkconfig --level 35 mysqld on
[root@localhost bin]#

另一种很好的设置MySQL自启动的方式:

[root@localhost bin]# echo "service mysqld start" >> /etc/rc.local

或者进入/etc/目录,直接vim rc.local编辑rc.local文件,在最后一行添加“service mysqld start”,保存退出

有时会遇到权限问题:
bash: /etc/rc.local: Permission denied
分析:
bash 返回 /etc/rc.local: Permission denied
这是因为重定向符号 “>” 也是 bash 的命令。sudo 只是让 echo 命令具有了 root 权限,
但是没有让 “>” 命令也具有root 权限,所以 bash 会认为这个命令没有写入信息的权限。

解决:
使用 bash -c 参数
[root@localhost bin]# sudo bash -c "echo "service mysqld start" >> /etc/rc.local"

6.初始化密码
mysql5.7会生成一个初始化密码,而在之前的版本首次登陆不需要登录。

[root@localhost bin]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2016-06-01 15:23:25
,xxxxxR5H9
[root@localhost bin]# ./mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.12

Copyright (c) 2000, 2016, 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> SET PASSWORD = PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

上面一步中,如果出现了提示密码过期,可用如下方法解决:

[root@localhost bin]# /usr/local/mysql/bin/mysqladmin -u root -p password
Enter password:
New password:
Confirm new password:
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
或者:
/usr/local/mysql/bin/mysqladmin -u root -p'<your temp password>' password '<your new password>'

7.添加远程访问权限

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select host, user from user; +-----------+-----------+
| host | user | +-----------+-----------+
| %         | root      |
| localhost | mysql.sys | +-----------+-----------+

9、更改配置文件和服务的权限
此步一定要改,我当初没改就一直提示说是有一个服务绑定了3306,死都找不出来问题。
先添加环境变量:

[root@localhost bin]# vim /etc/profile
最后一行填加:
MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
让修改立即生效:
[root@localhost bin]# source /etc/profile

再修改两个文件的权限:

[root@localhost bin]#
[root@localhost bin]# service mysqld stop
[root@localhost bin]# chown -R root:root /etc/init.d/mysqld
[root@localhost bin]# chown -R root:root /etc/my.cnf

10、修改配置文件

[root@localhost bin]# vim /etc/my.cnf
我的配置文件如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [client] default-character-set=utf8 #避免MySQL的外部锁定,减少出错几率增强稳定性。 socket = /tmp/mysql.sock [mysql] local-infile=1 loose-local-infile=1 [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 server_id = 1 character_set_server=utf8 #skip-grant-tables lower_case_table_names=1 #避免MySQL的外部锁定,减少出错几率增强稳定性。 socket = /tmp/mysql.sock skip-external-locking skip-name-resolve log_bin=/usr/local/mysql/log/bin.log log_error=/usr/local/mysql/log/error.log long_query_time=3 slow_query_log=ON slow_query_log_file="/usr/local/mysql/log/slowquery.log" general_log=ON general_log_file=/usr/local/mysql/log/general.log expire_logs_days = 10 # socket = ..... # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_IN_DATE

修改后保存退出

11、重启生效

[root@localhost bin]# /etc/init.d/mysqld restart
[root@localhost bin]# netstat -na | grep 3306,如果看到有监听说明服务启动了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: