Ubuntu 安装MySQL

Ubuntu 安装MySQL


1.

增加用户和组:

sudo groupadd mysql

sudo useradd -g mysql mysql


2.

我下载的MySQL版本是5.5.16,

由于新版的mysql采用了cmake编译,因此需要首先安装cmake:

sudo apt-get install cmake

之后再

cmake .  \

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql  \

-DINSTALL_DATADIR=/usr/local/mysql/data \

-DEXTRA_CHARSETS=all                           #安装所有字符集


上面这句话等价与旧版本中的 ./configure --prefix=/usr/local/mysql --with--extra-charsets=all

之后再

make

sudo make install


注意:

如果之前cmake过了,那么重新cmake的时候必须手动删除旧的对象文件和缓存信息:

make clean
rm -f  CMakeCache.txt

有关cmake和原来的configure之间的转换请参考:MySQL5.5编译工具configure向cmake过渡指南



3.

完成后:

cd /usr/local/mysql

chown -R mysql .

chgrp -R mysql .

scripts/mysql_install_db --user=mysql    #初始化MySQL授权

上面语句执行后的输出内容(很有用!):

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:


./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h hutao-ubuntu password 'new-password'


Alternatively you can run:
./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 . ; ./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 with the ./bin/mysqlbug script!


之后再:

chown -R root .                #更改目录的相关属性



4.

将mysql的配置文件拷贝到/etc,并改名:

cp support-files/my-medium.cnf /etc/my.cnf 



5.

启动mysql服务:

#将mysql的启动服务添加到系统服务中

sudo cp support-files/mysql.server /etc/init.d/mysql.server


#启动mysql服务:

service mysql.server start


#停止mysql服务:

service mysql.server stop 



#重启mysql服务:

service mysql.server restart 



注意:如果mysql服务没有启动,那么运行 /usr/local/mysql/bin/mysql 时会提示:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)



6.

开机自动启动mysql服务:

在上面的基础上:

chkconfig --add mysql.server (可能需要安装chkconfig命令)



7.

我真的不知道这步是干什么用的???

启动mysql:

bin/mysqld_safe --user=mysql &        #终端会在这里定格很长时间

(初始化并测试你的mysql,其中&为后台执行的意思???)

#在这一步可能出现的问题(注意:MySQL运行的时候最好经常查看日志,这是一个好习惯!!!,日志在/usr/local/mysql/data/下)

在日志中发现:

InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 11

解决办法:

ps -a

kill -9 XXX   #终止所有与mysql有关的进程

注意:

有的时候启动mysql服务的时候会出现如下错误:

The server quit without updating PID file (/usr/local/mysql/data/XXX.pid),解决办法同上!



8.

查看是否启动成功:

netstat -tnl|grep 3306  



9.

最后终于可以使用mysql啦:

/bin/mysql


Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.16-log Source distribution


Copyright (c) 2000, 2011, 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>




10.

为root设置密码:

bin/mysqladmin -u root password 'your password'


11.

用root登录mysql:

bin/mysql -u root -p

提示输入密码。


12.

增加链接
sudo ln -s /usr/local/mysql/bin/mysql /usr/bin

以后就可以直接输入mysql -u root -p 就可以啦!


13.

在终端输入mysql后仍然可以进入mysql,使用的是匿名用户,为了更安全,我们要删除匿名用户,这样可以使得用户在终端输入mysql的时候必须提供用户名和密码才可以登录:

mysql -u root -p 进入mysql;

use mysql;

select * from user;                                 //我们可以看到这里有好几个账户,删除不必要的账户

delete from user where user="";         //删除匿名账户

flush privileges;                                    //更新权限

退出mysql,在终端再次输入mysql的时候就必须提供用户名和密码才可以登录了。




完成!


Ubuntu安装MySQL有以下步骤: 1. 首先,卸载掉原来的MySQL。可以执行以下命令来清理残留数据并验证原有主机上是否安装MySQL: - sudo apt-get autoremove --purge mysql-server - sudo apt-get remove mysql-server - sudo apt-get autoremove mysql-server - sudo apt-get remove mysql-common - dpkg -l | grep ^rc | awk '{print $2}' | sudo xargs dpkg -P 2. 更新软件并安装MySQL: - sudo apt-get install update - sudo apt-get install upgrade - sudo apt-get install mysql-server - sudo apt-get install mysql-client - sudo apt-get install libmysqlclient-dev [2] 3. 安装完成后,可以通过以下命令来查看安装情况: - sudo netstat -tap | grep mysql 这样就可以在Ubuntu上成功安装MySQL了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [ubuntu篇---ubuntu安装mysql教程](https://blog.csdn.net/m0_46825740/article/details/128145298)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Ubuntu如何安装Mysql](https://blog.csdn.net/weixin_44430893/article/details/119296615)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值