1、解压mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz,并拷贝到/usr/local/mysql目录下
解压:
tar -zxvf mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz
image.png
拷贝:
#若解压包里面的内容 已放到/usr/local/mysql 此命令可忽略
sudo cp -r mysql-5.7.23-linux-glibc2.12-x86_64 /usr/local/mysql
[sudo] password for work:
[~ mysql]$ pwd
/usr/local/mysql
[~ mysql]$ ls
bin COPYING docs include lib man README share support-files
2、添加系统mysql组和mysql用户
添加系统mysql组
sudo groupadd mysql
添加mysql用户
sudo useradd -r -g mysql mysql
添加完成后可用id mysql
查看
$ id mysql
uid=498(mysql) gid=501(mysql) groups=501(mysql)
3、安装数据库
- 切换到/usr/local/mysql目录
cd /usr/local/mysql
- 修改当前目录拥有者为mysql用户
sudo chown -R mysql:mysql ./
- 安装数据库(5.7版本的安装已经不建议使用
mysql_install_db
来安装,而是使用mysqld
)
sudo bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/mysqldata
]$ sudo bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/mysqldata
2018-07-27T08:06:26.949667Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-07-27T08:06:27.257456Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-07-27T08:06:27.316545Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-07-27T08:06:27.374921Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: f64477d9-9173-11e8-99a4-fa163e9c2931.
2018-07-27T08:06:27.377825Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-07-27T08:06:27.379223Z 1 [Note] A temporary password is generated for root@localhost: 2Dfiwj*ds8Ee
临时密码:2Dfiwj*ds8Ee
- 执行以下命令创建RSA private key
sudo bin/mysql_ssl_rsa_setup --datadir=/home/mysql/mysqldata
]$ sudo bin/mysql_ssl_rsa_setup --datadir=/home/mysql/mysqldata
Generating a 2048 bit RSA private key
..........................................................................................+++
......................................................................+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
.............................+++
..................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
............................................................+++
.....................................................................+++
writing new private key to 'client-key.pem'
-----
- 修改当前目录拥有者为root用户
sudo chown -R root:root ./
- 修改当前data目录拥有者为mysql用户
sudo chown -R mysql:mysql /home/mysql/mysqldata
]$ sudo chown -R mysql:mysql /home/mysql
]$ ls -las
total 110660
4 drwxr-xr-x 5 mysql mysql 4096 May 23 15:54 .
4 drwxr-xr-x 3 root root 4096 May 23 15:53 ..
4 -rw-r----- 1 mysql mysql 56 May 23 15:53 auto.cnf
4 -rw------- 1 mysql mysql 1675 May 23 15:54 ca-key.pem
4 -rw-r--r-- 1 mysql mysql 1107 May 23 15:54 ca.pem
4 -rw-r--r-- 1 mysql mysql 1107 May 23 15:54 client-cert.pem
4 -rw------- 1 mysql mysql 1675 May 23 15:54 client-key.pem
4 -rw-r----- 1 mysql mysql 417 May 23 15:53 ib_buffer_pool
12288 -rw-r----- 1 mysql mysql 12582912 May 23 15:53 ibdata1
49152 -rw-r----- 1 mysql mysql 50331648 May 23 15:53 ib_logfile0
49152 -rw-r----- 1 mysql mysql 50331648 May 23 15:53 ib_logfile1
4 drwxr-x--- 2 mysql mysql 4096 May 23 15:53 mysql
4 drwxr-x--- 2 mysql mysql 4096 May 23 15:53 performance_schema
4 -rw------- 1 mysql mysql 1679 May 23 15:54 private_key.pem
4 -rw-r--r-- 1 mysql mysql 451 May 23 15:54 public_key.pem
4 -rw-r--r-- 1 mysql mysql 1107 May 23 15:54 server-cert.pem
4 -rw------- 1 mysql mysql 1679 May 23 15:54 server-key.pem
12 drwxr-x--- 2 mysql mysql 12288 May 23 15:53 sys
4、配置my.cnf和开机启动
cd /usr/local/mysql/support-files
sudo cp my-default.cnf /etc/my.cnf
sudo cp mysql.server /etc/init.d/mysql
sudo vim /etc/init.d/mysql ##修改basedir= 自己的路径 修改datadir= 自己的路径
[图片上传失败...(image-ec874e-1532678468478)]
[图片上传失败...(image-256588-1532678468478)]
修改完毕后:wq!
保存并退出vi
vi命令:
按ESC键 跳到命令模式,然后:
:w 保存文件但不退出vi
:w file 将修改另外保存到file中,不退出vi
:w! 强制保存,不推出vi
:wq 保存文件并退出vi
:wq! 强制保存文件,并退出vi
q: 不保存文件,退出vi
:q! 不保存文件,强制退出vi
:e! 放弃所有修改,从上次保存文件开始再编辑
修改my.cnf
sudo vim /etc/my.cnf
[mysqld]
character-set-server=utf8
datadir=/home/mysql/mysqldata
basedir=/usr/local/mysql
socket=/var/lib/mysql/mysql.sock
image.png
保存并退出
5、启动mysql
//需要软连接下
ln -fs /usr/local/mysql/bin/mysql mysql
sudo service mysql start
6、登录mysql,并设置远程连接
mysql -u user -p
GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
flush privileges;
</article>
添加mysql 命令
vi ~/.bash_profile
添加
export PATH=/usr/local/mysql/bin:$PATH
修改密码:
mysql -uroot -hlocalhost -P3306 -p
set password = password('123456');
7、常见问题
问题:
$ mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
解决方法:
$ find / -name mysql.sock
/home/mysql/mysqldata/mysql.sock
$ cd /tmp/
$ ln -s /home/mysql/mysqldata/mysql.sock
问题:
$ mysql
bash: mysql: command not found
解决方法:
//需要软连接下
$ cd /usr/local/bin/
$ ln - s /usr/local/mysql/bin/mysql mysql
问题:
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
解决方法:
mysql>
mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
或者
mysql> set password=password("youpassword");
2、刷新权限
mysql> flush privileges;
3、help contents
mysql> help contents
You asked for help about help category: "Contents"
For more information, type 'help <item>', where <item> is one of the following
categories:
Account Management
Administration
Compound Statements
Data Definition
Data Manipulation
Data Types
Functions
Functions and Modifiers for Use with GROUP BY
Geographic Features
Help Metadata
Language Structure
Plugins
Procedures
Storage Engines
Table Maintenance
Transactions
User-Defined Functions
Utility