mysql的安装
1. 关闭iptables
service iptables stop
chkconfig iptables off
2. 禁止selinux
方法:
/etc/sysconfig/selinux
SELINUX=DISABLED
用root登录操作系统,增加mysql用户和组,数据库将安装在此用户下:
groupadd mysql
useradd -g mysql -s /sbin/nologin -d /opt/mysql mysql //useradd -g mysql mysql
注: -d是修改用户家目录
uid=500(mysql) gid=500(mysql) groups=500(mysql)
解压mysql二进制安装包,安装文件放在/opt/mysql,并对解压后的mysql目录加一个符号连接,mysql,这样读mysql目录操作会更方便
cd /opt/mysql/
tar zxvf /root/Desktop/mysql-5.5.37-linux2.6-x86_64.tar.gz
[root@robert Desktop]# cd /opt/mysql/
[root@robert mysql]# ls
mysql-5.5.37-linux2.6-x86_64
[root@robert mysql]# cd mysql-5.5.37-linux2.6-x86_64/
[root@robert mysql-5.5.37-linux2.6-x86_64]# ls
bin COPYING data docs include INSTALL-BINARY lib man mysql-test README scripts share sql-bench support-files
创建软连接
---/usr/local/mysql -> 刚才解的文件
[root@robert local]# cd /usr/local/
[root@robert local]# ln -s /opt/mysql/mysql-5.5.39-linux2.6-x86_64 mysql
[root@robert local]# ls -l mysql
lrwxrwxrwx 1 root root 40 Nov 24 09:05 mysql -> /opt/mysql/mysql-5.5.37-linux2.6-x86_64/
创建mysql安装需要的目录
mkdir -p /data/mysql/mysql_3306/{data, logs,tmp}
cd /data
mkdir mysql
[root@node20 mysql]# mkdir mysql_3306
[root@node20 mysql]# cd mysql_3306/
[root@node20 mysql_3306]# mkdir data
[root@node20 mysql_3306]# mkdir logs
[root@node20 mysql_3306]# mkdir tmp
设置目录权限,讲data目录owner改为mysql,其他目录和文件为root
chown -R mysql:mysql /data/mysql/
chown -R mysql:mysql /usr/local/mysql/
加环境变量,解决找不到mysql命令的问题
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
source /etc/profile
依赖的lib库是否安全了
# ldd /usr/local/mysql/bin/mysqld
[root@robert mysql_3306]# ldd /usr/local/mysql/bin/mysqld
libpthread.so.0 => /lib64/libpthread.so.0 (0x000000303aa00000)
libaio.so.1 => /usr/lib64/libaio.so.1 (0x00002b4ef2124000)
libm.so.6 => /lib64/libm.so.6 (0x000000303a200000)
librt.so.1 => /lib64/librt.so.1 (0x000000303f600000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x000000304ce00000)
libdl.so.2 => /lib64/libdl.so.2 (0x000000303a600000)
libc.so.6 => /lib64/libc.so.6 (0x0000003039e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003039a00000)
创建修改mysql配置文件
cd /usr/local/mysql/support-files
# cp my-large.cnf /etc/my.cnf
修改my.cnf文件:
datadir =/data/mysql/mysql_3306/data
socket = /tmp/mysql.sock
: %s/home\/mysql/data\/mysql\/mysql_3306\/data/g
innodb_buffer_pool_size = 1G -> innodb_buffer_pool_size = 100M
innodb_log_files_in_group = 2 -> innodb_log_files_in_group = 3
保存退出。
cd /usr/local/mysql/
初始化数据库一定要在basedir 下进行
./scripts/mysql_install_db --user=mysql --defautls-file=/etc/my.cnf --datadir=/data/mysql/mysql_3306/data
启动mysql服务
[root@robert ~]# cd /usr/local/mysql/
[root@robert mysql]# cd bin/
[root@robert bin]# ./mysqld_safe --user=mysql & //mysql_safe是myslq的守护进程,自动启动mysqld。 他启动,mysqld也会启动
[root@robert /]# ps -ef|grep mysqld
root 22874 3491 0 10:44 pts/2 00:00:00 /bin/sh ./mysqld_safe -user=mysql
mysql 23119 22874 0 10:44 pts/2 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/mysql_3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql -user=mysql --log-error=/data/mysql/mysql_3306/data/robert.localdomain.err --pid-file=/data/mysql/mysql_3306/data/robert.localdomain.pid --socket=/tmp/mysql.sock --port=3306
root 23262 21384 0 10:49 pts/4 00:00:00 grep mysqld
看到这两个进程说明mysql已经启动成功了
启动之后也会产生一个套接文件
[root@robert /]# ls /tmp/mysql.sock
/tmp/mysql.sock
在/tmp下,如果有了这个文件,也说明mysql已经启动了,没有这个文件,mysql是不会启动的
[root@robert bin]# pwd
/usr/local/mysql/bin
连接mysql
1)连接到本机上的mysql
到mysql安装目录,一般可以直接输入命令./mysql -uroot -p,回车提示输入密码。 如果刚安装好mysql,超级用户root是没有密码的,所以./mysql直接回车,即可进入mysql中
[root@robert bin]# pwd
/usr/local/mysql/bin
[root@robert bin] ./mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.34 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit 退出
2)连接上远程主机上的mysql
远程主机ip:10.0.0.1 ,用户名root,密码123456,键入的命令为:
./mysql -h 10.0.0.1 -uroot -p123456
修改mysqlroot密码
[root@robert bin]# ./mysqladmin -u root password 123456 //给管理员用户root设置密码
[root@robert bin]# ./mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql的启动和关闭
1.命令行方式
1)启动服务
[root@robert bin]# ./mysqld_safe & //mysql_safe是myslq的守护进程,自动启动mysqld。 他启动,mysqld也会启动
[1] 26193
[root@robert bin]# 141124 22:07:43 mysqld_safe Logging to '/data/mysql/mysql_3306/data/robert.localdomain.err'.
141124 22:07:44 mysqld_safe Starting mysqld daemon with databases from /data/mysql/mysql_3306/data
[root@robert bin]# ps -ef|grep mysqld
root 26193 4762 0 22:07 pts/1 00:00:00 /bin/sh ./mysqld_safe
mysql 26421 26193 0 22:07 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/mysql_3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql_3306/data/robert.localdomain.err --pid-file=/data/mysql/mysql_3306/data/robert.localdomain.pid --socket=/tmp/mysql.sock --port=3306
root 26487 3491 0 22:11 pts/2 00:00:00 grep mysqld
看到这两个进程说明mysql已经启动成功了
启动之后也会产生一个套接文件
[root@robert /]# ls /tmp/mysql.sock
/tmp/mysql.sock
在/tmp下,如果有了这个文件,也说明mysql已经启动了,没有这个文件,mysql是不会启动的
[root@robert bin]# ./mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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>
[root@robert bin]# ./mysql -uroot -p123456
mysql>
2)关闭服务
[root@robert bin]# pwd
/usr/local/mysql/bin
[root@robert bin]# ./mysqladmin -uroot shutdown
./mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'
[root@robert bin]# ./mysqladmin -uroot shutdown -p //直接[root@robert bin]# ./mysqladmin -uroot -p123456 shutdown
Enter password:
[root@robert bin]# ./mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
2服务的方式
如果mysql是用rpm包安装的,则启动和关闭mysql服务的过程如下:
#service mysql start
#service mysql restart
#service mysql stop
注:在命令上启动mysql的时候,如果不加 “--console”,启动和关闭信息将不会再界面中显示,而是记录在安装目录下的data目录里面,文件名一般是hostname.err,可以通过此文件查看mysql控制台信息。
如何指定/etc/init.d/mysqld启动或关闭mysql
# find / -name mysql.server
/opt/mysql/mysql-5.5.37-linux2.6-x86_64/support-files/mysql.server
If you could find this file,copy it to /etc/rc.d/init.d and rename it as mysqld,then you could using " /etc/rc.d/init.d/mysqld start " to start your mysql instance.
cd /opt/mysql/mysql-5.5.37-linux2.6-x86_64/support-files/
#cp mysql.server /etc/rc.d/init.d
[root@robert bin]# /etc/init.d/mysqld stop
Shutting down MySQL. [ OK ]
[root@robert bin]# /etc/init.d/mysqld start
Starting MySQL.. [ OK ]
1. 关闭iptables
service iptables stop
chkconfig iptables off
2. 禁止selinux
方法:
/etc/sysconfig/selinux
SELINUX=DISABLED
用root登录操作系统,增加mysql用户和组,数据库将安装在此用户下:
groupadd mysql
useradd -g mysql -s /sbin/nologin -d /opt/mysql mysql //useradd -g mysql mysql
注: -d是修改用户家目录
-s /sbin/nologin 当用户配置成/sbin/nologin时,如果再使用该用户ssh到linux操作系统,会提示 This account is currently not available.
[root@robert ~]# su - mysql
This account is currently not available.
uid=500(mysql) gid=500(mysql) groups=500(mysql)
解压mysql二进制安装包,安装文件放在/opt/mysql,并对解压后的mysql目录加一个符号连接,mysql,这样读mysql目录操作会更方便
cd /opt/mysql/
tar zxvf /root/Desktop/mysql-5.5.37-linux2.6-x86_64.tar.gz
[root@robert Desktop]# cd /opt/mysql/
[root@robert mysql]# ls
mysql-5.5.37-linux2.6-x86_64
[root@robert mysql]# cd mysql-5.5.37-linux2.6-x86_64/
[root@robert mysql-5.5.37-linux2.6-x86_64]# ls
bin COPYING data docs include INSTALL-BINARY lib man mysql-test README scripts share sql-bench support-files
创建软连接
---/usr/local/mysql -> 刚才解的文件
[root@robert local]# cd /usr/local/
[root@robert local]# ln -s /opt/mysql/mysql-5.5.39-linux2.6-x86_64 mysql
[root@robert local]# ls -l mysql
lrwxrwxrwx 1 root root 40 Nov 24 09:05 mysql -> /opt/mysql/mysql-5.5.37-linux2.6-x86_64/
创建mysql安装需要的目录
mkdir -p /data/mysql/mysql_3306/{data, logs,tmp}
cd /data
mkdir mysql
[root@node20 mysql]# mkdir mysql_3306
[root@node20 mysql]# cd mysql_3306/
[root@node20 mysql_3306]# mkdir data
[root@node20 mysql_3306]# mkdir logs
[root@node20 mysql_3306]# mkdir tmp
设置目录权限,讲data目录owner改为mysql,其他目录和文件为root
chown -R mysql:mysql /data/mysql/
chown -R mysql:mysql /usr/local/mysql/
加环境变量,解决找不到mysql命令的问题
echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
source /etc/profile
依赖的lib库是否安全了
# ldd /usr/local/mysql/bin/mysqld
[root@robert mysql_3306]# ldd /usr/local/mysql/bin/mysqld
libpthread.so.0 => /lib64/libpthread.so.0 (0x000000303aa00000)
libaio.so.1 => /usr/lib64/libaio.so.1 (0x00002b4ef2124000)
libm.so.6 => /lib64/libm.so.6 (0x000000303a200000)
librt.so.1 => /lib64/librt.so.1 (0x000000303f600000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x000000304ce00000)
libdl.so.2 => /lib64/libdl.so.2 (0x000000303a600000)
libc.so.6 => /lib64/libc.so.6 (0x0000003039e00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003039a00000)
创建修改mysql配置文件
cd /usr/local/mysql/support-files
# cp my-large.cnf /etc/my.cnf
修改my.cnf文件:
datadir =/data/mysql/mysql_3306/data
socket = /tmp/mysql.sock
: %s/home\/mysql/data\/mysql\/mysql_3306\/data/g
innodb_buffer_pool_size = 1G -> innodb_buffer_pool_size = 100M
innodb_log_files_in_group = 2 -> innodb_log_files_in_group = 3
保存退出。
cd /usr/local/mysql/
初始化数据库一定要在basedir 下进行
./scripts/mysql_install_db --user=mysql --defautls-file=/etc/my.cnf --datadir=/data/mysql/mysql_3306/data
启动mysql服务
[root@robert ~]# cd /usr/local/mysql/
[root@robert mysql]# cd bin/
[root@robert bin]# ./mysqld_safe --user=mysql & //mysql_safe是myslq的守护进程,自动启动mysqld。 他启动,mysqld也会启动
[root@robert /]# ps -ef|grep mysqld
root 22874 3491 0 10:44 pts/2 00:00:00 /bin/sh ./mysqld_safe -user=mysql
mysql 23119 22874 0 10:44 pts/2 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/mysql_3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql -user=mysql --log-error=/data/mysql/mysql_3306/data/robert.localdomain.err --pid-file=/data/mysql/mysql_3306/data/robert.localdomain.pid --socket=/tmp/mysql.sock --port=3306
root 23262 21384 0 10:49 pts/4 00:00:00 grep mysqld
看到这两个进程说明mysql已经启动成功了
启动之后也会产生一个套接文件
[root@robert /]# ls /tmp/mysql.sock
/tmp/mysql.sock
在/tmp下,如果有了这个文件,也说明mysql已经启动了,没有这个文件,mysql是不会启动的
[root@robert bin]# pwd
/usr/local/mysql/bin
连接mysql
1)连接到本机上的mysql
到mysql安装目录,一般可以直接输入命令./mysql -uroot -p,回车提示输入密码。 如果刚安装好mysql,超级用户root是没有密码的,所以./mysql直接回车,即可进入mysql中
[root@robert bin]# pwd
/usr/local/mysql/bin
[root@robert bin] ./mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.34 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit 退出
2)连接上远程主机上的mysql
远程主机ip:10.0.0.1 ,用户名root,密码123456,键入的命令为:
./mysql -h 10.0.0.1 -uroot -p123456
修改mysqlroot密码
[root@robert bin]# ./mysqladmin -u root password 123456 //给管理员用户root设置密码
[root@robert bin]# ./mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql的启动和关闭
1.命令行方式
1)启动服务
[root@robert bin]# ./mysqld_safe & //mysql_safe是myslq的守护进程,自动启动mysqld。 他启动,mysqld也会启动
[1] 26193
[root@robert bin]# 141124 22:07:43 mysqld_safe Logging to '/data/mysql/mysql_3306/data/robert.localdomain.err'.
141124 22:07:44 mysqld_safe Starting mysqld daemon with databases from /data/mysql/mysql_3306/data
[root@robert bin]# ps -ef|grep mysqld
root 26193 4762 0 22:07 pts/1 00:00:00 /bin/sh ./mysqld_safe
mysql 26421 26193 0 22:07 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/mysql_3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/mysql_3306/data/robert.localdomain.err --pid-file=/data/mysql/mysql_3306/data/robert.localdomain.pid --socket=/tmp/mysql.sock --port=3306
root 26487 3491 0 22:11 pts/2 00:00:00 grep mysqld
看到这两个进程说明mysql已经启动成功了
启动之后也会产生一个套接文件
[root@robert /]# ls /tmp/mysql.sock
/tmp/mysql.sock
在/tmp下,如果有了这个文件,也说明mysql已经启动了,没有这个文件,mysql是不会启动的
[root@robert bin]# ./mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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>
[root@robert bin]# ./mysql -uroot -p123456
mysql>
2)关闭服务
[root@robert bin]# pwd
/usr/local/mysql/bin
[root@robert bin]# ./mysqladmin -uroot shutdown
./mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'
[root@robert bin]# ./mysqladmin -uroot shutdown -p //直接[root@robert bin]# ./mysqladmin -uroot -p123456 shutdown
Enter password:
[root@robert bin]# ./mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
2服务的方式
如果mysql是用rpm包安装的,则启动和关闭mysql服务的过程如下:
#service mysql start
#service mysql restart
#service mysql stop
注:在命令上启动mysql的时候,如果不加 “--console”,启动和关闭信息将不会再界面中显示,而是记录在安装目录下的data目录里面,文件名一般是hostname.err,可以通过此文件查看mysql控制台信息。
如何指定/etc/init.d/mysqld启动或关闭mysql
# find / -name mysql.server
/opt/mysql/mysql-5.5.37-linux2.6-x86_64/support-files/mysql.server
If you could find this file,copy it to /etc/rc.d/init.d and rename it as mysqld,then you could using " /etc/rc.d/init.d/mysqld start " to start your mysql instance.
cd /opt/mysql/mysql-5.5.37-linux2.6-x86_64/support-files/
#cp mysql.server /etc/rc.d/init.d
[root@robert bin]# /etc/init.d/mysqld stop
Shutting down MySQL. [ OK ]
[root@robert bin]# /etc/init.d/mysqld start
Starting MySQL.. [ OK ]